Skip to content

Commit fb770bc

Browse files
fix test
1 parent 778a7d3 commit fb770bc

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

test/fixtures/primary.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as pg from "pg";
44
import { setupPrimary } from "../../lib/";
55
import { createAdapter } from "@socket.io/cluster-adapter";
66
import { Server } from "socket.io";
7+
import { sleep } from "../util.ts";
78

89
const WORKERS_COUNT = 3;
910
const EXPECTED_PEERS_COUNT = 3 * WORKERS_COUNT - 1;
@@ -39,14 +40,26 @@ if (cluster.isPrimary) {
3940
cb(process.pid);
4041
});
4142

42-
setTimeout(() => {
43+
function isInitComplete() {
44+
return io.of("/").adapter.nodesMap.size === EXPECTED_PEERS_COUNT;
45+
}
46+
47+
async function runTest() {
48+
io.of("/").adapter.init();
49+
50+
while (!isInitComplete()) {
51+
await sleep(20);
52+
}
53+
4354
io.serverSideEmit("test", (err, res) => {
4455
assert.equal(err, null);
4556
assert.equal(res.length, EXPECTED_PEERS_COUNT);
4657

4758
setTimeout(() => {
48-
process.exit(0);
59+
process.exit(0); // exit after having responded to other nodes
4960
}, GRACE_PERIOD_IN_MS);
5061
});
51-
}, GRACE_PERIOD_IN_MS);
62+
}
63+
64+
runTest();
5265
}

0 commit comments

Comments
 (0)