index.spec.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, expect, it } from "@jest/globals";
  2. import { Client } from "../../../src/models/client.ts";
  3. import { Realm } from "../../../src/models/realm.ts";
  4. import { CheckBrokenConnections } from "../../../src/services/checkBrokenConnections/index.ts";
  5. import { wait } from "../../utils.ts";
  6. describe("CheckBrokenConnections", () => {
  7. it("should remove client after 2 checks", async () => {
  8. const realm = new Realm();
  9. const doubleCheckTime = 55; //~ equals to checkBrokenConnections.checkInterval * 2
  10. const checkBrokenConnections = new CheckBrokenConnections({
  11. realm,
  12. config: { alive_timeout: doubleCheckTime },
  13. checkInterval: 30,
  14. });
  15. const client = new Client({ id: "id", token: "" });
  16. realm.setClient(client, "id");
  17. checkBrokenConnections.start();
  18. await wait(checkBrokenConnections.checkInterval * 2 + 30);
  19. expect(realm.getClientById("id")).toBeUndefined();
  20. checkBrokenConnections.stop();
  21. });
  22. it("should remove client after 1 ping", async () => {
  23. const realm = new Realm();
  24. const doubleCheckTime = 55; //~ equals to checkBrokenConnections.checkInterval * 2
  25. const checkBrokenConnections = new CheckBrokenConnections({
  26. realm,
  27. config: { alive_timeout: doubleCheckTime },
  28. checkInterval: 30,
  29. });
  30. const client = new Client({ id: "id", token: "" });
  31. realm.setClient(client, "id");
  32. checkBrokenConnections.start();
  33. //set ping after first check
  34. await wait(checkBrokenConnections.checkInterval);
  35. client.setLastPing(new Date().getTime());
  36. await wait(checkBrokenConnections.checkInterval * 2 + 10);
  37. expect(realm.getClientById("id")).toBeUndefined();
  38. checkBrokenConnections.stop();
  39. });
  40. });