Skip to content

Commit 0f37d02

Browse files
committed
add/improve cluster unit tests
1 parent c726df4 commit 0f37d02

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/packages/backend/conat/test/cluster/cluster-sticky.test.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ describe("create cluster of two nodes, and verify that *sticky* subs properly wo
1616
({ server: server1, client: client1 } = await createClusterNode({
1717
clusterName: "cluster0",
1818
id: "1",
19-
systemAccountPassword: "squeamish",
19+
autoscanInterval: 100,
20+
longAutoscanInterval: 1000,
2021
}));
2122
client1b = server1.client();
2223
({ server: server2, client: client2 } = await createClusterNode({
2324
clusterName: "cluster0",
2425
id: "2",
25-
systemAccountPassword: "ossifrage",
26+
autoscanInterval: 100,
27+
longAutoscanInterval: 1000,
2628
}));
2729
await server1.join(server2.address());
2830
await server2.join(server1.address());
@@ -60,11 +62,11 @@ describe("create cluster of two nodes, and verify that *sticky* subs properly wo
6062

6163
it("send messages and note they all go to the same target -- next the hard case across the cluster", async () => {
6264
// NOTE: if we do this test without waiting for consistent state, it will definitely
63-
// fail sometimes, since server2 literally doesn't know enough about the servers yet,
65+
// fail sometimes, since server2 literally doesn't know enough about the servers yet,
6466
// so has to make a different choice. Services of course must account for the fact that
6567
// for the first moments of their existence, sticky routing can't work.
6668
await waitForConsistentState([server1, server2]);
67-
69+
6870
await client2.waitForInterest(subject);
6971
for (let i = 0; i < count; i++) {
7072
await client2.publish(subject, "hi");
@@ -73,6 +75,45 @@ describe("create cluster of two nodes, and verify that *sticky* subs properly wo
7375
expect(recv1 + recv1b).toEqual(2 * count);
7476
expect(recv1 * recv1b).toEqual(0);
7577
});
78+
79+
let server3, client3;
80+
it("add a third node", async () => {
81+
const { client, server } = await createClusterNode({
82+
clusterName: "cluster0",
83+
autoscanInterval: 100,
84+
longAutoscanInterval: 5000,
85+
id: "3",
86+
});
87+
server3 = server;
88+
client3 = client;
89+
await server1.join(server.address());
90+
});
91+
92+
it("waits for consistent state -- this verifies, e.g., that sticky state is equal", async () => {
93+
await waitForConsistentState([server1, server2, server3]);
94+
});
95+
96+
it("client connected to server3 also routes properly", async () => {
97+
const total = recv1 + recv1b;
98+
await client3.waitForInterest(subject);
99+
for (let i = 0; i < count; i++) {
100+
await client3.publish(subject, "hi");
101+
}
102+
await wait({ until: () => recv1 + recv1b >= total + count });
103+
expect(recv1 + recv1b).toEqual(total + count);
104+
expect(recv1 * recv1b).toEqual(0);
105+
});
106+
107+
it("client1 and client2 still route properly", async () => {
108+
const total = recv1 + recv1b;
109+
for (let i = 0; i < count; i++) {
110+
await client1.publish(subject, "hi");
111+
await client2.publish(subject, "hi");
112+
}
113+
await wait({ until: () => recv1 + recv1b >= total + 2 * count });
114+
expect(recv1 + recv1b).toEqual(total + 2 * count);
115+
expect(recv1 * recv1b).toEqual(0);
116+
});
76117
});
77118

78119
afterAll(after);

src/packages/backend/conat/test/setup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ export async function waitForConsistentState(
191191
// @ts-ignore
192192
const link = servers[j].clusterLinks[clusterName]?.[servers[i].id];
193193
if (link == null) {
194-
throw Error(`node ${j} is not connected to node ${i}`);
194+
if (Date.now() - start > 3000) {
195+
console.log(`node ${j} is not connected to node ${i}`);
196+
}
197+
return false;
195198
}
196199
const hashLink = link.hash();
197200
const x = link.interest.serialize().patterns;

0 commit comments

Comments
 (0)