|
1 | | -import { ClientRequest } from "http"; |
2 | | - |
3 | 1 | import { setConnectionTimeout } from "./set-connection-timeout"; |
4 | 2 |
|
5 | 3 | describe("setConnectionTimeout", () => { |
| 4 | + const reject = jest.fn(); |
6 | 5 | const clientRequest: any = { |
7 | 6 | on: jest.fn(), |
| 7 | + destroy: jest.fn(), |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | beforeEach(() => { |
11 | | - clientRequest.on.mockClear(); |
| 11 | + jest.clearAllMocks(); |
12 | 12 | }); |
13 | 13 |
|
14 | 14 | it("will not attach listeners if timeout is 0", () => { |
15 | | - setConnectionTimeout(<ClientRequest>clientRequest, jest.fn(), 0); |
16 | | - |
17 | | - expect(clientRequest.on.mock.calls.length).toBe(0); |
| 15 | + setConnectionTimeout(clientRequest, reject, 0); |
| 16 | + expect(clientRequest.on).not.toHaveBeenCalled(); |
18 | 17 | }); |
19 | 18 |
|
20 | 19 | it("will not attach listeners if timeout is not provided", () => { |
21 | | - setConnectionTimeout(<ClientRequest>clientRequest, jest.fn()); |
22 | | - |
23 | | - expect(clientRequest.on.mock.calls.length).toBe(0); |
| 20 | + setConnectionTimeout(clientRequest, reject); |
| 21 | + expect(clientRequest.on).not.toHaveBeenCalled(); |
24 | 22 | }); |
25 | 23 |
|
26 | | - it("will attach listeners if timeout is provided", () => { |
27 | | - setConnectionTimeout(<ClientRequest>clientRequest, jest.fn(), 100); |
| 24 | + describe("when timeout is provided", () => { |
| 25 | + const timeoutInMs = 100; |
| 26 | + const mockSocket = { |
| 27 | + connecting: true, |
| 28 | + on: jest.fn(), |
| 29 | + }; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + jest.useFakeTimers(); |
| 33 | + setConnectionTimeout(clientRequest, reject, timeoutInMs); |
| 34 | + }); |
| 35 | + |
| 36 | + it("attaches listener", () => { |
| 37 | + expect(clientRequest.on).toHaveBeenCalledTimes(1); |
| 38 | + expect(clientRequest.on).toHaveBeenCalledWith("socket", expect.any(Function)); |
| 39 | + }); |
| 40 | + |
| 41 | + it("doesn't set timeout if socket is already connected", () => { |
| 42 | + clientRequest.on.mock.calls[0][1]({ |
| 43 | + ...mockSocket, |
| 44 | + connecting: false, |
| 45 | + }); |
| 46 | + expect(mockSocket.on).not.toHaveBeenCalled(); |
| 47 | + expect(setTimeout).not.toHaveBeenCalled(); |
| 48 | + expect(reject).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("rejects and aborts request if socket isn't connected by timeout", () => { |
| 52 | + clientRequest.on.mock.calls[0][1](mockSocket); |
| 53 | + expect(setTimeout).toHaveBeenCalledTimes(1); |
| 54 | + expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timeoutInMs); |
| 55 | + expect(mockSocket.on).toHaveBeenCalledTimes(1); |
| 56 | + expect(mockSocket.on).toHaveBeenCalledWith("connect", expect.any(Function)); |
| 57 | + |
| 58 | + expect(clientRequest.destroy).not.toHaveBeenCalled(); |
| 59 | + expect(reject).not.toHaveBeenCalled(); |
| 60 | + |
| 61 | + // Fast-forward until timer has been executed. |
| 62 | + jest.advanceTimersByTime(timeoutInMs); |
| 63 | + expect(clientRequest.destroy).toHaveBeenCalledTimes(1); |
| 64 | + expect(reject).toHaveBeenCalledTimes(1); |
| 65 | + expect(reject).toHaveBeenCalledWith( |
| 66 | + Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), { |
| 67 | + name: "TimeoutError", |
| 68 | + }) |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("clears timeout if socket gets connected", () => { |
| 73 | + const mockTimeoutId = 42; |
| 74 | + ((setTimeout as unknown) as jest.Mock).mockReturnValueOnce(mockTimeoutId); |
| 75 | + clientRequest.on.mock.calls[0][1](mockSocket); |
| 76 | + |
| 77 | + expect(clientRequest.destroy).not.toHaveBeenCalled(); |
| 78 | + expect(reject).not.toHaveBeenCalled(); |
| 79 | + expect(clearTimeout).not.toHaveBeenCalled(); |
| 80 | + |
| 81 | + // Fast-forward for half the amount of time and call connect callback to clear timer. |
| 82 | + jest.advanceTimersByTime(timeoutInMs / 2); |
| 83 | + mockSocket.on.mock.calls[0][1](); |
| 84 | + |
| 85 | + expect(clearTimeout).toHaveBeenCalled(); |
| 86 | + expect(clearTimeout).toHaveBeenCalledWith(mockTimeoutId); |
28 | 87 |
|
29 | | - expect(clientRequest.on.mock.calls.length).toBe(1); |
30 | | - expect(clientRequest.on.mock.calls[0][0]).toBe("socket"); |
| 88 | + // Fast-forward until timer has been executed. |
| 89 | + jest.runAllTimers(); |
| 90 | + expect(clientRequest.destroy).not.toHaveBeenCalled(); |
| 91 | + expect(reject).not.toHaveBeenCalled(); |
| 92 | + }); |
31 | 93 | }); |
32 | 94 | }); |
0 commit comments