Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop REPL servers on call "halt" method #1019

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't stop repl server from master process. So I need to send command to all workers.

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();
Copy link
Member Author

@DudaGod DudaGod Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If replServer doesn't exist (user do not use --repl option) then nothing will fail here.

}

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);
});
});
});
Loading