add basic ws connectivity tests

This commit is contained in:
tiagosiebler
2022-09-15 14:11:17 +01:00
parent 3f5039ef8b
commit 1b422a1beb
7 changed files with 435 additions and 31 deletions

View File

@@ -0,0 +1,75 @@
import {
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
logAllEvents,
promiseSleep,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Private Inverse Perps Websocket Client', () => {
let wsClient: WebsocketClient;
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
});
const wsClientOptions: WSClientConfigurableOptions = {
market: 'inverse',
key: API_KEY,
secret: API_SECRET,
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPrivate();
});
afterAll(() => {
// await promiseSleep(2000);
wsClient.closeAll();
});
it('should open a ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.inverse,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to private wallet events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'wallet';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
// No easy way to trigger a private event (other than executing trades)
// expect(wsUpdatePromise).resolves.toMatchObject({
// topic: wsTopic,
// data: expect.any(Array),
// });
wsClient.subscribe(wsTopic);
await Promise.all([wsResponsePromise]);
});
});

View File

@@ -0,0 +1,75 @@
import {
LinearClient,
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
promiseSleep,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Public Inverse Perps Websocket Client', () => {
let wsClient: WebsocketClient;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'inverse',
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPublic();
});
afterAll(() => {
wsClient.closeAll();
});
it('should open a private ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.inverse,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to public orderBookL2_25 events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'orderBookL2_25.BTCUSD';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
expect(wsUpdatePromise).resolves.toMatchObject({
topic: wsTopic,
data: expect.any(Array),
});
wsClient.subscribe(wsTopic);
try {
await wsResponsePromise;
} catch (e) {
console.error(
`Wait for "${wsTopic}" subscription response exception: `,
e
);
}
try {
await wsUpdatePromise;
} catch (e) {
console.error(`Wait for "${wsTopic}" event exception: `, e);
}
});
});

View File

@@ -0,0 +1,73 @@
import {
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
promiseSleep,
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Private Linear Websocket Client', () => {
let wsClient: WebsocketClient;
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
it('should have api credentials to test with', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
});
const wsClientOptions: WSClientConfigurableOptions = {
market: 'linear',
key: API_KEY,
secret: API_SECRET,
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPrivate();
});
afterAll(() => {
wsClient.closeAll();
});
it('should open a ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.linearPrivate,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to private wallet events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'wallet';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
// No easy way to trigger a private event (other than executing trades)
// expect(wsUpdatePromise).resolves.toMatchObject({
// topic: wsTopic,
// data: expect.any(Array),
// });
wsClient.subscribe(wsTopic);
await Promise.all([wsResponsePromise]);
});
});

View File

@@ -0,0 +1,76 @@
import {
LinearClient,
WebsocketClient,
WSClientConfigurableOptions,
WS_KEY_MAP,
} from '../../src';
import {
silentLogger,
waitForSocketEvent,
WS_OPEN_EVENT_PARTIAL,
} from '../ws.util';
describe('Public Linear Websocket Client', () => {
let wsClient: WebsocketClient;
const wsClientOptions: WSClientConfigurableOptions = {
market: 'linear',
};
beforeAll(() => {
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
wsClient.connectPublic();
});
afterAll(() => {
wsClient.closeAll();
});
it('should open a private ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({
event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.linearPublic,
});
await Promise.all([wsOpenPromise]);
});
it('should subscribe to public orderBookL2_25 events', async () => {
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'orderBookL2_25.BTCUSDT';
expect(wsResponsePromise).resolves.toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: true,
});
expect(wsUpdatePromise).resolves.toMatchObject({
topic: wsTopic,
data: {
order_book: expect.any(Array),
},
});
wsClient.subscribe(wsTopic);
try {
await wsResponsePromise;
} catch (e) {
console.error(
`Wait for "${wsTopic}" subscription response exception: `,
e
);
}
try {
await wsUpdatePromise;
} catch (e) {
console.error(`Wait for "${wsTopic}" event exception: `, e);
}
});
});

82
test/ws.util.ts Normal file
View File

@@ -0,0 +1,82 @@
import { WebsocketClient, WsClientEvent } from '../src';
export const silentLogger = {
silly: () => {},
debug: () => {},
notice: () => {},
info: () => {},
warning: () => {},
error: () => {},
};
export const WS_OPEN_EVENT_PARTIAL = {
type: 'open',
};
/** Resolves a promise if an event is seen before a timeout (defaults to 2.5 seconds) */
export function waitForSocketEvent(
wsClient: WebsocketClient,
event: WsClientEvent,
timeoutMs: number = 4.5 * 1000
) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(
`Failed to receive "${event}" event before timeout. Check that these are correct: topic, api keys (if private), signature process (if private)`
);
}, timeoutMs);
let resolvedOnce = false;
wsClient.on(event, (event) => {
clearTimeout(timeout);
resolve(event);
resolvedOnce = true;
});
wsClient.on('error', (event) => {
clearTimeout(timeout);
if (!resolvedOnce) {
reject(event);
}
});
// if (event !== 'close') {
// wsClient.on('close', (event) => {
// clearTimeout(timeout);
// if (!resolvedOnce) {
// reject(event);
// }
// });
// }
});
}
export function logAllEvents(wsClient: WebsocketClient) {
wsClient.on('update', (data) => {
console.log('wsUpdate: ', JSON.stringify(data, null, 2));
});
wsClient.on('open', (data) => {
console.log('wsOpen: ', data.wsKey);
});
wsClient.on('response', (data) => {
console.log('wsResponse ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnect', ({ wsKey }) => {
console.log('wsReconnecting ', wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('wsReconnected ', data?.wsKey);
});
wsClient.on('close', (data) => {
// console.log('wsClose: ', data);
});
}
export function promiseSleep(ms: number) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}