@@ -32,7 +32,6 @@ const a: any = require('awaiting');
32
32
const request : any = require ( 'requisition' ) ;
33
33
import fs from 'fs' ;
34
34
import { Stopper } from '../../../plugin/drainHttpServer/stoppable' ;
35
- import child from 'child_process' ;
36
35
import path from 'path' ;
37
36
import type { AddressInfo } from 'net' ;
38
37
import { describe , it , expect , afterEach , beforeEach } from '@jest/globals' ;
@@ -136,7 +135,9 @@ Object.keys(schemes).forEach((schemeName) => {
136
135
) ,
137
136
) ;
138
137
expect ( err . code ) . toMatch ( / E C O N N R E F U S E D / ) ;
139
- expect ( closed ) . toBe ( 0 ) ;
138
+
139
+ const isNode20 = ! ! process . version . match ( / ^ v 2 0 \. / ) ;
140
+ expect ( closed ) . toBe ( isNode20 ? 1 : 0 ) ;
140
141
} ) ;
141
142
} ) ;
142
143
@@ -301,12 +302,21 @@ Object.keys(schemes).forEach((schemeName) => {
301
302
302
303
if ( schemeName === 'http' ) {
303
304
it ( 'with in-flights finishing before grace period ends' , async ( ) => {
304
- const file = path . join ( __dirname , 'stoppable' , 'server.js' ) ;
305
- const server = child . spawn ( 'node' , [ file ] ) ;
306
- const [ data ] = await a . event ( server . stdout , 'data' ) ;
307
- const port = + data . toString ( ) ;
308
- expect ( typeof port ) . toBe ( 'number' ) ;
309
- const res = await request ( `${ schemeName } ://localhost:${ port } /` ) . agent (
305
+ let stopper : Stopper ;
306
+ const killServerBarrier = resolvable ( ) ;
307
+ const server = http . createServer ( async ( _ , res ) => {
308
+ res . writeHead ( 200 ) ;
309
+ res . write ( 'hello' ) ;
310
+
311
+ await killServerBarrier ;
312
+ res . end ( 'world' ) ;
313
+ await stopper . stop ( ) ;
314
+ } ) ;
315
+ stopper = new Stopper ( server ) ;
316
+ server . listen ( 0 ) ;
317
+ const p = port ( server ) ;
318
+
319
+ const res = await request ( `${ schemeName } ://localhost:${ p } /` ) . agent (
310
320
scheme . agent ( { keepAlive : true } ) ,
311
321
) ;
312
322
let gotBody = false ;
@@ -320,13 +330,13 @@ Object.keys(schemes).forEach((schemeName) => {
320
330
expect ( gotBody ) . toBe ( false ) ;
321
331
322
332
// Tell the server that its request should finish.
323
- server . kill ( 'SIGUSR1' ) ;
333
+ killServerBarrier . resolve ( ) ;
324
334
325
335
const body = await bodyPromise ;
326
336
expect ( gotBody ) . toBe ( true ) ;
327
337
expect ( body ) . toBe ( 'helloworld' ) ;
328
338
329
- // Wait for subprocess to go away .
339
+ // Wait for server to close .
330
340
await a . event ( server , 'close' ) ;
331
341
} ) ;
332
342
}
0 commit comments