-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Expand file tree
/
Copy pathindex.ts
More file actions
387 lines (307 loc) · 10.2 KB
/
index.ts
File metadata and controls
387 lines (307 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import expect = require("expect.js");
import { setupPrimary } from "..";
import { times, sleep } from "./util";
import cluster, { Worker } from "node:cluster";
const NODES_COUNT = 3;
cluster.setupMaster({
exec: "./test/worker.js",
// @ts-ignore
serialization: "advanced", // needed for packets containing buffers
});
setupPrimary();
const getRooms = (worker): Promise<Set<string>> => {
worker.send("get rooms");
return new Promise((resolve) => {
worker.once("message", (content) => {
resolve(content);
});
});
};
describe("@socket.io/cluster-adapter", () => {
let clientSockets: ClientSocket[], workers: Worker[];
beforeEach((done) => {
clientSockets = [];
workers = [];
for (let i = 1; i <= NODES_COUNT; i++) {
const PORT = 40000 + i;
const worker = cluster.fork({
PORT,
});
worker.on("listening", () => {
const clientSocket = ioc(`http://localhost:${PORT}`);
clientSocket.on("connect", async () => {
workers.push(worker);
clientSockets.push(clientSocket);
if (clientSockets.length === NODES_COUNT) {
done();
}
});
});
}
});
afterEach(() => {
for (const id in cluster.workers) {
cluster.workers[id].kill();
}
clientSockets.forEach((socket) => {
socket.disconnect();
});
});
describe("broadcast", function () {
it("broadcasts to all clients", (done) => {
const partialDone = times(3, done);
clientSockets.forEach((clientSocket) => {
clientSocket.on("test", (arg1, arg2, arg3) => {
expect(arg1).to.eql(1);
expect(arg2).to.eql("2");
expect(Buffer.isBuffer(arg3)).to.be(true);
partialDone();
});
});
workers[0].send("broadcasts to all clients");
});
it("broadcasts to all clients in a namespace", (done) => {
const partialDone = times(3, done);
const onConnect = times(3, async () => {
workers[0].send("broadcasts to all clients in a namespace");
});
clientSockets.forEach((clientSocket) => {
const socket = clientSocket.io.socket("/custom");
socket.on("connect", onConnect);
socket.on("test", () => {
socket.disconnect();
partialDone();
});
});
});
it("broadcasts to all clients in a room", (done) => {
workers[1].send("join room1");
clientSockets[0].on("test", () => {
done(new Error("should not happen"));
});
clientSockets[1].on("test", () => {
done();
});
clientSockets[2].on("test", () => {
done(new Error("should not happen"));
});
workers[0].send("broadcasts to all clients in a room");
});
it("broadcasts to all clients except in room", (done) => {
const partialDone = times(2, done);
workers[1].send("join room1");
clientSockets[0].on("test", () => {
partialDone();
});
clientSockets[1].on("test", () => {
done(new Error("should not happen"));
});
clientSockets[2].on("test", () => {
partialDone();
});
workers[0].send("broadcasts to all clients except in room");
});
it("broadcasts to local clients only", (done) => {
clientSockets[0].on("test", () => {
done();
});
clientSockets[1].on("test", () => {
done(new Error("should not happen"));
});
clientSockets[2].on("test", () => {
done(new Error("should not happen"));
});
workers[0].send("broadcasts to local clients only");
});
it("broadcasts with multiple acknowledgements", (done) => {
clientSockets[0].on("test", (cb) => {
cb(1);
});
clientSockets[1].on("test", (cb) => {
cb(2);
});
clientSockets[2].on("test", (cb) => {
cb(3);
});
workers[0].send("broadcasts with multiple acknowledgements");
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
it("broadcasts with multiple acknowledgements (binary content)", (done) => {
clientSockets[0].on("test", (cb) => {
cb(Buffer.from([1]));
});
clientSockets[1].on("test", (cb) => {
cb(Buffer.from([2]));
});
clientSockets[2].on("test", (cb) => {
cb(Buffer.from([3]));
});
workers[0].send(
"broadcasts with multiple acknowledgements (binary content)",
);
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
it("broadcasts with multiple acknowledgements (no client)", (done) => {
workers[0].send("broadcasts with multiple acknowledgements (no client)");
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
it("broadcasts with multiple acknowledgements (timeout)", (done) => {
clientSockets[0].on("test", (cb) => {
cb(1);
});
clientSockets[1].on("test", (cb) => {
cb(2);
});
clientSockets[2].on("test", (cb) => {
// do nothing
});
workers[0].send("broadcasts with multiple acknowledgements (timeout)");
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
});
describe("socketsJoin", () => {
it("makes all socket instances join the specified room", async () => {
workers[0].send("makes all socket instances join the specified room");
await sleep(100);
expect((await getRooms(workers[0])).has("room1")).to.be(true);
expect((await getRooms(workers[1])).has("room1")).to.be(true);
expect((await getRooms(workers[2])).has("room1")).to.be(true);
});
it("makes the matching socket instances join the specified room", async () => {
workers[0].send("join room1");
workers[2].send("join room1");
workers[0].send(
"makes the matching socket instances join the specified room",
);
await sleep(100);
expect((await getRooms(workers[0])).has("room2")).to.be(true);
expect((await getRooms(workers[1])).has("room2")).to.be(false);
expect((await getRooms(workers[2])).has("room2")).to.be(true);
});
it("avoids race condition when followed by emit (with await)", (done) => {
const partialDone = times(3, done);
clientSockets.forEach((clientSocket) => {
clientSocket.on("test-event", (payload) => {
expect(payload).to.eql("test-payload");
partialDone();
});
});
// This test verifies that awaiting socketsJoin ensures all sockets
// receive the subsequent broadcast
workers[0].send("test socketsJoin race condition with await");
});
});
describe("socketsLeave", () => {
it("makes all socket instances leave the specified room", async () => {
workers[0].send("join room1");
workers[2].send("join room1");
workers[0].send("makes all socket instances leave the specified room");
await sleep(100);
expect((await getRooms(workers[0])).has("room1")).to.be(false);
expect((await getRooms(workers[1])).has("room1")).to.be(false);
expect((await getRooms(workers[2])).has("room1")).to.be(false);
});
it("makes the matching socket instances leave the specified room", async () => {
workers[0].send("join room1 & room2");
workers[2].send("join room2");
workers[0].send(
"makes the matching socket instances leave the specified room",
);
await sleep(100);
expect((await getRooms(workers[0])).has("room2")).to.be(false);
expect((await getRooms(workers[1])).has("room2")).to.be(false);
expect((await getRooms(workers[2])).has("room2")).to.be(true);
});
});
describe("disconnectSockets", () => {
it("makes all socket instances disconnect", (done) => {
const partialDone = times(3, done);
clientSockets.forEach((clientSocket) => {
clientSocket.on("disconnect", (reason) => {
expect(reason).to.eql("io server disconnect");
partialDone();
});
});
workers[0].send("makes all socket instances disconnect");
});
});
describe("fetchSockets", () => {
it("returns all socket instances", (done) => {
workers[0].send("returns all socket instances");
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
});
describe("serverSideEmit", () => {
it("sends an event to other server instances", (done) => {
const partialDone = times(2, done);
workers[0].send("sends an event to other server instances");
workers[0].on("message", (result) => {
if (result === "ok") {
done(new Error("should not happen"));
}
});
workers[1].on("message", (result) => {
expect(result).to.eql("ok");
partialDone();
});
workers[2].on("message", (result) => {
expect(result).to.eql("ok");
partialDone();
});
});
it("sends an event and receives a response from the other server instances", (done) => {
workers[0].send(
"sends an event and receives a response from the other server instances (1)",
);
workers[1].send(
"sends an event and receives a response from the other server instances (2)",
);
workers[2].send(
"sends an event and receives a response from the other server instances (3)",
);
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
it("sends an event but timeout if one server does not respond", function (done) {
this.timeout(6000); // currently not possible to configure the timeout delay
workers[0].send(
"sends an event but timeout if one server does not respond (1)",
);
workers[1].send(
"sends an event but timeout if one server does not respond (2)",
);
workers[2].send(
"sends an event but timeout if one server does not respond (3)",
);
workers[0].on("message", (result) => {
if (result === "ok") {
done();
}
});
});
});
});