|
1 | 1 | 'use strict'
|
| 2 | +const { pipeline } = require('node:stream/promises') |
| 3 | +const { Readable, PassThrough } = require('node:stream') |
| 4 | + |
2 | 5 | const { promisify } = require('util')
|
3 | 6 |
|
4 | 7 | const tap = require('tap')
|
@@ -86,7 +89,7 @@ tap.test('fastify-racing#decoration', subtest => {
|
86 | 89 | })
|
87 | 90 |
|
88 | 91 | tap.test('fastify-racing#promise', { only: true }, subtest => {
|
89 |
| - subtest.plan(4) |
| 92 | + subtest.plan(6) |
90 | 93 |
|
91 | 94 | subtest.test('Should handle a request aborted', t => {
|
92 | 95 | t.plan(3)
|
@@ -124,13 +127,111 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
|
124 | 127 | t.ok(err)
|
125 | 128 | }
|
126 | 129 | )
|
127 |
| - |
128 | 130 | // Allow a full event loop cycle
|
129 | 131 | await sleep(5)
|
130 | 132 | abtCtlr.abort()
|
131 | 133 | })
|
132 | 134 | })
|
133 | 135 |
|
| 136 | + subtest.test('Should not incorrectly abort request when body stream consumed', t => { |
| 137 | + t.plan(4) |
| 138 | + |
| 139 | + const app = fastify() |
| 140 | + app.register(plugin) |
| 141 | + app.addContentTypeParser('application/octet-stream', {}, (_req, payload, done) => done(null, payload)) |
| 142 | + |
| 143 | + t.teardown(() => app.close()) |
| 144 | + |
| 145 | + app.post('/', async (req, _reply) => { |
| 146 | + const signal = req.race() |
| 147 | + |
| 148 | + // consume stream |
| 149 | + await pipeline(req.body, new PassThrough()) |
| 150 | + await sleep(5) // Allow a full event loop cycle |
| 151 | + t.equal(signal.aborted, false) |
| 152 | + |
| 153 | + const result = await Promise.race([signal, dummy(signal)]) |
| 154 | + t.equal(typeof result, 'string') |
| 155 | + |
| 156 | + if (result.type === 'aborted') return '' |
| 157 | + else return `${result}-world` |
| 158 | + }) |
| 159 | + |
| 160 | + app |
| 161 | + .ready() |
| 162 | + .then(() => app.listen({ port: 0 })) |
| 163 | + .then(async () => { |
| 164 | + request( |
| 165 | + `http://localhost:${app.server.address().port}`, |
| 166 | + { |
| 167 | + method: 'POST', |
| 168 | + path: '/', |
| 169 | + signal: undefined, |
| 170 | + body: Readable.from('stream data'), |
| 171 | + headers: { |
| 172 | + 'Content-Type': 'application/octet-stream' |
| 173 | + } |
| 174 | + }, |
| 175 | + (err, res) => { |
| 176 | + t.error(err) |
| 177 | + t.equal(res.statusCode, 200) |
| 178 | + } |
| 179 | + ) |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + subtest.test('Should handle a stream request aborted', t => { |
| 184 | + t.plan(5) |
| 185 | + |
| 186 | + const app = fastify() |
| 187 | + const abtCtlr = new AbortController() |
| 188 | + app.register(plugin) |
| 189 | + app.addContentTypeParser('application/octet-stream', {}, (_req, payload, done) => done(null, payload)) |
| 190 | + |
| 191 | + t.teardown(() => app.close()) |
| 192 | + |
| 193 | + app.post('/', async (req, _reply) => { |
| 194 | + const signal = req.race() |
| 195 | + |
| 196 | + // consume stream |
| 197 | + await pipeline(req.body, new PassThrough()) |
| 198 | + await sleep(5) // Allow a full event loop cycle |
| 199 | + t.equal(signal.aborted, false) |
| 200 | + |
| 201 | + const result = await Promise.race([signal, dummy(signal)]) |
| 202 | + t.equal(signal.aborted, true) |
| 203 | + t.equal(typeof result, 'object') |
| 204 | + t.equal(result.type, 'abort') |
| 205 | + |
| 206 | + if (result.type === 'aborted') return '' |
| 207 | + else return `${result}-world` |
| 208 | + }) |
| 209 | + |
| 210 | + app |
| 211 | + .ready() |
| 212 | + .then(() => app.listen({ port: 0 })) |
| 213 | + .then(async () => { |
| 214 | + request( |
| 215 | + `http://localhost:${app.server.address().port}`, |
| 216 | + { |
| 217 | + method: 'POST', |
| 218 | + path: '/', |
| 219 | + signal: abtCtlr.signal, |
| 220 | + body: Readable.from('stream data'), |
| 221 | + headers: { |
| 222 | + 'Content-Type': 'application/octet-stream' |
| 223 | + } |
| 224 | + }, |
| 225 | + err => { |
| 226 | + t.ok(err) |
| 227 | + } |
| 228 | + ) |
| 229 | + // Allow multiple event loop cycles |
| 230 | + await sleep(20) |
| 231 | + abtCtlr.abort() |
| 232 | + }) |
| 233 | + }) |
| 234 | + |
134 | 235 | subtest.test(
|
135 | 236 | 'Should be able to handle more than one race check within a request',
|
136 | 237 | t => {
|
|
0 commit comments