Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: websocket connection of LiveQuery interrupts frequently #8048

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions spec/ParseWebSocketServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ 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);

// Make sure callback is called
expect(onConnectCallback).toHaveBeenCalled();
// Check that `waitingForPong` toggles
expect(ws.waitingForPong).toBe(false);
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
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 +124,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