forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.5.test.js
122 lines (109 loc) · 3 KB
/
listen.5.test.js
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
115
116
117
118
119
120
121
122
'use strict'
const { test } = require('node:test')
const net = require('node:net')
const Fastify = require('../fastify')
const { once } = require('node:events')
const { FSTWRN003 } = require('../lib/warnings.js')
function createDeferredPromise () {
const promise = {}
promise.promise = new Promise((resolve) => {
promise.resolve = resolve
})
return promise
}
test('same port conflict and success should not fire callback multiple times - callback', async (t) => {
t.plan(7)
const server = net.createServer()
server.listen({ port: 0, host: '127.0.0.1' })
await once(server, 'listening')
const option = { port: server.address().port, host: server.address().address }
let count = 0
const fastify = Fastify()
const promise = createDeferredPromise()
function callback (err) {
switch (count) {
case 6: {
// success in here
t.assert.ifError(err)
fastify.close((err) => {
t.assert.ifError(err)
promise.resolve()
})
break
}
case 5: {
server.close()
setTimeout(() => {
fastify.listen(option, callback)
}, 100)
break
}
default: {
// expect error
t.assert.strictEqual(err.code, 'EADDRINUSE')
setTimeout(() => {
fastify.listen(option, callback)
}, 100)
}
}
count++
}
fastify.listen(option, callback)
await promise.promise
})
test('same port conflict and success should not fire callback multiple times - promise', async (t) => {
t.plan(5)
const server = net.createServer()
server.listen({ port: 0, host: '127.0.0.1' })
await once(server, 'listening')
const option = { port: server.address().port, host: server.address().address }
const fastify = Fastify()
try {
await fastify.listen(option)
} catch (err) {
t.assert.strictEqual(err.code, 'EADDRINUSE')
}
try {
await fastify.listen(option)
} catch (err) {
t.assert.strictEqual(err.code, 'EADDRINUSE')
}
try {
await fastify.listen(option)
} catch (err) {
t.assert.strictEqual(err.code, 'EADDRINUSE')
}
try {
await fastify.listen(option)
} catch (err) {
t.assert.strictEqual(err.code, 'EADDRINUSE')
}
try {
await fastify.listen(option)
} catch (err) {
t.assert.strictEqual(err.code, 'EADDRINUSE')
}
server.close()
await once(server, 'close')
// when ever we can listen, and close properly
// which means there is no problem on the callback
await fastify.listen()
await fastify.close()
})
test('should emit a warning when using async callback', (t, done) => {
t.plan(2)
process.on('warning', onWarning)
function onWarning (warning) {
t.assert.strictEqual(warning.name, 'FastifyWarning')
t.assert.strictEqual(warning.code, FSTWRN003.code)
}
const fastify = Fastify()
t.after(async () => {
await fastify.close()
process.removeListener('warning', onWarning)
FSTWRN003.emitted = false
})
fastify.listen({ port: 0 }, async function doNotUseAsyncCallback () {
done()
})
})