CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
plugin-timeout.test.js219 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('tap')4const boot = require('..')5 6const message = (name) => `Plugin did not start in time: '${name}'. You may have forgotten to call 'done' function or to resolve a Promise`7 8test('timeout without calling next - callbacks', (t) => {9  t.plan(4)10  const app = boot({}, {11    timeout: 10 // 10 ms12  })13  app.use(one)14  function one (app, opts, next) {15    // do not call next on purpose16  }17  app.ready((err) => {18    t.ok(err)19    t.equal(err.fn, one)20    t.equal(err.message, message('one'))21    t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')22  })23})24 25test('timeout without calling next - promises', (t) => {26  t.plan(4)27  const app = boot({}, {28    timeout: 10 // 10 ms29  })30  app.use(two)31  function two (app, opts) {32    return new Promise(function (resolve) {33      // do not call resolve on purpose34    })35  }36  app.ready((err) => {37    t.ok(err)38    t.equal(err.fn, two)39    t.equal(err.message, message('two'))40    t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')41  })42})43 44test('timeout without calling next - use file as name', (t) => {45  t.plan(3)46  const app = boot({}, {47    timeout: 10 // 10 ms48  })49  app.use(require('./fixtures/plugin-no-next'))50  app.ready((err) => {51    t.ok(err)52    t.equal(err.message, message('noNext'))53    t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')54  })55})56 57test('timeout without calling next - use code as name', (t) => {58  t.plan(3)59  const app = boot({}, {60    timeout: 10 // 10 ms61  })62  app.use(function (app, opts, next) {63    // do not call next on purpose - code as name64  })65 66  app.ready((err) => {67    t.ok(err)68    t.equal(err.message, message('function (app, opts, next) { -- // do not call next on purpose - code as name'))69    t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')70  })71})72 73test('does not keep going', (t) => {74  t.plan(2)75  const app = boot({}, {76    timeout: 10 // 10 ms77  })78  app.use(function three (app, opts, next) {79    next(new Error('kaboom'))80  })81  app.ready((err) => {82    t.ok(err)83    t.equal(err.message, 'kaboom')84  })85})86 87test('throw in override without autostart', (t) => {88  t.plan(2)89 90  const server = { my: 'server' }91  const app = boot(server, {92    timeout: 10,93    autostart: false94  })95 96  app.override = function (s) {97    throw new Error('kaboom')98  }99 100  app.use(function (s, opts, cb) {101    t.fail('this is never reached')102  })103 104  setTimeout(function () {105    app.ready((err) => {106      t.ok(err)107      t.equal(err.message, 'kaboom')108    })109  }, 20)110})111 112test('timeout without calling next in ready and ignoring the error', (t) => {113  t.plan(11)114  const app = boot({}, {115    timeout: 10, // 10 ms116    autostart: false117  })118 119  let preReady = false120 121  app.use(function one (app, opts, next) {122    t.pass('loaded')123    app.ready(function readyOk (err, done) {124      t.notOk(err)125      t.pass('first ready called')126      done()127    })128    next()129  })130 131  app.on('preReady', () => {132    t.pass('preReady should be called')133    preReady = true134  })135 136  app.on('start', () => {137    t.pass('start should be called')138  })139 140  app.ready(function onReadyWithoutDone (err, done) {141    t.pass('wrong ready called')142    t.ok(preReady, 'preReady already called')143    t.notOk(err)144    // done() // Don't call done145  })146 147  app.ready(function onReadyTwo (err) {148    t.ok(err)149    t.equal(err.message, message('onReadyWithoutDone'))150    t.equal(err.code, 'AVV_ERR_READY_TIMEOUT')151    // don't rethrow the error152  })153 154  app.start()155})156 157test('timeout without calling next in ready and rethrowing the error', (t) => {158  t.plan(11)159  const app = boot({}, {160    timeout: 10, // 10 ms161    autostart: true162  })163 164  app.use(function one (app, opts, next) {165    t.pass('loaded')166    app.ready(function readyOk (err, done) {167      t.ok(err)168      t.equal(err.message, message('onReadyWithoutDone'))169      t.equal(err.code, 'AVV_ERR_READY_TIMEOUT')170      done(err)171    })172    next()173  })174 175  app.on('preReady', () => {176    t.pass('preReady should be called')177  })178 179  app.on('start', () => {180    t.pass('start should be called in any case')181  })182 183  app.ready(function onReadyWithoutDone (err, done) {184    t.pass('wrong ready called')185    t.notOk(err)186    // done() // Don't call done187  })188 189  app.ready(function onReadyTwo (err, done) {190    t.ok(err)191    t.equal(err.message, message('onReadyWithoutDone'))192    t.equal(err.code, 'AVV_ERR_READY_TIMEOUT')193    done(err)194  })195 196  app.start()197})198 199test('nested timeout do not crash - await', (t) => {200  t.plan(4)201  const app = boot({}, {202    timeout: 10 // 10 ms203  })204  app.use(one)205  async function one (app, opts) {206    await app.use(two)207  }208 209  function two (app, opts, next) {210    // do not call next on purpose211  }212  app.ready((err) => {213    t.ok(err)214    t.equal(err.fn, two)215    t.equal(err.message, message('two'))216    t.equal(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')217  })218})219