cleaning for ws tests

This commit is contained in:
tiagosiebler
2022-09-16 15:09:39 +01:00
parent f4a569dcba
commit 1c3707b6d2
12 changed files with 90 additions and 93 deletions

View File

@@ -68,12 +68,10 @@ describe('Private Inverse Perps Websocket Client', () => {
it('should open a ws connection', async () => { it('should open a ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.inverse, wsKey: WS_KEY_MAP.inverse,
}); });
await Promise.all([wsOpenPromise]);
}); });
it('should subscribe to private wallet events', async () => { it('should subscribe to private wallet events', async () => {

View File

@@ -30,12 +30,14 @@ describe('Public Inverse Perps Websocket Client', () => {
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.inverse, wsKey: WS_KEY_MAP.inverse,
}); });
} catch (e) {
await Promise.all([wsOpenPromise]); expect(e).toBeFalsy();
}
}); });
it('should subscribe to public orderBookL2_25 events', async () => { it('should subscribe to public orderBookL2_25 events', async () => {

View File

@@ -71,12 +71,14 @@ describe('Private Linear Perps Websocket Client', () => {
it('should open a ws connection', async () => { it('should open a ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.linearPrivate, wsKey: WS_KEY_MAP.linearPrivate,
}); });
} catch (e) {
await Promise.all([wsOpenPromise]); expect(e).toBeFalsy();
}
}); });
it('should subscribe to private wallet events', async () => { it('should subscribe to private wallet events', async () => {

View File

@@ -23,17 +23,16 @@ describe('Public Linear Perps Websocket Client', () => {
afterAll(() => { afterAll(() => {
wsClient.closeAll(); wsClient.closeAll();
wsClient.removeAllListeners();
}); });
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.linearPublic, wsKey: WS_KEY_MAP.linearPublic,
}); });
await Promise.all([wsOpenPromise]);
}); });
it('should subscribe to public orderBookL2_25 events', async () => { it('should subscribe to public orderBookL2_25 events', async () => {
@@ -41,33 +40,27 @@ describe('Public Linear Perps Websocket Client', () => {
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'orderBookL2_25.BTCUSDT'; const wsTopic = 'orderBookL2_25.BTCUSDT';
expect(wsResponsePromise).resolves.toMatchObject({ wsClient.subscribe(wsTopic);
try {
expect(await wsResponsePromise).toMatchObject({
request: { request: {
args: [wsTopic], args: [wsTopic],
op: 'subscribe', op: 'subscribe',
}, },
success: true, success: true,
}); });
expect(wsUpdatePromise).resolves.toMatchObject({ } catch (e) {
expect(e).toBeFalsy();
}
try {
expect(await wsUpdatePromise).toMatchObject({
topic: wsTopic, topic: wsTopic,
data: { data: {
order_book: expect.any(Array), 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) { } catch (e) {
console.error(`Wait for "${wsTopic}" event exception: `, e); console.error(`Wait for "${wsTopic}" event exception: `, e);
} }
@@ -78,13 +71,6 @@ describe('Public Linear Perps Websocket Client', () => {
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); // const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
const wsTopic = 'wallet'; 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) // No easy way to trigger a private event (other than executing trades)
// expect(wsUpdatePromise).resolves.toMatchObject({ // expect(wsUpdatePromise).resolves.toMatchObject({
@@ -95,9 +81,15 @@ describe('Public Linear Perps Websocket Client', () => {
wsClient.subscribe(wsTopic); wsClient.subscribe(wsTopic);
try { try {
await Promise.all([wsResponsePromise]); expect(await wsResponsePromise).toBeFalsy();
} catch (e) { } catch (e) {
// expect(e).toMatchObject({
request: {
args: [wsTopic],
op: 'subscribe',
},
success: false,
});
} }
}); });
}); });

View File

@@ -46,15 +46,20 @@ describe('Private Spot V1 Websocket Client', () => {
wsClient.connectPrivate(); wsClient.connectPrivate();
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.spotPrivate, // wsKey: WS_KEY_MAP.spotPrivate,
// also opens public conn automatically, which can confuse the test
}); });
} catch (e) {
expect(e).toBeFalsy();
}
// expect(wsUpdatePromise).resolves.toMatchObject({ // expect(wsUpdatePromise).resolves.toMatchObject({
// topic: 'wsTopic', // topic: 'wsTopic',
// data: expect.any(Array), // data: expect.any(Array),
// }); // });
await Promise.all([wsOpenPromise]);
// await Promise.all([wsUpdatePromise]); // await Promise.all([wsUpdatePromise]);
// await promiseSleep(4000); // await promiseSleep(4000);
}); });

