This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* MM-20664 Add client side ping pong * Make ping event after every 10secs * clear timer to not create ping events on new message * close conn if server does not respond with a pong in due time * lint fix * Update client/websocket_client.test.jsx Co-authored-by: Guillermo Vayá <[email protected]> * Clear ping and pong on close * Fix constant * Fix clear of pingpong * Change to use conn.onClose when closing connection Co-authored-by: Guillermo Vayá <[email protected]> Co-authored-by: Mattermod <[email protected]>
- Loading branch information
1 parent
d556bcd
commit 58bb453
Showing
3 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
import WebSocketClient from './websocket_client.jsx'; | ||
const FAKE_URL = 'ws:https://localhostt:9001'; | ||
|
||
describe('WebSocketClient', () => { | ||
test('should call for createPingEvent on conn open and clear existing ping pong timers', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.createPingEvent = jest.fn(); | ||
clientSocket.initialize(FAKE_URL); | ||
clientSocket.conn.onopen(); | ||
expect(clientSocket.createPingEvent).toHaveBeenCalled(); | ||
expect(clearTimeout).toHaveBeenCalledWith(clientSocket.pongTimer); | ||
expect(clearInterval).toHaveBeenCalledWith(clientSocket.pingTimer); | ||
}); | ||
|
||
test('should clear ping pong timers on close of connection', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.initialize(FAKE_URL); | ||
clientSocket.conn.onclose(); | ||
expect(clearTimeout).toHaveBeenCalledWith(clientSocket.pongTimer); | ||
expect(clearInterval).toHaveBeenCalledWith(clientSocket.pingTimer); | ||
}); | ||
|
||
test('should clear ping pong timers on error of connection', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.initialize(FAKE_URL); | ||
clientSocket.conn.onerror(); | ||
expect(clearTimeout).toHaveBeenCalledWith(clientSocket.pongTimer); | ||
expect(clearInterval).toHaveBeenCalledWith(clientSocket.pingTimer); | ||
}); | ||
|
||
test('should call for createPingEvent on onmessage and clear existing ping pong timers', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.createPingEvent = jest.fn(); | ||
clientSocket.initialize(FAKE_URL); | ||
clientSocket.conn.onmessage({data: '{}'}); | ||
expect(clientSocket.createPingEvent).toHaveBeenCalled(); | ||
expect(clearTimeout).toHaveBeenCalledWith(clientSocket.pongTimer); | ||
expect(clearInterval).toHaveBeenCalledWith(clientSocket.pingTimer); | ||
}); | ||
|
||
test('should call sendMessage on call of createPingEvent', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.sendMessage = jest.fn(); | ||
clientSocket.createPingEvent(); | ||
expect(clientSocket.sendMessage).not.toHaveBeenCalled(); | ||
jest.runTimersToTime(10000); | ||
expect(clientSocket.sendMessage).toHaveBeenCalledWith('ping'); | ||
clientSocket.clearPingPong(); | ||
}); | ||
|
||
test('should call conn.onclose if waitForPong is called and timer is not cleared', () => { | ||
const clientSocket = new WebSocketClient(); | ||
clientSocket.conn = { | ||
onclose: jest.fn(), | ||
}; | ||
|
||
clientSocket.waitForPong(); | ||
expect(clientSocket.conn.onclose).not.toHaveBeenCalled(); | ||
clientSocket.clearPingPong(); | ||
jest.runTimersToTime(2000); | ||
expect(clientSocket.conn.onclose).not.toHaveBeenCalled(); | ||
clientSocket.waitForPong(); | ||
jest.runTimersToTime(2000); | ||
expect(clientSocket.conn.onclose).toHaveBeenCalled(); | ||
}); | ||
}); |
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