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: Webchat socket instantiation delay #2179

Merged
merged 6 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added unit test to make sure backedup messages are cleared before con…
…nection starts

Signed-off-by: Srinaath Ravichandran <srravich@microsoft.com>
  • Loading branch information
Srinaath Ravichandran committed Aug 21, 2020
commit a88377cbeca2de7aa9f221941006d9cbcf09e760
81 changes: 81 additions & 0 deletions packages/app/main/src/server/webSocketServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import { WebSocketServer } from './webSocketServer';
import { Activity } from 'botframework-schema';

const mockWSServer = {
handleUpgrade: jest.fn(),
Expand Down Expand Up @@ -157,4 +158,84 @@ describe('WebSocketServer', () => {
expect(Object.keys((WebSocketServer as any)._servers)).toEqual([mockConversationId]);
expect(mockWSServer.handleUpgrade).toHaveBeenCalledTimes(1);
});

it('should clear the messages backed up before websocket connection is started', async () => {
let >
let websocketHandler = null;

(WebSocketServer as any)._restServer = undefined;
(WebSocketServer as any)._servers = {};
(WebSocketServer as any)._sockets = {};

mockWSServer.on.mockImplementation((event, implementation) => {
if (event === 'connection') {
>
}
});

mockCreateServer.mockReturnValueOnce({
address: () => ({ port: 55523 }),
get: (route, handler) => {
websocketHandler = handler;
},
listen: jest.fn((_port, cb) => {
cb();
}),
once: jest.fn(),
});
await WebSocketServer.init();

WebSocketServer.queueActivities('conv-123', { id: 'activity-1' } as Activity);
WebSocketServer.queueActivities('conv-234', { id: 'activity-1' } as Activity);

WebSocketServer.queueActivities('conv-123', { id: 'activity-2' } as Activity);
WebSocketServer.queueActivities('conv-234', { id: 'activity-2' } as Activity);
websocketHandler(
{
params: {
conversationId: 'conv-234',
},
},
{
claimUpgrade: jest.fn(() => {
return {
head: jest.fn(),
socket: jest.fn(),
};
}),
}
);
const socketSendMock = jest.fn();
onConnectionFunction({
send: socketSendMock,
on: jest.fn(),
});
expect(socketSendMock).toHaveBeenCalledTimes(2);
expect(socketSendMock).toHaveBeenNthCalledWith(1, JSON.stringify({ activities: [{ id: 'activity-1' }] }));
expect(socketSendMock).toHaveBeenNthCalledWith(2, JSON.stringify({ activities: [{ id: 'activity-2' }] }));
socketSendMock.mockClear();

websocketHandler(
{
params: {
conversationId: 'conv-123',
},
},
{
claimUpgrade: jest.fn(() => {
return {
head: jest.fn(),
socket: jest.fn(),
};
}),
}
);
onConnectionFunction({
send: socketSendMock,
on: jest.fn(),
});
expect(socketSendMock).toHaveBeenCalledTimes(2);
expect(socketSendMock).toHaveBeenNthCalledWith(1, JSON.stringify({ activities: [{ id: 'activity-1' }] }));
expect(socketSendMock).toHaveBeenNthCalledWith(2, JSON.stringify({ activities: [{ id: 'activity-2' }] }));
});
});
2 changes: 1 addition & 1 deletion packages/app/main/src/server/webSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class WebSocketServer {
const wsServer = new WSServer({
noServer: true,
});
wsServer.on('connection', async (socket, req) => {
wsServer.on('connection', (socket, req) => {
this.sendBackedUpMessages(conversationId, socket);
this._sockets[conversationId] = socket;

Expand Down