strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('tap')4const boot = require('..')5 6test('reentrant', (t) => {7 t.plan(7)8 9 const app = boot()10 let firstLoaded = false11 let secondLoaded = false12 13 app14 .use(first)15 .after(() => {16 t.ok(firstLoaded, 'first is loaded')17 t.ok(secondLoaded, 'second is loaded')18 t.pass('booted')19 })20 21 function first (s, opts, done) {22 t.notOk(firstLoaded, 'first is not loaded')23 t.notOk(secondLoaded, 'second is not loaded')24 firstLoaded = true25 s.use(second)26 done()27 }28 29 function second (s, opts, done) {30 t.ok(firstLoaded, 'first is loaded')31 t.notOk(secondLoaded, 'second is not loaded')32 secondLoaded = true33 done()34 }35})36 37test('reentrant with callbacks deferred', (t) => {38 t.plan(11)39 40 const app = boot()41 let firstLoaded = false42 let secondLoaded = false43 let thirdLoaded = false44 45 app.use(first)46 47 function first (s, opts, done) {48 t.notOk(firstLoaded, 'first is not loaded')49 t.notOk(secondLoaded, 'second is not loaded')50 t.notOk(thirdLoaded, 'third is not loaded')51 firstLoaded = true52 s.use(second)53 setTimeout(() => {54 try {55 s.use(third)56 } catch (err) {57 t.equal(err.message, 'Root plugin has already booted')58 }59 }, 500)60 done()61 }62 63 function second (s, opts, done) {64 t.ok(firstLoaded, 'first is loaded')65 t.notOk(secondLoaded, 'second is not loaded')66 t.notOk(thirdLoaded, 'third is not loaded')67 secondLoaded = true68 done()69 }70 71 function third (s, opts, done) {72 thirdLoaded = true73 done()74 }75 76 app.on('start', () => {77 t.ok(firstLoaded, 'first is loaded')78 t.ok(secondLoaded, 'second is loaded')79 t.notOk(thirdLoaded, 'third is not loaded')80 t.pass('booted')81 })82})83 84test('multiple loading time', t => {85 t.plan(1)86 const app = boot()87 88 function a (instance, opts, done) {89 (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })90 setTimeout(done, 10)91 }92 const pointer = a93 94 function b (instance, opts, done) {95 (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })96 setTimeout(done, 20)97 }98 99 function c (instance, opts, done) {100 (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })101 setTimeout(done, 30)102 }103 104 app105 .use(function a (instance, opts, done) {106 instance.use(pointer, { use: [b], subUse: [c] })107 .use(b)108 setTimeout(done, 0)109 })110 .after(() => {111 t.pass('booted')112 })113})114 