File size: 2,520 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'

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

test('reentrant', (t) => {
  t.plan(7)

  const app = boot()
  let firstLoaded = false
  let secondLoaded = false

  app
    .use(first)
    .after(() => {
      t.ok(firstLoaded, 'first is loaded')
      t.ok(secondLoaded, 'second is loaded')
      t.pass('booted')
    })

  function first (s, opts, done) {
    t.notOk(firstLoaded, 'first is not loaded')
    t.notOk(secondLoaded, 'second is not loaded')
    firstLoaded = true
    s.use(second)
    done()
  }

  function second (s, opts, done) {
    t.ok(firstLoaded, 'first is loaded')
    t.notOk(secondLoaded, 'second is not loaded')
    secondLoaded = true
    done()
  }
})

test('reentrant with callbacks deferred', (t) => {
  t.plan(11)

  const app = boot()
  let firstLoaded = false
  let secondLoaded = false
  let thirdLoaded = false

  app.use(first)

  function first (s, opts, done) {
    t.notOk(firstLoaded, 'first is not loaded')
    t.notOk(secondLoaded, 'second is not loaded')
    t.notOk(thirdLoaded, 'third is not loaded')
    firstLoaded = true
    s.use(second)
    setTimeout(() => {
      try {
        s.use(third)
      } catch (err) {
        t.equal(err.message, 'Root plugin has already booted')
      }
    }, 500)
    done()
  }

  function second (s, opts, done) {
    t.ok(firstLoaded, 'first is loaded')
    t.notOk(secondLoaded, 'second is not loaded')
    t.notOk(thirdLoaded, 'third is not loaded')
    secondLoaded = true
    done()
  }

  function third (s, opts, done) {
    thirdLoaded = true
    done()
  }

  app.on('start', () => {
    t.ok(firstLoaded, 'first is loaded')
    t.ok(secondLoaded, 'second is loaded')
    t.notOk(thirdLoaded, 'third is not loaded')
    t.pass('booted')
  })
})

test('multiple loading time', t => {
  t.plan(1)
  const app = boot()

  function a (instance, opts, done) {
    (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })
    setTimeout(done, 10)
  }
  const pointer = a

  function b (instance, opts, done) {
    (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })
    setTimeout(done, 20)
  }

  function c (instance, opts, done) {
    (opts.use || []).forEach(_ => { instance.use(_, { use: opts.subUse || [] }) })
    setTimeout(done, 30)
  }

  app
    .use(function a (instance, opts, done) {
      instance.use(pointer, { use: [b], subUse: [c] })
        .use(b)
      setTimeout(done, 0)
    })
    .after(() => {
      t.pass('booted')
    })
})