View File

@@ -79,13 +79,11 @@ describe('Private Spot V3 Websocket Client', () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.spotV3Private, wsKey: WS_KEY_MAP.spotV3Private,
}); });
try {
await Promise.all([wsOpenPromise]);
} catch (e) { } catch (e) {
expect(e).toBeFalsy(); expect(e).toBeFalsy();
} }

View File

@@ -34,12 +34,14 @@ describe('Public Spot V1 Websocket Client', () => {
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.spotPublic, wsKey: WS_KEY_MAP.spotPublic,
}); });
} catch (e) {
await Promise.all([wsOpenPromise]); expect(e).toBeFalsy();
}
}); });
it('should subscribe to public orderbook events', async () => { it('should subscribe to public orderbook events', async () => {

View File

@@ -34,12 +34,14 @@ describe('Public Spot V3 Websocket Client', () => {
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.spotV3Public, wsKey: WS_KEY_MAP.spotV3Public,
}); });
} catch (e) {
await Promise.all([wsOpenPromise]); expect(e).toBeFalsy();
}
}); });
it('should subscribe to public orderbook events', async () => { it('should subscribe to public orderbook events', async () => {

View File

@@ -77,7 +77,6 @@ describe('Private USDC Option Websocket Client', () => {
wsClientOptions, wsClientOptions,
getSilentLogger('expectSuccess') getSilentLogger('expectSuccess')
); );
wsClient.connectPrivate();
// logAllEvents(wsClient); // logAllEvents(wsClient);
}); });
@@ -86,16 +85,15 @@ describe('Private USDC Option Websocket Client', () => {
}); });
it('should open a private ws connection', async () => { it('should open a private ws connection', async () => {
wsClient.connectPrivate();
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.usdcOptionPrivate, wsKey: WS_KEY_MAP.usdcOptionPrivate,
}); });
try {
await Promise.all([wsOpenPromise]);
} catch (e) { } catch (e) {
expect(e).toBeFalsy(); expect(e).toBeFalsy();
} }
@@ -113,6 +111,7 @@ describe('Private USDC Option Websocket Client', () => {
}); });
it(`should subscribe to private "${wsTopic}" events`, async () => { it(`should subscribe to private "${wsTopic}" events`, async () => {
wsClient.connectPrivate();
const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');

View File

@@ -31,13 +31,14 @@ describe('Public USDC Option Websocket Client', () => {
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
try {
expect(wsOpenPromise).resolves.toMatchObject({ expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.usdcOptionPublic, wsKey: WS_KEY_MAP.usdcOptionPublic,
}); });
} catch (e) {
await Promise.all([wsOpenPromise]); expect(e).toBeFalsy();
}
}); });
it('should subscribe to public trade events', async () => { it('should subscribe to public trade events', async () => {

View File

@@ -89,13 +89,11 @@ describe('Private USDC Perp Websocket Client', () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
expect(wsOpenPromise).resolves.toMatchObject({ try {
expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.usdcPerpPrivate, wsKey: WS_KEY_MAP.usdcPerpPrivate,
}); });
try {
await Promise.all([wsOpenPromise]);
} catch (e) { } catch (e) {
expect(e).toBeFalsy(); expect(e).toBeFalsy();
} }

View File

@@ -33,12 +33,10 @@ describe('Public USDC Perp Websocket Client', () => {
it('should open a public ws connection', async () => { it('should open a public ws connection', async () => {
const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
expect(wsOpenPromise).resolves.toMatchObject({ expect(await wsOpenPromise).toMatchObject({
event: WS_OPEN_EVENT_PARTIAL, event: WS_OPEN_EVENT_PARTIAL,
wsKey: WS_KEY_MAP.usdcPerpPublic, wsKey: WS_KEY_MAP.usdcPerpPublic,
}); });
await Promise.all([wsOpenPromise]);
}); });
it('should subscribe to public trade events', async () => { it('should subscribe to public trade events', async () => {