Skip to content

Commit

Permalink
fix: websocket connection of LiveQuery interrupts frequently (#8048)
Browse files Browse the repository at this point in the history
  • Loading branch information
LMBernardo authored Jun 17, 2022
1 parent 72fac8a commit 03caae1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions spec/ParseWebSocketServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ describe('ParseWebSocketServer', function () {
expect(wssError).toBe('Invalid Packet');
});

it('can handle ping/pong', async () => {
const onConnectCallback = jasmine.createSpy('onConnectCallback');
const http = require('http');
const server = http.createServer();
const parseWebSocketServer = new ParseWebSocketServer(server, onConnectCallback, {
websocketTimeout: 10,
}).server;

const ws = new EventEmitter();
ws.readyState = 0;
ws.OPEN = 0;
ws.ping = jasmine.createSpy('ping');
ws.terminate = jasmine.createSpy('terminate');

parseWebSocketServer.onConnection(ws);

expect(onConnectCallback).toHaveBeenCalled();
expect(ws.waitingForPong).toBe(false);
await new Promise(resolve => setTimeout(resolve, 10));
expect(ws.ping).toHaveBeenCalled();
expect(ws.waitingForPong).toBe(true);
ws.emit('pong');
expect(ws.waitingForPong).toBe(false);
await new Promise(resolve => setTimeout(resolve, 10));
expect(ws.waitingForPong).toBe(true);
expect(ws.terminate).not.toHaveBeenCalled();
server.close();
});

it('closes interrupted connection', async () => {
const onConnectCallback = jasmine.createSpy('onConnectCallback');
const http = require('http');
Expand All @@ -93,8 +122,10 @@ describe('ParseWebSocketServer', function () {

// Make sure callback is called
expect(onConnectCallback).toHaveBeenCalled();
expect(ws.waitingForPong).toBe(false);
await new Promise(resolve => setTimeout(resolve, 10));
expect(ws.ping).toHaveBeenCalled();
expect(ws.waitingForPong).toBe(true);
await new Promise(resolve => setTimeout(resolve, 10));
expect(ws.terminate).toHaveBeenCalled();
server.close();
Expand Down
2 changes: 1 addition & 1 deletion src/LiveQuery/ParseWebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ParseWebSocketServer {
wss.onConnection = ws => {
ws.waitingForPong = false;
ws.on('pong', () => {
this.waitingForPong = false;
ws.waitingForPong = false;
});
ws.on('error', error => {
logger.error(error.message);
Expand Down

0 comments on commit 03caae1

Please sign in to comment.