Skip to content

Commit

Permalink
test(cluster-engine): fix flaky test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jul 26, 2024
1 parent b79d80a commit 582655f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/socket.io-cluster-engine/test/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import expect = require("expect.js");
import { handshake, url } from "./util";
import { setupPrimary } from "../lib";

const WORKER_COUNT = 3;

cluster.setupPrimary({
exec: "./test/worker.js",
// @ts-expect-error
Expand All @@ -13,7 +15,7 @@ setupPrimary();

describe("cluster", () => {
beforeEach((done) => {
for (let i = 0; i < 3; i++) {
for (let i = 0; i < WORKER_COUNT; i++) {
const worker = cluster.fork();

if (i === 2) {
Expand All @@ -23,16 +25,18 @@ describe("cluster", () => {
});

afterEach((done) => {
for (const worker of Object.values(cluster.workers)) {
worker.kill();
}
let i = 0;
function onExit() {
if (Object.keys(cluster.workers).length === 0) {
if (++i === WORKER_COUNT) {
cluster.off("exit", onExit);
done();
}
}
cluster.on("exit", onExit);

for (const worker of Object.values(cluster.workers)) {
worker.kill();
}
});

it("should ping/pong", (done) => {
Expand Down

0 comments on commit 582655f

Please sign in to comment.