Skip to content

Commit

Permalink
fix: delay plugin promise resolution until next tick (#173)
Browse files Browse the repository at this point in the history
* fix: delay plugin promise resolution until next tick

* replace some of the `sleep` with FS reads

* fix fs.promises import
  • Loading branch information
chrskrchr authored Feb 25, 2022
1 parent 067dd04 commit 15c4cd3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
20 changes: 11 additions & 9 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ Plugin.prototype.loadedSoFar = function () {
this._error = err
this.q.pause()

if (err) {
debug('rejecting promise', this.name, err)
this._promise.reject(err)
} else {
debug('resolving promise', this.name)
this._promise.resolve()
}
this._promise = null
process.nextTick(() => {
if (err) {
debug('rejecting promise', this.name, err)
this._promise.reject(err)
} else {
debug('resolving promise', this.name)
this._promise.resolve()
}
this._promise = null

process.nextTick(cb, err)
process.nextTick(cb, err)
})
})
this.q.resume()
}
Expand Down
54 changes: 53 additions & 1 deletion test/await-after.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { test } = require('tap')
const boot = require('..')
const { promisify } = require('util')
const sleep = promisify(setTimeout)
const fs = require('fs').promises
const path = require('path')

test('await after - nested plugins with same tick callbacks', async (t) => {
const app = {}
Expand Down Expand Up @@ -247,7 +249,7 @@ test('await after complex scenario', async (t) => {
t.notOk(fourthLoaded, 'fourth is not loaded')
app.use(second)
t.ok(firstLoaded, 'first is loaded')
t.notOk(secondLoaded, 'second is not loaded')
t.ok(secondLoaded, 'second is loaded')
t.notOk(thirdLoaded, 'third is not loaded')
t.notOk(fourthLoaded, 'fourth is not loaded')
app.use(third)
Expand Down Expand Up @@ -280,6 +282,56 @@ test('await after complex scenario', async (t) => {
}
})

test('without autostart and sync/async plugin mix', async (t) => {
const app = {}
boot(app, { autostart: false })
t.plan(11)

let firstLoaded = false
let secondLoaded = false
let thirdLoaded = false

app.use(first)
await app.after()
t.ok(firstLoaded, 'first is loaded')
t.notOk(secondLoaded, 'second is not loaded')
t.notOk(thirdLoaded, 'third is not loaded')

const contents = await fs.readFile(path.join(__dirname, 'fixtures', 'dummy.txt'), 'utf-8')
t.equal(contents, 'hello, world!')

app.use(second)
await app.after()
t.ok(firstLoaded, 'first is loaded')
t.ok(secondLoaded, 'second is loaded')
t.notOk(thirdLoaded, 'third is not loaded')

await sleep(10)

app.use(third)
await app.after()
t.ok(firstLoaded, 'first is loaded')
t.ok(secondLoaded, 'second is loaded')
t.ok(thirdLoaded, 'third is loaded')

await app.ready()

async function first () {
firstLoaded = true
}

async function second () {
const contents = await fs.readFile(path.join(__dirname, 'fixtures', 'dummy.txt'), 'utf-8')
t.equal(contents, 'hello, world!')
secondLoaded = true
}

async function third (app) {
await sleep(10)
thirdLoaded = true
}
})

test('without autostart', async (t) => {
const app = {}
boot(app, { autostart: false })
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/dummy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!

0 comments on commit 15c4cd3

Please sign in to comment.