@@ -32,6 +32,7 @@ const a: any = require('awaiting');
3232const request : any = require ( 'requisition' ) ;
3333import fs from 'fs' ;
3434import { Stopper } from '../../../plugin/drainHttpServer/stoppable' ;
35+ import child from 'child_process' ;
3536import path from 'path' ;
3637import type { AddressInfo } from 'net' ;
3738import { describe , it , expect , afterEach , beforeEach } from '@jest/globals' ;
@@ -135,9 +136,7 @@ Object.keys(schemes).forEach((schemeName) => {
135136 ) ,
136137 ) ;
137138 expect ( err . code ) . toMatch ( / E C O N N R E F U S E D / ) ;
138-
139- const isNode20 = ! ! process . version . match ( / ^ v 2 0 \. / ) ;
140- expect ( closed ) . toBe ( isNode20 ? 1 : 0 ) ;
139+ expect ( closed ) . toBe ( 0 ) ;
141140 } ) ;
142141 } ) ;
143142
@@ -302,21 +301,12 @@ Object.keys(schemes).forEach((schemeName) => {
302301
303302 if ( schemeName === 'http' ) {
304303 it ( 'with in-flights finishing before grace period ends' , async ( ) => {
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 (
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 (
320310 scheme . agent ( { keepAlive : true } ) ,
321311 ) ;
322312 let gotBody = false ;
@@ -330,13 +320,13 @@ Object.keys(schemes).forEach((schemeName) => {
330320 expect ( gotBody ) . toBe ( false ) ;
331321
332322 // Tell the server that its request should finish.
333- killServerBarrier . resolve ( ) ;
323+ server . kill ( 'SIGUSR1' ) ;
334324
335325 const body = await bodyPromise ;
336326 expect ( gotBody ) . toBe ( true ) ;
337327 expect ( body ) . toBe ( 'helloworld' ) ;
338328
339- // Wait for server to close .
329+ // Wait for subprocess to go away .
340330 await a . event ( server , 'close' ) ;
341331 } ) ;
342332 }
0 commit comments