Skip to content

Commit

Permalink
Fix timeout count for nested plugins (#254)
Browse files Browse the repository at this point in the history
* Fix timeout count for nested plugins

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 21, 2024
1 parent 7e66765 commit 9c895a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
17 changes: 8 additions & 9 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,16 @@ Boot.prototype._addPlugin = function (pluginFn, opts, isAfter) {
// we always add plugins to load at the current element
const current = this._current[0]

// In case of promises, adjust the timeout
if (isAfter && this._lastUsed) {
// We need to decrease it by 2ms to make sure the internal timeout
// is triggered earlier
const delta = Date.now() - current.startTime + 2
if (this._lastUsed.timeout > 0 && delta > 0) {
this._lastUsed.timeout = this._lastUsed.timeout - delta
}
let timeout = this._opts.timeout

if (!current.loaded && current.timeout > 0) {
const delta = Date.now() - current.startTime
// We need to decrease it by 3ms to make sure the internal timeout
// is triggered earlier than the parent
timeout = current.timeout - (delta + 3)
}

const plugin = new Plugin(fastq(this, this._loadPluginNextTick, 1), pluginFn, opts, isAfter, this._opts.timeout)
const plugin = new Plugin(fastq(this, this._loadPluginNextTick, 1), pluginFn, opts, isAfter, timeout)
this._trackPluginLoading(plugin)

if (current.loaded) {
Expand Down
33 changes: 33 additions & 0 deletions test/plugin-timeout-await.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

/* eslint no-prototype-builtins: off */

const { test } = require('tap')
const boot = require('..')

test('do not load', async (t) => {
const app = boot({}, { timeout: 10 })

app.use(first)

async function first (s, opts) {
await s.use(second)
}

async function second (s, opts) {
await s.use(third)
}

function third (s, opts) {
return new Promise((resolve, reject) => {
// no resolve
})
}

try {
await app.start()
t.fail('should throw')
} catch (err) {
t.equal(err.message, 'Plugin did not start in time: \'third\'. You may have forgotten to call \'done\' function or to resolve a Promise')
}
})

0 comments on commit 9c895a1

Please sign in to comment.