Skip to content

Commit

Permalink
fix(clean up): prevent sandbox creation after dispose (#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Apr 26, 2019
1 parent 3e21a8a commit 73fc0a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/SandboxPool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as os from 'os';
import { range, Subject, Observable } from 'rxjs';
import { flatMap, tap, zip, merge, map } from 'rxjs/operators';
import { flatMap, tap, zip, merge, map, filter } from 'rxjs/operators';
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { TestFramework } from '@stryker-mutator/api/test_framework';
import Sandbox from './Sandbox';
Expand Down Expand Up @@ -60,9 +60,15 @@ export class SandboxPool implements Disposable {
const concurrency = this.determineConcurrency();

return range(0, concurrency).pipe(
flatMap(n => {
return this.registerSandbox(Sandbox.create(this.options, n, this.initialFiles, this.testFramework, this.overheadTimeMS, this.loggingContext));
}, MAX_CONCURRENT_INITIALIZING_SANDBOXES)
flatMap(async n => {
if (this.isDisposed) {
return null;
} else {
return this.registerSandbox(Sandbox.create(this.options, n, this.initialFiles, this.testFramework, this.overheadTimeMS, this.loggingContext));
}
}, MAX_CONCURRENT_INITIALIZING_SANDBOXES),
filter(sandboxOrNull => !!sandboxOrNull),
map(sandbox => sandbox as Sandbox)
);
}

Expand All @@ -89,7 +95,9 @@ export class SandboxPool implements Disposable {
return promisedSandbox;
}

private isDisposed = false;
public async dispose() {
this.isDisposed = true;
const sandboxes = await Promise.all(this.allSandboxes);
return Promise.all(sandboxes.map(sandbox => sandbox.dispose()));
}
Expand Down
28 changes: 28 additions & 0 deletions packages/core/test/unit/SandboxPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe(SandboxPool.name, () => {
await expect(actRunMutants()).rejectedWith(expectedError);
});
});

describe('dispose', () => {
it('should have disposed all sandboxes', async () => {
sut = createSut();
Expand Down Expand Up @@ -216,5 +217,32 @@ describe(SandboxPool.name, () => {
await disposePromise;
expect(secondSandbox.dispose).called;
});

it('should halt creating of new sandboxes', async () => {
// Arrange
sut = createSut();
sinon.stub(os, 'cpus').returns([1, 2, 3]); // stub 3 cpus
const task = new Task<Sandbox>();
const task2 = new Task<Sandbox>();
createStub.reset();
createStub
.onCall(0).returns(task.promise)
.onCall(1).returns(task2.promise)
.onCall(2).resolves(genericSandboxForAllSubsequentCallsToNewSandbox); // promise is not yet resolved
inputMutants.push(transpiledMutant(), transpiledMutant()); // 3 mutants

// Act
const runPromise = sut.runMutants(from(inputMutants))
.pipe(toArray())
.toPromise();
const disposePromise = sut.dispose();
task.resolve(firstSandbox as unknown as Sandbox);
task2.resolve(secondSandbox as unknown as Sandbox);
await disposePromise;
await runPromise;

// Assert
expect(createStub).calledTwice;
});
});
});

0 comments on commit 73fc0a8

Please sign in to comment.