1 | 'use strict'
|
2 |
|
3 | const Fastify = require('../fastify')
|
4 | const immediate = require('util').promisify(setImmediate)
|
5 |
|
6 | module.exports = function asyncTest (t) {
|
7 | t.test('async onReady should be called in order', t => {
|
8 | t.plan(7)
|
9 | const fastify = Fastify()
|
10 |
|
11 | let order = 0
|
12 |
|
13 | fastify.addHook('onReady', async function () {
|
14 | await immediate()
|
15 | t.equals(order++, 0, 'called in root')
|
16 | t.equals(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
|
17 | })
|
18 |
|
19 | fastify.register(async (childOne, o) => {
|
20 | childOne.addHook('onReady', async function () {
|
21 | await immediate()
|
22 | t.equals(order++, 1, 'called in childOne')
|
23 | t.equals(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
|
24 | })
|
25 |
|
26 | childOne.register(async (childTwo, o) => {
|
27 | childTwo.addHook('onReady', async function () {
|
28 | await immediate()
|
29 | t.equals(order++, 2, 'called in childTwo')
|
30 | t.equals(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
|
31 | })
|
32 | })
|
33 | })
|
34 |
|
35 | return fastify.ready().then(() => { t.pass('ready') })
|
36 | })
|
37 |
|
38 | t.test('mix ready and onReady', t => {
|
39 | t.plan(2)
|
40 | const fastify = Fastify()
|
41 | let order = 0
|
42 |
|
43 | fastify.addHook('onReady', async function () {
|
44 | await immediate()
|
45 | order++
|
46 | })
|
47 |
|
48 | return fastify.ready()
|
49 | .then(() => {
|
50 | t.equals(order, 1)
|
51 | return fastify.ready()
|
52 | })
|
53 | .then(() => {
|
54 | t.equals(order, 1, 'ready hooks execute once')
|
55 | })
|
56 | })
|
57 |
|
58 | t.test('listen and onReady order', async t => {
|
59 | t.plan(9)
|
60 |
|
61 | const fastify = Fastify()
|
62 | let order = 0
|
63 |
|
64 | fastify.register((instance, opts, next) => {
|
65 | instance.ready(checkOrder.bind(null, 0))
|
66 | instance.addHook('onReady', checkOrder.bind(null, 4))
|
67 |
|
68 | instance.register((subinstance, opts, next) => {
|
69 | subinstance.ready(checkOrder.bind(null, 1))
|
70 | subinstance.addHook('onReady', checkOrder.bind(null, 5))
|
71 |
|
72 | subinstance.register((realSubInstance, opts, next) => {
|
73 | realSubInstance.ready(checkOrder.bind(null, 2))
|
74 | realSubInstance.addHook('onReady', checkOrder.bind(null, 6))
|
75 | next()
|
76 | })
|
77 | next()
|
78 | })
|
79 | next()
|
80 | })
|
81 |
|
82 | fastify.addHook('onReady', checkOrder.bind(null, 3))
|
83 |
|
84 | await fastify.ready()
|
85 | t.pass('trigger the onReady')
|
86 | await fastify.listen(0)
|
87 | t.pass('do not trigger the onReady')
|
88 |
|
89 | await fastify.close()
|
90 |
|
91 | function checkOrder (shouldbe) {
|
92 | t.equals(order, shouldbe)
|
93 | order++
|
94 | }
|
95 | })
|
96 |
|
97 | t.test('multiple ready calls', async t => {
|
98 | t.plan(11)
|
99 |
|
100 | const fastify = Fastify()
|
101 | let order = 0
|
102 |
|
103 | fastify.register(async (instance, opts) => {
|
104 | instance.ready(checkOrder.bind(null, 1))
|
105 | instance.addHook('onReady', checkOrder.bind(null, 6))
|
106 |
|
107 | await instance.register(async (subinstance, opts) => {
|
108 | subinstance.ready(checkOrder.bind(null, 2))
|
109 | subinstance.addHook('onReady', checkOrder.bind(null, 7))
|
110 | })
|
111 |
|
112 | t.equals(order, 0, 'ready and hooks not triggered yet')
|
113 | order++
|
114 | })
|
115 |
|
116 | fastify.addHook('onReady', checkOrder.bind(null, 3))
|
117 | fastify.addHook('onReady', checkOrder.bind(null, 4))
|
118 | fastify.addHook('onReady', checkOrder.bind(null, 5))
|
119 |
|
120 | await fastify.ready()
|
121 | t.pass('trigger the onReady')
|
122 |
|
123 | await fastify.ready()
|
124 | t.pass('do not trigger the onReady')
|
125 |
|
126 | await fastify.ready()
|
127 | t.pass('do not trigger the onReady')
|
128 |
|
129 | function checkOrder (shouldbe) {
|
130 | t.equals(order, shouldbe)
|
131 | order++
|
132 | }
|
133 | })
|
134 |
|
135 | t.test('onReady should manage error in async', t => {
|
136 | t.plan(4)
|
137 | const fastify = Fastify()
|
138 |
|
139 | fastify.addHook('onReady', function (done) {
|
140 | t.pass('called in root')
|
141 | done()
|
142 | })
|
143 |
|
144 | fastify.register(async (childOne, o) => {
|
145 | childOne.addHook('onReady', async function () {
|
146 | t.pass('called in childOne')
|
147 | throw new Error('FAIL ON READY')
|
148 | })
|
149 |
|
150 | childOne.register(async (childTwo, o) => {
|
151 | childTwo.addHook('onReady', async function () {
|
152 | t.fail('should not be called')
|
153 | })
|
154 | })
|
155 | })
|
156 |
|
157 | fastify.ready(err => {
|
158 | t.ok(err)
|
159 | t.equals(err.message, 'FAIL ON READY')
|
160 | })
|
161 | })
|
162 |
|
163 | t.test('onReady throw loading error', t => {
|
164 | t.plan(1)
|
165 | const fastify = Fastify()
|
166 |
|
167 | try {
|
168 | fastify.addHook('onReady', async function (done) {})
|
169 | } catch (e) {
|
170 | t.true(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
171 | }
|
172 | })
|
173 | }
|