Skip to content

Commit be8fffb

Browse files
committed
refactor tests to avoid mocking when not necessary
1 parent b3d1fd4 commit be8fffb

File tree

1 file changed

+98
-96
lines changed

1 file changed

+98
-96
lines changed

packages/core/src/test/shared/utilities/processUtils.test.ts

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -384,133 +384,135 @@ function startSleepProcess(timeout: number = 90): RunningProcess {
384384

385385
describe('ChildProcessTracker', function () {
386386
let tracker: ChildProcessTracker
387-
let clock: FakeTimers.InstalledClock
388-
let usageMock: sinon.SinonStub
389387

390388
before(function () {
391-
clock = installFakeClock()
392389
tracker = ChildProcessTracker.instance
393-
usageMock = sinon.stub(ChildProcessTracker.prototype, 'getUsage')
394390
})
395391

396392
afterEach(function () {
397393
tracker.clear()
398-
usageMock.reset()
399394
})
400395

401-
after(function () {
402-
clock.uninstall()
403-
usageMock.restore()
404-
})
405-
406-
it(`removes stopped processes every ${ChildProcessTracker.pollingInterval / 1000} seconds`, async function () {
407-
// Start a 'sleep' command, check it only removes after we stop it.
408-
const runningProcess = startSleepProcess()
409-
tracker.add(runningProcess.childProcess)
410-
assert.strictEqual(tracker.has(runningProcess.childProcess), true, 'failed to add sleep command')
411-
412-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
413-
assert.strictEqual(tracker.has(runningProcess.childProcess), true, 'process was mistakenly removed')
414-
await stopAndWait(runningProcess)
415-
416-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
417-
assert.strictEqual(tracker.has(runningProcess.childProcess), false, 'process was not removed after stopping')
418-
})
396+
describe('tracking functionality', function () {
397+
let clock: FakeTimers.InstalledClock
398+
let usageMock: sinon.SinonStub
419399

420-
it('multiple processes from same command are tracked seperately', async function () {
421-
const runningProcess1 = startSleepProcess()
422-
const runningProcess2 = startSleepProcess()
423-
tracker.add(runningProcess1.childProcess)
424-
tracker.add(runningProcess2.childProcess)
400+
before(function () {
401+
clock = installFakeClock()
402+
tracker = ChildProcessTracker.instance
403+
usageMock = sinon.stub(ChildProcessTracker.prototype, 'getUsage')
404+
})
425405

426-
assert.strictEqual(tracker.has(runningProcess1.childProcess), true, 'Missing first process')
427-
assert.strictEqual(tracker.has(runningProcess2.childProcess), true, 'Missing second process')
406+
afterEach(function () {
407+
usageMock.reset()
408+
})
428409

429-
await stopAndWait(runningProcess1)
430-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
431-
assert.strictEqual(tracker.has(runningProcess2.childProcess), true, 'second process was mistakenly removed')
432-
assert.strictEqual(
433-
tracker.has(runningProcess1.childProcess),
434-
false,
435-
'first process was not removed after stopping it'
436-
)
437-
438-
await stopAndWait(runningProcess2)
439-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
440-
assert.strictEqual(
441-
tracker.has(runningProcess2.childProcess),
442-
false,
443-
'second process was not removed after stopping it'
444-
)
445-
446-
assert.strictEqual(tracker.size, 0, 'expected tracker to be empty')
447-
})
410+
after(function () {
411+
clock.uninstall()
412+
usageMock.restore()
413+
})
448414

449-
it('logs a warning message when system usage exceeds threshold', async function () {
450-
const runningProcess = startSleepProcess()
451-
tracker.add(runningProcess.childProcess)
415+
it(`removes stopped processes every ${ChildProcessTracker.pollingInterval / 1000} seconds`, async function () {
416+
// Start a 'sleep' command, check it only removes after we stop it.
417+
const runningProcess = startSleepProcess()
418+
tracker.add(runningProcess.childProcess)
419+
assert.strictEqual(tracker.has(runningProcess.childProcess), true, 'failed to add sleep command')
420+
421+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
422+
assert.strictEqual(tracker.has(runningProcess.childProcess), true, 'process was mistakenly removed')
423+
await stopAndWait(runningProcess)
424+
425+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
426+
assert.strictEqual(
427+
tracker.has(runningProcess.childProcess),
428+
false,
429+
'process was not removed after stopping'
430+
)
431+
})
452432

453-
const highCpu: ProcessStats = {
454-
cpu: ChildProcessTracker.thresholds.cpu + 1,
455-
memory: 0,
456-
}
457-
const highMemory: ProcessStats = {
458-
cpu: 0,
459-
memory: ChildProcessTracker.thresholds.memory + 1,
460-
}
433+
it('multiple processes from same command are tracked seperately', async function () {
434+
const runningProcess1 = startSleepProcess()
435+
const runningProcess2 = startSleepProcess()
436+
tracker.add(runningProcess1.childProcess)
437+
tracker.add(runningProcess2.childProcess)
438+
439+
assert.strictEqual(tracker.has(runningProcess1.childProcess), true, 'Missing first process')
440+
assert.strictEqual(tracker.has(runningProcess2.childProcess), true, 'Missing second process')
441+
442+
await stopAndWait(runningProcess1)
443+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
444+
assert.strictEqual(tracker.has(runningProcess2.childProcess), true, 'second process was mistakenly removed')
445+
assert.strictEqual(
446+
tracker.has(runningProcess1.childProcess),
447+
false,
448+
'first process was not removed after stopping it'
449+
)
450+
451+
await stopAndWait(runningProcess2)
452+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
453+
assert.strictEqual(
454+
tracker.has(runningProcess2.childProcess),
455+
false,
456+
'second process was not removed after stopping it'
457+
)
458+
459+
assert.strictEqual(tracker.size, 0, 'expected tracker to be empty')
460+
})
461461

462-
usageMock.resolves(highCpu)
462+
it('logs a warning message when system usage exceeds threshold', async function () {
463+
const runningProcess = startSleepProcess()
464+
tracker.add(runningProcess.childProcess)
463465

464-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
465-
assertLogsContain('exceeded cpu threshold', false, 'warn')
466+
const highCpu: ProcessStats = {
467+
cpu: ChildProcessTracker.thresholds.cpu + 1,
468+
memory: 0,
469+
}
470+
const highMemory: ProcessStats = {
471+
cpu: 0,
472+
memory: ChildProcessTracker.thresholds.memory + 1,
473+
}
466474

467-
usageMock.resolves(highMemory)
468-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
469-
assertLogsContain('exceeded memory threshold', false, 'warn')
475+
usageMock.resolves(highCpu)
470476

471-
await stopAndWait(runningProcess)
472-
})
477+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
478+
assertLogsContain('exceeded cpu threshold', false, 'warn')
473479

474-
it('includes pid in logs', async function () {
475-
const runningProcess = startSleepProcess()
476-
tracker.add(runningProcess.childProcess)
480+
usageMock.resolves(highMemory)
481+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
482+
assertLogsContain('exceeded memory threshold', false, 'warn')
477483

478-
usageMock.resolves({
479-
cpu: ChildProcessTracker.thresholds.cpu + 1,
480-
memory: 0,
484+
await stopAndWait(runningProcess)
481485
})
482486

483-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
484-
assertLogsContain(runningProcess.childProcess.pid().toString(), false, 'warn')
487+
it('includes pid in logs', async function () {
488+
const runningProcess = startSleepProcess()
489+
tracker.add(runningProcess.childProcess)
485490

486-
await stopAndWait(runningProcess)
487-
})
491+
usageMock.resolves({
492+
cpu: ChildProcessTracker.thresholds.cpu + 1,
493+
memory: 0,
494+
})
488495

489-
it('does not log for processes within threshold', async function () {
490-
const runningProcess = startSleepProcess()
496+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
497+
assertLogsContain(runningProcess.childProcess.pid().toString(), false, 'warn')
491498

492-
usageMock.resolves({
493-
cpu: ChildProcessTracker.thresholds.cpu - 1,
494-
memory: ChildProcessTracker.thresholds.memory - 1,
499+
await stopAndWait(runningProcess)
495500
})
496501

497-
await clock.tickAsync(ChildProcessTracker.pollingInterval)
498-
499-
assert.throws(() => assertLogsContain(runningProcess.childProcess.pid().toString(), false, 'warn'))
502+
it('does not log for processes within threshold', async function () {
503+
const runningProcess = startSleepProcess()
500504

501-
await stopAndWait(runningProcess)
502-
})
503-
})
505+
usageMock.resolves({
506+
cpu: ChildProcessTracker.thresholds.cpu - 1,
507+
memory: ChildProcessTracker.thresholds.memory - 1,
508+
})
504509

505-
describe('ChildProcessTracker.logAllUsage', function () {
506-
let tracker: ChildProcessTracker
510+
await clock.tickAsync(ChildProcessTracker.pollingInterval)
507511

508-
before(function () {
509-
tracker = ChildProcessTracker.instance
510-
})
512+
assert.throws(() => assertLogsContain(runningProcess.childProcess.pid().toString(), false, 'warn'))
511513

512-
afterEach(function () {
513-
tracker.clear()
514+
await stopAndWait(runningProcess)
515+
})
514516
})
515517

516518
it('logAllUsage includes only active processes', async function () {

0 commit comments

Comments
 (0)