|
1 | | -import { createServer } from "http"; |
2 | | -import { Server, Socket as ServerSocket } from "socket.io"; |
3 | | -import { io as ioc, Socket as ClientSocket } from "socket.io-client"; |
4 | | -import expect = require("expect.js"); |
5 | | -import { createAdapter } from "../lib"; |
6 | | -import type { AddressInfo } from "net"; |
7 | | -import { times, sleep, shouldNotHappen } from "./util"; |
8 | | -import { Pool } from "pg"; |
9 | | - |
10 | | -const NODES_COUNT = 3; |
| 1 | +import { type Server, type Socket as ServerSocket } from "socket.io"; |
| 2 | +import { type Socket as ClientSocket } from "socket.io-client"; |
| 3 | +import expect from "expect.js"; |
| 4 | +import { setup, times, sleep, shouldNotHappen } from "./util"; |
11 | 5 |
|
12 | 6 | describe("@socket.io/postgres-adapter", () => { |
13 | 7 | let servers: Server[], |
14 | 8 | serverSockets: ServerSocket[], |
15 | 9 | clientSockets: ClientSocket[], |
16 | | - pool: Pool; |
17 | | - |
18 | | - beforeEach((done) => { |
19 | | - servers = []; |
20 | | - serverSockets = []; |
21 | | - clientSockets = []; |
22 | | - pool = new Pool({ |
23 | | - user: "postgres", |
24 | | - host: "localhost", |
25 | | - database: "postgres", |
26 | | - password: "changeit", |
27 | | - port: 5432, |
28 | | - }); |
29 | | - |
30 | | - pool.query( |
31 | | - ` |
32 | | - CREATE TABLE IF NOT EXISTS events ( |
33 | | - id bigserial UNIQUE, |
34 | | - created_at timestamptz DEFAULT NOW(), |
35 | | - payload bytea |
36 | | - ); |
37 | | - `, |
38 | | - () => {} |
39 | | - ); |
40 | | - |
41 | | - for (let i = 1; i <= NODES_COUNT; i++) { |
42 | | - const httpServer = createServer(); |
43 | | - const io = new Server(httpServer); |
44 | | - io.adapter( |
45 | | - createAdapter(pool, { |
46 | | - tableName: "events", |
47 | | - }) |
48 | | - ); |
49 | | - httpServer.listen(() => { |
50 | | - const port = (httpServer.address() as AddressInfo).port; |
51 | | - const clientSocket = ioc(`http://localhost:${port}`); |
52 | | - |
53 | | - io.on("connection", async (socket) => { |
54 | | - clientSockets.push(clientSocket); |
55 | | - serverSockets.push(socket); |
56 | | - servers.push(io); |
57 | | - if (servers.length === NODES_COUNT) { |
58 | | - await sleep(200); |
59 | | - |
60 | | - // ensure all nodes know each other |
61 | | - servers[0].emit("ping"); |
62 | | - servers[1].emit("ping"); |
63 | | - servers[2].emit("ping"); |
64 | | - |
65 | | - await sleep(200); |
66 | | - |
67 | | - done(); |
68 | | - } |
69 | | - }); |
70 | | - }); |
71 | | - } |
| 10 | + cleanup: () => void; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + const testContext = await setup(); |
| 14 | + servers = testContext.servers; |
| 15 | + serverSockets = testContext.serverSockets; |
| 16 | + clientSockets = testContext.clientSockets; |
| 17 | + cleanup = testContext.cleanup; |
72 | 18 | }); |
73 | 19 |
|
74 | | - afterEach((done) => { |
75 | | - servers.forEach((server) => server.close()); |
76 | | - clientSockets.forEach((socket) => socket.disconnect()); |
77 | | - pool.end(done); |
78 | | - }); |
| 20 | + afterEach(() => cleanup()); |
79 | 21 |
|
80 | 22 | describe("broadcast", function () { |
81 | 23 | it("broadcasts to all clients", (done) => { |
|
0 commit comments