|
| 1 | +import clientmanager from "../../clientmanager"; |
| 2 | +import { |
| 3 | + BalancerConnection, |
| 4 | + BalancerConnectionEventHandlers, |
| 5 | + BalancerConnectionEvents, |
| 6 | + BalancerConnectionReal, |
| 7 | + MsgM2B, |
| 8 | + balancerManager, |
| 9 | +} from "../../balancer"; |
| 10 | +import { BalancerClient, Client } from "../../client"; |
| 11 | +import { OttWebsocketError } from "common/models/types"; |
| 12 | +import { buildClients } from "../../redisclient"; |
| 13 | +import { Result, ok } from "../../../common/result"; |
| 14 | +import roommanager from "../../roommanager"; |
| 15 | +import { loadModels } from "../../models"; |
| 16 | + |
| 17 | +class TestClient extends Client { |
| 18 | + sendRawMock = jest.fn(); |
| 19 | + kickMock = jest.fn(); |
| 20 | + |
| 21 | + sendRaw(msg: string): void { |
| 22 | + this.sendRawMock(msg); |
| 23 | + } |
| 24 | + |
| 25 | + kick(code: OttWebsocketError): void { |
| 26 | + this.kickMock(code); |
| 27 | + this.emit("disconnect", this); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class BalancerConnectionMock extends BalancerConnection { |
| 32 | + sendMock = jest.fn(); |
| 33 | + |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + } |
| 37 | + |
| 38 | + send(msg: MsgM2B): Result<void, Error> { |
| 39 | + this.sendMock(msg); |
| 40 | + return ok(undefined); |
| 41 | + } |
| 42 | + |
| 43 | + public emit<E extends BalancerConnectionEvents>( |
| 44 | + event: E, |
| 45 | + ...args: Parameters<BalancerConnectionEventHandlers<E>> |
| 46 | + ) { |
| 47 | + super.emit(event, ...args); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +describe("ClientManager", () => { |
| 52 | + beforeAll(async () => { |
| 53 | + loadModels(); |
| 54 | + await buildClients(); |
| 55 | + await clientmanager.setup(); |
| 56 | + }); |
| 57 | + |
| 58 | + beforeEach(async () => { |
| 59 | + await roommanager.createRoom({ |
| 60 | + name: "foo", |
| 61 | + isTemporary: true, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(async () => { |
| 66 | + await roommanager.unloadRoom("foo"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should add clients", () => { |
| 70 | + const client = new TestClient("foo"); |
| 71 | + clientmanager.addClient(client); |
| 72 | + expect(clientmanager.getClient(client.id)).toBe(client); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should add clients to roomJoins when they auth", async () => { |
| 76 | + const client = new TestClient("foo"); |
| 77 | + clientmanager.addClient(client); |
| 78 | + client.emit("auth", client, "token", { isLoggedIn: false, username: "foo" }); |
| 79 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 80 | + const joins = clientmanager.getClientsInRoom("foo"); |
| 81 | + expect(joins).toHaveLength(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should remove clients when they disconnect", () => { |
| 85 | + const client = new TestClient("foo"); |
| 86 | + clientmanager.addClient(client); |
| 87 | + client.emit("disconnect", client); |
| 88 | + expect(clientmanager.getClient(client.id)).toBeUndefined(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should remove clients from roomJoins when they disconnect", async () => { |
| 92 | + const client = new TestClient("foo"); |
| 93 | + clientmanager.addClient(client); |
| 94 | + client.emit("auth", client, "token", { isLoggedIn: false, username: "foo" }); |
| 95 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 96 | + const joins = clientmanager.getClientsInRoom("foo"); |
| 97 | + expect(joins).toHaveLength(1); |
| 98 | + |
| 99 | + client.emit("disconnect", client); |
| 100 | + const joins2 = clientmanager.getClientsInRoom("foo"); |
| 101 | + expect(joins2).toHaveLength(0); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should disconnect all clients when a balancer disconnects", async () => { |
| 105 | + const mockBalancerCon = new BalancerConnectionMock(); |
| 106 | + balancerManager.addBalancerConnection(mockBalancerCon); |
| 107 | + const client = new BalancerClient("foo", "foo", mockBalancerCon); |
| 108 | + clientmanager.addClient(client); |
| 109 | + client.emit("auth", client, "token", { isLoggedIn: false, username: "foo" }); |
| 110 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 111 | + const joins = clientmanager.getClientsInRoom("foo"); |
| 112 | + expect(joins).toHaveLength(1); |
| 113 | + |
| 114 | + mockBalancerCon.emit("disconnect", 1000, "reason"); |
| 115 | + |
| 116 | + expect(clientmanager.getClient(client.id)).toBeUndefined(); |
| 117 | + const joins2 = clientmanager.getClientsInRoom("foo"); |
| 118 | + expect(joins2).toHaveLength(0); |
| 119 | + }); |
| 120 | +}); |
0 commit comments