aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/socket.io/socket.io-socket-timeout.test.ts
blob: c69d80e28ebdb2dbccbc915f314c4aa60f35e7d9 (plain) (blame)
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
import { Server } from "socket.io";
import { describe, it, expect } from "bun:test";

import { success, fail, createClient } from "./support/util.ts";

describe("timeout", () => {
  it("should timeout if the client does not acknowledge the event", done => {
    const io = new Server(0);
    const client = createClient(io, "/");
    try {
      const timeout = setTimeout(() => {
        fail(done, io, new Error("timeout"), client);
      }, 200);

      io.on("connection", socket => {
        socket.timeout(50).emit("unknown", err => {
          clearTimeout(timeout);
          try {
            expect(err).toBeInstanceOf(Error);
            success(done, io, client);
          } catch (err) {
            fail(done, io, err, client);
          }
        });
      });
    } catch (err) {
      fail(done, io, err, client);
    }
  });

  it("should timeout if the client does not acknowledge the event in time", done => {
    const io = new Server(0);
    const client = createClient(io, "/");
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), client);
    }, 500);

    client.on("echo", (arg, cb) => {
      cb(arg);
    });

    let count = 0;

    io.on("connection", socket => {
      socket.timeout(0).emit("echo", 42, err => {
        try {
          expect(err).toBeInstanceOf(Error);
          count++;
        } catch (err) {
          clearTimeout(timeout);
          fail(done, io, err, client);
        }
      });
    });

    setTimeout(() => {
      clearTimeout(timeout);
      try {
        expect(count).toBe(1);
        success(done, io, client);
      } catch (err) {
        fail(done, io, err, client);
      }
    }, 200);
  });

  it("should not timeout if the client does acknowledge the event", done => {
    const io = new Server(0);
    const client = createClient(io, "/");
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), client);
    }, 200);

    client.on("echo", (arg, cb) => {
      cb(arg);
    });

    io.on("connection", socket => {
      socket.timeout(50).emit("echo", 42, (err, value) => {
        clearTimeout(timeout);
        try {
          expect(err).toBe(null);
          expect(value).toBe(42);
          success(done, io, client);
        } catch (err) {
          fail(done, io, err, client);
        }
      });
    });
  });

  it("should timeout if the client does not acknowledge the event (promise)", done => {
    const io = new Server(0);
    const client = createClient(io, "/");
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), client);
    }, 200);

    io.on("connection", async socket => {
      try {
        await socket.timeout(50).emitWithAck("unknown");
        clearTimeout(timeout);
        fail(done, io, new Error("timeout"), client);
      } catch (err) {
        clearTimeout(timeout);
        expect(err).toBeInstanceOf(Error);
        success(done, io, client);
      }
    });
  });

  it("should not timeout if the client does acknowledge the event (promise)", done => {
    const io = new Server(0);
    const client = createClient(io, "/");
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), client);
    }, 200);

    client.on("echo", (arg, cb) => {
      cb(arg);
    });

    io.on("connection", async socket => {
      try {
        const value = await socket.timeout(50).emitWithAck("echo", 42);
        clearTimeout(timeout);
        expect(value).toBe(42);
        success(done, io, client);
      } catch (err) {
        clearTimeout(timeout);
        fail(done, io, err, client);
      }
    });
  });
});