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

test: remove sleep from UNSUBSCRIBE_BLOCK e2e test #258

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions test/e2e/tests/subscribe-block.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getWebSocketClient } from '../utils/setup-websocket-client.js';
const fixtures = await getFixtures('subscribe-block');

describe('subscribe-block', () => {

for (const fixture of fixtures) {
test(fixture.testName, async () => {
const ws = getWebSocketClient();
Expand All @@ -16,7 +15,7 @@ describe('subscribe-block', () => {
'subscribed': true,
});

const messages = await ws.waitForSubscriptionMessages(2,160_000);
const messages = await ws.waitForSubscriptionMessages(2, 300_000);

expect(messages[0].data).toMatchObject(fixture.subscribe_message_schema);
expect(messages[1].data).toMatchObject(fixture.subscribe_message_schema);
Expand Down
50 changes: 36 additions & 14 deletions test/e2e/tests/unsubscribe-block.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import { describe, test } from 'vitest';
import { afterAll, beforeAll, describe, test } from 'vitest';
import { expect } from 'vitest';
import { sleep } from '../utils/sleep.js';
import { getWebSocketClient } from '../utils/setup-websocket-client.js';

import { WebsocketClientE2e } from '../utils/websocket-client-e2e.js';

describe('unsubscribe-block', () => {
// alternative ws client used to be aware when server emits a new block
let ws2: WebsocketClientE2e;

beforeAll(async () => {
ws2 = new WebsocketClientE2e('ws://localhost:3005');

// init the alternative client
await ws2.waitForConnection();
});

afterAll(() => {
if (ws2) ws2.close();
});

test('UNSUBSCRIBE_BLOCK - success', async () => {
const ws = getWebSocketClient();

await ws.sendAndWait('SUBSCRIBE_BLOCK');
ws.clearSubscriptionMessages();
const response = await ws.sendAndWait('UNSUBSCRIBE_BLOCK');
expect(response.data).toMatchObject({
'subscribed': false,
});
await sleep(100_000);
expect(ws.getSubscriptionMessages().length).equals(0);
test('UNSUBSCRIBE_BLOCK - success', async () => {
// standard e2e ws client
const ws1 = getWebSocketClient();

// subscribe both the clients
await Promise.all([ws1.sendAndWait('SUBSCRIBE_BLOCK'), ws2.sendAndWait('SUBSCRIBE_BLOCK')]);

// immediately unsubscribe the standard e2e ws client
const response = await ws1.sendAndWait('UNSUBSCRIBE_BLOCK');
expect(response.data).toMatchObject({
'subscribed': false,
});

ws1.clearSubscriptionMessages();
ws2.clearSubscriptionMessages();

// wait for a new block
await ws2.waitForSubscriptionMessages(1, 160_000);

// ensure the alternative client got the new block while the standard one didn't
expect(ws1.getSubscriptionMessages().length).equals(0);
expect(ws2.getSubscriptionMessages().length).equals(1);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you ws2.close(); to tear down additional ws clinet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

309f4ef should solve this

});
Loading