Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Update integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
bobanm committed Nov 22, 2023
1 parent 53946ff commit e24a43c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions elements/lisk-api-client/src/ws_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { EventEmitter } from 'events';
import { JSONRPCMessage, EventCallback, Defer } from './types';
import { convertRPCError, defer, messageIsNotification, promiseWithTimeout } from './utils';

const CONNECTION_TIMEOUT = 5000;
const RESPONSE_TIMEOUT = 10000;
export const CONNECTION_TIMEOUT = 5000;
export const RESPONSE_TIMEOUT = 10000;

export class WSChannel {
public isAlive = false;
Expand Down
52 changes: 29 additions & 23 deletions elements/lisk-api-client/test/integration/ws_channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { createServer, Server } from 'http';
import * as WebSocket from 'isomorphic-ws';
import { Socket } from 'net';
import { WSChannel } from '../../src/ws_channel';
import { WSChannel, CONNECTION_TIMEOUT, RESPONSE_TIMEOUT } from '../../src/ws_channel';

jest.unmock('isomorphic-ws');

Expand Down Expand Up @@ -58,32 +58,38 @@ describe('WSChannel', () => {
expect.assertions(3);
});

it('should timeout if ws server not responding', async () => {
const http = createServer();
const server = new WebSocket.Server({ path: '/my-path', noServer: true });
it(
'should timeout if ws server not responding',
async () => {
const http = createServer();
const server = new WebSocket.Server({ path: '/my-path', noServer: true });

// https://github.com/websockets/ws/issues/377#issuecomment-462152231
http.on('upgrade', (request, socket, head) => {
setTimeout(() => {
server.handleUpgrade(request, socket as Socket, head, ws => {
server.emit('connection', ws, request);
});
}, 3000);
});
// https://github.com/websockets/ws/issues/377#issuecomment-462152231
http.on('upgrade', (request, socket, head) => {
setTimeout(() => {
server.handleUpgrade(request, socket as Socket, head, ws => {
server.emit('connection', ws, request);
});
}, CONNECTION_TIMEOUT + 1000);
});

http.listen(65535);
http.listen(65535);

const channel = new WSChannel('ws://127.0.0.1:65535/my-path');
const channel = new WSChannel('ws://127.0.0.1:65535/my-path');

try {
await expect(channel.connect()).rejects.toThrow('Could not connect in 2000ms');
expect(server.clients.size).toBe(0);
} finally {
await closeServer(http);
await closeServer(server);
}
expect.assertions(2);
}, 5000);
try {
await expect(channel.connect()).rejects.toThrow(
`Could not connect in ${CONNECTION_TIMEOUT}ms`,
);
expect(server.clients.size).toBe(0);
} finally {
await closeServer(http);
await closeServer(server);
}
expect.assertions(2);
},
RESPONSE_TIMEOUT,
);

it('should throw error if server is not running', async () => {
const channel = new WSChannel('ws://127.0.0.1:65534/my-path');
Expand Down

0 comments on commit e24a43c

Please sign in to comment.