Skip to content

Commit 9c895a1

Browse files
authored
Fix timeout count for nested plugins (#254)
* Fix timeout count for nested plugins Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 7e66765 commit 9c895a1

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

boot.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,16 @@ Boot.prototype._addPlugin = function (pluginFn, opts, isAfter) {
173173
// we always add plugins to load at the current element
174174
const current = this._current[0]
175175

176-
// In case of promises, adjust the timeout
177-
if (isAfter && this._lastUsed) {
178-
// We need to decrease it by 2ms to make sure the internal timeout
179-
// is triggered earlier
180-
const delta = Date.now() - current.startTime + 2
181-
if (this._lastUsed.timeout > 0 && delta > 0) {
182-
this._lastUsed.timeout = this._lastUsed.timeout - delta
183-
}
176+
let timeout = this._opts.timeout
177+
178+
if (!current.loaded && current.timeout > 0) {
179+
const delta = Date.now() - current.startTime
180+
// We need to decrease it by 3ms to make sure the internal timeout
181+
// is triggered earlier than the parent
182+
timeout = current.timeout - (delta + 3)
184183
}
185184

186-
const plugin = new Plugin(fastq(this, this._loadPluginNextTick, 1), pluginFn, opts, isAfter, this._opts.timeout)
185+
const plugin = new Plugin(fastq(this, this._loadPluginNextTick, 1), pluginFn, opts, isAfter, timeout)
187186
this._trackPluginLoading(plugin)
188187

189188
if (current.loaded) {

test/plugin-timeout-await.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
/* eslint no-prototype-builtins: off */
4+
5+
const { test } = require('tap')
6+
const boot = require('..')
7+
8+
test('do not load', async (t) => {
9+
const app = boot({}, { timeout: 10 })
10+
11+
app.use(first)
12+
13+
async function first (s, opts) {
14+
await s.use(second)
15+
}
16+
17+
async function second (s, opts) {
18+
await s.use(third)
19+
}
20+
21+
function third (s, opts) {
22+
return new Promise((resolve, reject) => {
23+
// no resolve
24+
})
25+
}
26+
27+
try {
28+
await app.start()
29+
t.fail('should throw')
30+
} catch (err) {
31+
t.equal(err.message, 'Plugin did not start in time: \'third\'. You may have forgotten to call \'done\' function or to resolve a Promise')
32+
}
33+
})

0 commit comments

Comments
 (0)