-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathretry-with-a-custom-handler.test.js
179 lines (137 loc) · 5.57 KB
/
retry-with-a-custom-handler.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const { request, Agent } = require('undici')
const From = require('..')
const http = require('node:http')
function serverWithCustomError (stopAfter, statusCodeToFailOn, closeSocket) {
let requestCount = 0
return http.createServer((req, res) => {
if (requestCount++ < stopAfter) {
if (closeSocket) req.socket.end()
res.statusCode = statusCodeToFailOn
res.setHeader('Content-Type', 'text/plain')
return res.end('This Service is Unavailable')
} else {
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
return res.end(`Hello World ${requestCount}!`)
}
})
}
async function setupServer (t, fromOptions = {}, statusCodeToFailOn = 500, stopAfter = 4, closeSocket = false) {
const target = serverWithCustomError(stopAfter, statusCodeToFailOn, closeSocket)
await target.listen({ port: 0 })
t.teardown(target.close.bind(target))
const instance = Fastify()
instance.register(From, {
base: `http://localhost:${target.address().port}`
})
instance.get('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}`, fromOptions)
})
t.teardown(instance.close.bind(instance))
await instance.listen({ port: 0 })
return {
instance
}
}
test('a 500 status code with no custom handler should fail', async (t) => {
const { instance } = await setupServer(t)
const result = await request(`http://localhost:${instance.server.address().port}`, { dispatcher: new Agent({ pipelining: 3 }) })
t.equal(result.statusCode, 500)
t.equal(await result.body.text(), 'This Service is Unavailable')
})
test("a server 500's with a custom handler and should revive", async (t) => {
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
const defaultDelay = getDefaultDelay()
if (defaultDelay) return defaultDelay
if (res && res.statusCode === 500 && req.method === 'GET') {
return 0.1
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic })
const res = await request(`http://localhost:${instance.server.address().port}`)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.statusCode, 205)
t.equal(await res.body.text(), 'Hello World 5!')
})
test('custom retry does not invoke the default delay causing a 501', async (t) => {
// the key here is our retryDelay doesn't register the deefault handler and as a result it doesn't work
const customRetryLogic = ({ req, res }) => {
if (res && res.statusCode === 500 && req.method === 'GET') {
return 0
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic }, 501)
const res = await request(`http://localhost:${instance.server.address().port}`)
t.equal(res.statusCode, 501)
t.equal(await res.body.text(), 'This Service is Unavailable')
})
test('custom retry delay functions can invoke the default delay', async (t) => {
const customRetryLogic = ({ req, res, getDefaultDelay }) => {
// registering the default retry logic for non 500 errors if it occurs
const defaultDelay = getDefaultDelay()
if (defaultDelay) return defaultDelay
if (res && res.statusCode === 500 && req.method === 'GET') {
return 0.1
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic }, 500)
const res = await request(`http://localhost:${instance.server.address().port}`)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.statusCode, 205)
t.equal(await res.body.text(), 'Hello World 5!')
})
test('custom retry delay function inspects the err paramater', async (t) => {
const customRetryLogic = ({ err }) => {
if (err && (err.code === 'UND_ERR_SOCKET' || err.code === 'ECONNRESET')) {
return 0.1
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic }, 500, 4, true)
const res = await request(`http://localhost:${instance.server.address().port}`)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.statusCode, 205)
t.equal(await res.body.text(), 'Hello World 5!')
})
test('we can exceed our retryCount and introspect attempts independently', async (t) => {
const attemptCounter = []
const customRetryLogic = ({ err, attempt }) => {
attemptCounter.push(attempt)
if (err && (err.code === 'UND_ERR_SOCKET' || err.code === 'ECONNRESET')) {
return 0.1
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic }, 500, 4, true)
const res = await request(`http://localhost:${instance.server.address().port}`)
t.match(attemptCounter, [0, 1, 2, 3, 4])
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.statusCode, 205)
t.equal(await res.body.text(), 'Hello World 5!')
})
test('we handle our retries based on the retryCount', async (t) => {
const attemptCounter = []
const customRetryLogic = ({ req, res, attempt, retriesCount }) => {
if (retriesCount < attempt) {
return null
}
if (res && res.statusCode === 500 && req.method === 'GET') {
attemptCounter.push(attempt)
return 0.1
}
return null
}
const { instance } = await setupServer(t, { retryDelay: customRetryLogic, retriesCount: 2 }, 500)
await request(`http://localhost:${instance.server.address().port}`)
const res = await request(`http://localhost:${instance.server.address().port}`)
t.match(attemptCounter, [0, 1])
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.statusCode, 205)
t.equal(await res.body.text(), 'Hello World 5!')
})