@@ -366,6 +366,12 @@ async function stopAndWait(cp: ChildProcess, clock: FakeTimers.InstalledClock) {
366366 await clock . tickAsync ( ChildProcess . stopTimeout )
367367}
368368
369+ function startSleepProcess ( timeout : number = 90 ) {
370+ const childProcess = new ChildProcess ( getSleepCmd ( ) , [ timeout . toString ( ) ] )
371+ childProcess . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
372+ return childProcess
373+ }
374+
369375describe ( 'ChildProcessTracker' , function ( ) {
370376 let tracker : ChildProcessTracker
371377 let clock : FakeTimers . InstalledClock
@@ -388,8 +394,7 @@ describe('ChildProcessTracker', function () {
388394
389395 it ( `removes stopped processes every ${ ChildProcessTracker . pollingInterval / 1000 } seconds` , async function ( ) {
390396 // Start a 'sleep' command, check it only removes after we stop it.
391- const childProcess = new ChildProcess ( getSleepCmd ( ) , [ '90' ] )
392- childProcess . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
397+ const childProcess = startSleepProcess ( )
393398 tracker . add ( childProcess )
394399 assert . strictEqual ( tracker . has ( childProcess ) , true , 'failed to add sleep command' )
395400
@@ -402,10 +407,8 @@ describe('ChildProcessTracker', function () {
402407 } )
403408 for ( const _ of Array . from ( { length : 100 } ) ) {
404409 it ( 'multiple processes from same command are tracked seperately' , async function ( ) {
405- const childProcess1 = new ChildProcess ( getSleepCmd ( ) , [ '90' ] )
406- const childProcess2 = new ChildProcess ( getSleepCmd ( ) , [ '90' ] )
407- childProcess1 . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
408- childProcess2 . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
410+ const childProcess1 = startSleepProcess ( )
411+ const childProcess2 = startSleepProcess ( )
409412 tracker . add ( childProcess1 )
410413 tracker . add ( childProcess2 )
411414
@@ -420,8 +423,7 @@ describe('ChildProcessTracker', function () {
420423 }
421424
422425 it ( 'logs a warning message when system usage exceeds threshold' , async function ( ) {
423- const childProcess = new ChildProcess ( getSleepCmd ( ) , [ '90' ] )
424- childProcess . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
426+ const childProcess = startSleepProcess ( )
425427 tracker . add ( childProcess )
426428
427429 const highCpu : ProcessStats = {
@@ -444,14 +446,12 @@ describe('ChildProcessTracker', function () {
444446 } )
445447
446448 it ( 'includes pid in logs' , async function ( ) {
447- const childProcess = new ChildProcess ( getSleepCmd ( ) , [ '90' ] )
448- childProcess . run ( ) . catch ( ( ) => assert . fail ( 'sleep command threw an error' ) )
449+ const childProcess = startSleepProcess ( )
449450 tracker . add ( childProcess )
450451
451452 usageMock . resolves ( {
452453 cpu : ChildProcessTracker . thresholds . cpu + 1 ,
453454 memory : 0 ,
454- elapsed : 0 ,
455455 } )
456456
457457 await clock . tickAsync ( ChildProcessTracker . pollingInterval )
0 commit comments