-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathconnection-timeout.test.ts
More file actions
58 lines (46 loc) · 1.64 KB
/
connection-timeout.test.ts
File metadata and controls
58 lines (46 loc) · 1.64 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
import { describe, test, expect } from "vitest";
import { SmeeServer } from "./helpers/smee-server.ts";
import { VoidLogger } from "./helpers/void-logger.ts";
import { WebhookServer } from "./helpers/webhook-server.ts";
import SmeeClient from "../index.ts";
describe("connection timeout", () => {
test("should throw a connection timeout", async () => {
const webhookServer = new WebhookServer({
handler: async (req, res) => {
await new Promise((resolve) => setTimeout(resolve, 2000));
res.writeHead(200).end("OK");
},
});
await webhookServer.start();
const smeeServer = new SmeeServer();
await smeeServer.start();
const logger = new VoidLogger();
const smeeClient = new SmeeClient({
source: webhookServer.url,
target: webhookServer.url,
maxConnectionTimeout: 500,
logger,
});
expect(smeeClient.onopen).toBe(null);
expect(logger.infoCalls.length).toBe(0);
try {
await smeeClient.start();
throw new Error("Expected start to throw due to timeout");
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe(
`Connection to ${webhookServer.url} timed out after 500ms`,
);
}
await new Promise((resolve) => setTimeout(resolve, 100));
expect(logger.errorCalls.length).toBe(1);
expect(logger.errorCalls[0][0]).toBe(
`Connection to ${webhookServer.url} timed out after 500ms`,
);
expect(logger.infoCalls.length).toBe(1);
expect(logger.infoCalls[0][0]).toBe(`Connection closed`);
await smeeClient.stop();
await smeeServer.stop();
await webhookServer.stop();
});
});