@@ -3,7 +3,7 @@ const { promisify } = require('util')
3
3
4
4
const tap = require ( 'tap' )
5
5
const fastify = require ( 'fastify' )
6
- const { request } = require ( 'undici' )
6
+ const { request, Client } = require ( 'undici' )
7
7
8
8
const plugin = require ( '../.' )
9
9
const { Errors } = require ( '../lib' )
@@ -85,8 +85,6 @@ tap.test('fastify-racing#decoration', subtest => {
85
85
)
86
86
} )
87
87
88
- // TODO: find what's hanging the tests
89
- // TODO: remove "only" once done
90
88
tap . test ( 'fastify-racing#promise' , { only : true } , subtest => {
91
89
subtest . plan ( 4 )
92
90
@@ -113,7 +111,7 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
113
111
114
112
app
115
113
. ready ( )
116
- . then ( ( ) => app . listen ( ) )
114
+ . then ( ( ) => app . listen ( { port : 0 } ) )
117
115
. then ( async ( ) => {
118
116
request (
119
117
`http://localhost:${ app . server . address ( ) . port } ` ,
@@ -196,7 +194,7 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
196
194
197
195
app
198
196
. ready ( )
199
- . then ( ( ) => app . listen ( ) )
197
+ . then ( ( ) => app . listen ( { port : 0 } ) )
200
198
. then ( async ( ) => {
201
199
request (
202
200
`http://localhost:${ app . server . address ( ) . port } ` ,
@@ -220,9 +218,14 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
220
218
subtest . test (
221
219
'Should reuse AbortController for the single request' ,
222
220
async t => {
223
- let first
221
+ // eslint-disable-next-line
222
+ let first , client
224
223
const app = fastify ( )
225
224
225
+ t . teardown ( async ( ) => {
226
+ await client . destroy ( )
227
+ await app . close ( )
228
+ } )
226
229
t . plan ( 5 )
227
230
228
231
app . register ( plugin )
@@ -243,79 +246,81 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
243
246
t . notOk ( second . aborted )
244
247
t . equal ( second , first , 'Should reuse the initial controller' )
245
248
246
- return 'Hello World'
249
+ _reply . send ( 'Hello World' )
247
250
}
248
251
)
249
252
250
- t . teardown ( ( ) => app . close ( ) )
253
+ await app . listen ( { port : 0 } )
251
254
252
- await app . listen ( )
255
+ client = new Client ( `http://localhost: ${ app . server . address ( ) . port } ` )
253
256
254
- const response = await request (
255
- `http://localhost:${ app . server . address ( ) . port } ` ,
256
- {
257
- method : 'GET' ,
258
- path : '/'
259
- }
260
- )
257
+ const response = await client . request ( {
258
+ method : 'GET' ,
259
+ path : '/'
260
+ } )
261
+
262
+ const responseBody = await response . body . text ( )
261
263
262
264
t . equal ( response . statusCode , 200 )
263
- t . equal ( await response . body . text ( ) , 'Hello World' )
264
- t . end ( )
265
+ t . equal ( responseBody , 'Hello World' )
265
266
}
266
267
)
267
268
268
269
// TODO: Find how to close the socket after request finished
269
- subtest . test (
270
- 'Should throw on already closed request' ,
271
- async t => {
272
- let first
273
- const app = fastify ( )
270
+ subtest . test ( 'Should throw on already closed request' , async t => {
271
+ // eslint-disable-next-line
272
+ let first , client
273
+ const app = fastify ( )
274
274
275
- t . plan ( 7 )
275
+ t . teardown ( async ( ) => {
276
+ await client . destroy ( )
277
+ await app . close ( )
278
+ } )
276
279
277
- app . register ( plugin )
280
+ t . plan ( 7 )
278
281
279
- app . get (
280
- '/' ,
281
- {
282
- onResponse : async ( req , _reply , done ) => {
283
- req . raw . destroy ( )
282
+ app . register ( plugin )
284
283
285
- try {
286
- first = await req . race ( )
287
- } catch ( err ) {
288
- t . ok ( err )
289
- t . ok ( err instanceof Errors . SOCKET_CLOSED )
290
- t . equal ( err . code , 'FST_PLUGIN_RACE_SOCKET_CLOSED' )
291
- t . equal ( err . statusCode , 500 )
292
- }
284
+ app . get (
285
+ '/' ,
286
+ {
287
+ onResponse : async ( req , _reply , done ) => {
288
+ req . raw . destroy ( )
293
289
294
- t . notOk ( first )
295
- done ( )
290
+ try {
291
+ first = await req . race ( )
292
+ } catch ( err ) {
293
+ t . ok ( err )
294
+ t . ok ( err instanceof Errors . SOCKET_CLOSED )
295
+ t . equal ( err . code , 'FST_PLUGIN_RACE_SOCKET_CLOSED' )
296
+ t . equal ( err . statusCode , 500 )
296
297
}
297
- } ,
298
- ( req , _reply ) => {
299
- return 'Hello World'
298
+
299
+ t . notOk ( first )
300
+ done ( )
300
301
}
301
- )
302
+ } ,
303
+ ( req , _reply ) => {
304
+ return 'Hello World'
305
+ }
306
+ )
302
307
303
- t . teardown ( ( ) => app . close ( ) )
308
+ t . teardown ( ( ) => app . close ( ) )
304
309
305
- await app . listen ( )
310
+ await app . listen ( { port : 0 } )
306
311
307
- const response = await request (
308
- `http://localhost:${ app . server . address ( ) . port } ` ,
309
- {
310
- method : 'GET' ,
311
- path : '/'
312
- }
313
- )
312
+ client = new Client ( `http://localhost:${ app . server . address ( ) . port } ` )
314
313
315
- t . equal ( response . statusCode , 200 )
316
- t . equal ( await response . body . text ( ) , 'Hello World' )
317
- }
318
- )
314
+ const response = await client . request (
315
+ {
316
+ method : 'GET' ,
317
+ path : '/'
318
+ }
319
+ )
320
+
321
+ t . equal ( response . statusCode , 200 )
322
+ t . equal ( await response . body . text ( ) , 'Hello World' )
323
+ } )
319
324
320
325
async function dummy ( signal , ms = 3000 ) {
321
326
await sleep ( ms , null , { signal, ref : false } )
0 commit comments