Skip to content

Commit

Permalink
fix: stop REPL servers on call "halt" method
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Sep 19, 2024
1 parent 41ac023 commit a527042
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import PromiseGroup from "./promise-group";
import { TestCollection } from "../test-collection";
import * as logger from "../utils/logger";
import { Config } from "../config";
import type { runTest } from "../worker";
import type { runTest, cancel } from "../worker";
import type { Stats as RunnerStats } from "../stats";
import EventEmitter from "events";
import { Test } from "../types";

interface WorkerMethods {
runTest: typeof runTest;
cancel: typeof cancel;
}

export interface Workers extends EventEmitter, WorkerMethods {}
Expand Down Expand Up @@ -73,7 +74,7 @@ export class MainRunner extends Runner {
}

this.workersRegistry.init();
this.workers = this.workersRegistry.register(require.resolve("../worker"), ["runTest"]) as Workers;
this.workers = this.workersRegistry.register(require.resolve("../worker"), ["runTest", "cancel"]) as Workers;
this.browserPool = pool.create(this.config, this);
}

Expand Down Expand Up @@ -173,6 +174,8 @@ export class MainRunner extends Runner {
this.browserPool?.cancel();

this.activeBrowserRunners.forEach(runner => runner.cancel());

this.workers?.cancel();
}

registerWorkers<T extends ReadonlyArray<string>>(workerFilepath: string, exportedMethods: T): RegisterWorkers<T> {
Expand Down
4 changes: 4 additions & 0 deletions src/worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ testplaneFacade.init();
exports.runTest = (fullTitle, options) => {
return testplaneFacade.runTest(fullTitle, options);
};

exports.cancel = () => {
return testplaneFacade.cancel();
};
4 changes: 4 additions & 0 deletions src/worker/testplane-facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = class TestplaneFacade {
return this.promise;
}

cancel(): void {
RuntimeConfig.getInstance().replServer?.close();
}

syncConfig(): Promise<void> {
this.syncConfig = (): Promise<void> => this.promise;

Expand Down
12 changes: 12 additions & 0 deletions test/src/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("NodejsEnvRunner", () => {
const mkWorkers_ = () => {
return {
runTest: sandbox.stub().resolves(),
cancel: sandbox.stub().resolves(),
};
};

Expand Down Expand Up @@ -694,5 +695,16 @@ describe("NodejsEnvRunner", () => {
assert.notCalled(BrowserRunner.prototype.run);
assert.notCalled(BrowserRunner.prototype.cancel);
});

it("should cancel all executing workers", async () => {
const workers = mkWorkers_();
WorkersRegistry.prototype.register.withArgs(sinon.match.string, ["runTest", "cancel"]).returns(workers);
const runner = new Runner(makeConfigStub());

runner.init();
runner.cancel();

assert.calledOnceWithExactly(workers.cancel);
});
});
});
25 changes: 25 additions & 0 deletions test/src/worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,29 @@ describe("worker", () => {
return assert.isRejected(runTest("fullTitle", { some: "opts" }), /foo/);
});
});

describe("cancel", () => {
let cancel;

beforeEach(() => {
TestplaneFacade.prototype.cancel.returns();

const worker = require("src/worker");
cancel = worker.cancel;
});

it("should delegate cancel call to testplane facade", () => {
TestplaneFacade.prototype.cancel.returns();

cancel();

assert.calledOnceWithExactly(TestplaneFacade.prototype.cancel);
});

it("should throws on testplane facade cancel fail", () => {
TestplaneFacade.prototype.cancel.throws(new Error("o.O"));

assert.throws(() => cancel(), Error, "o.O");
});
});
});
20 changes: 20 additions & 0 deletions test/src/worker/testplane-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const proxyquire = require("proxyquire");
const { AsyncEmitter } = require("src/events/async-emitter");
const { Testplane } = require("src/worker/testplane");
const RuntimeConfig = require("src/config/runtime-config");
const { makeConfigStub } = require("../../utils");
const ipc = require("src/utils/ipc");
const TestplaneFacade = require("src/worker/testplane-facade");
Expand Down Expand Up @@ -79,4 +80,23 @@ describe("worker/testplane-facade", () => {
assert.callOrder(TestplaneFacade.prototype.syncConfig, testplane.runTest);
});
});

describe("cancel", () => {
beforeEach(() => {
sandbox.stub(RuntimeConfig, "getInstance").returns({});
});

it("should not throw if repl server is not exists in runtime config", () => {
assert.doesNotThrow(() => testplaneFacade.cancel());
});

it("should close repl server if it exists in runtime config", () => {
const replServer = { close: sandbox.stub() };
RuntimeConfig.getInstance.returns({ replServer });

testplaneFacade.cancel();

assert.calledOnceWithExactly(replServer.close);
});
});
});

0 comments on commit a527042

Please sign in to comment.