@@ -366,6 +366,12 @@ async function stopAndWait(cp: ChildProcess, clock: FakeTimers.InstalledClock) {
366
366
await clock . tickAsync ( ChildProcess . stopTimeout )
367
367
}
368
368
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
+
369
375
describe ( 'ChildProcessTracker' , function ( ) {
370
376
let tracker : ChildProcessTracker
371
377
let clock : FakeTimers . InstalledClock
@@ -388,8 +394,7 @@ describe('ChildProcessTracker', function () {
388
394
389
395
it ( `removes stopped processes every ${ ChildProcessTracker . pollingInterval / 1000 } seconds` , async function ( ) {
390
396
// 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 ( )
393
398
tracker . add ( childProcess )
394
399
assert . strictEqual ( tracker . has ( childProcess ) , true , 'failed to add sleep command' )
395
400
@@ -402,10 +407,8 @@ describe('ChildProcessTracker', function () {
402
407
} )
403
408
for ( const _ of Array . from ( { length : 100 } ) ) {
404
409
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 ( )
409
412
tracker . add ( childProcess1 )
410
413
tracker . add ( childProcess2 )
411
414
@@ -420,8 +423,7 @@ describe('ChildProcessTracker', function () {
420
423
}
421
424
422
425
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 ( )
425
427
tracker . add ( childProcess )
426
428
427
429
const highCpu : ProcessStats = {
@@ -444,14 +446,12 @@ describe('ChildProcessTracker', function () {
444
446
} )
445
447
446
448
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 ( )
449
450
tracker . add ( childProcess )
450
451
451
452
usageMock . resolves ( {
452
453
cpu : ChildProcessTracker . thresholds . cpu + 1 ,
453
454
memory : 0 ,
454
- elapsed : 0 ,
455
455
} )
456
456
457
457
await clock . tickAsync ( ChildProcessTracker . pollingInterval )
0 commit comments