-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from robtaussig/heartbeat_pr
Heartbeat pr
- Loading branch information
Showing
8 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { heartbeat } from "./heartbeat"; | ||
|
||
describe("heartbeat", () => { | ||
let ws: WebSocket; | ||
let sendSpy: jest.Mock; | ||
let closeSpy: jest.Mock; | ||
let addEventListenerSpy: jest.Mock; | ||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
sendSpy = jest.fn(); | ||
closeSpy = jest.fn(); | ||
addEventListenerSpy = jest.fn(); | ||
ws = { | ||
send: sendSpy, | ||
close: closeSpy, | ||
addEventListener: addEventListenerSpy, | ||
} as unknown as WebSocket; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("sends a ping message at the specified interval", () => { | ||
heartbeat(ws, { interval: 100 }); | ||
expect(sendSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(99); | ||
expect(sendSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(1); | ||
expect(sendSpy).toHaveBeenCalledTimes(1); | ||
jest.advanceTimersByTime(100); | ||
expect(sendSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test("closes the WebSocket if onMessageCb is not invoked within the specified timeout", () => { | ||
heartbeat(ws, { timeout: 100 }); | ||
expect(closeSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(99); | ||
expect(closeSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(1); | ||
expect(closeSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("does not close the WebSocket if messageCallback is invoked within the specified timeout", () => { | ||
const onMessageCb = heartbeat(ws, { timeout: 100 }); | ||
expect(closeSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(99); | ||
onMessageCb(); | ||
expect(closeSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(1); | ||
expect(closeSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("sends the custom ping message", () => { | ||
heartbeat(ws, { message: "pong" }); | ||
expect(sendSpy).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(25000); | ||
expect(sendSpy).toHaveBeenCalledWith("pong"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DEFAULT_HEARTBEAT } from "./constants"; | ||
import { HeartbeatOptions } from "./types"; | ||
|
||
export function heartbeat(ws: WebSocket, options?: HeartbeatOptions): () => void { | ||
const { | ||
interval = DEFAULT_HEARTBEAT.interval, | ||
timeout = DEFAULT_HEARTBEAT.timeout, | ||
message = DEFAULT_HEARTBEAT.message, | ||
} = options || {}; | ||
|
||
let messageAccepted = false; | ||
|
||
const pingTimer = setInterval(() => { | ||
try { | ||
ws.send(message); | ||
} catch (error) { | ||
// do nothing | ||
} | ||
}, interval); | ||
|
||
const timeoutTimer = setInterval(() => { | ||
if (!messageAccepted) { | ||
ws.close(); | ||
} else { | ||
messageAccepted = false; | ||
} | ||
}, timeout); | ||
|
||
ws.addEventListener("close", () => { | ||
clearInterval(pingTimer); | ||
clearInterval(timeoutTimer); | ||
}); | ||
|
||
return () => { | ||
messageAccepted = true; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters