Skip to content

Commit

Permalink
Add test cases for subscribing and unsubscribing broadcast events
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 committed Aug 22, 2024
1 parent bc8b66c commit b0b2766
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
93 changes: 91 additions & 2 deletions packages/sdk/test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,9 @@ describe.sequential('Client', function () {
}, task.name);
});

it('Successfully broadcast serializeable payload', async ({ task }) => {
it('Should successfully broadcast serializeable payload', async ({
task,
}) => {
const cli = new yorkie.Client(testRPCAddr);
await cli.activate();

Expand All @@ -881,7 +883,7 @@ describe.sequential('Client', function () {
await cli.deactivate();
});

it('Throw error when broadcasting unserializeable payload', async ({
it('Should throw error when broadcasting unserializeable payload', async ({
task,
}) => {
const cli = new yorkie.Client(testRPCAddr);
Expand All @@ -901,3 +903,90 @@ describe.sequential('Client', function () {
await cli.deactivate();
});
});

it('Should trigger the handler for a subscribed broadcast event', async ({
task,
}) => {
await withTwoClientsAndDocuments<{ t: Text }>(
async (c1, d1, c2, d2) => {
const spy = vi.fn();

const broadcastTopic = 'test';
const unsubscribe = d2.subscribeBroadcastEvent(broadcastTopic, spy);

const payload = { a: 1, b: '2' };
await c1.broadcast(d1, broadcastTopic, payload);

// Assuming that every subscriber can receive the broadcast event within 1000ms.
await new Promise((res) => setTimeout(res, 1000));

expect(spy.mock.calls.length).toBe(1);
expect(spy.mock.calls[0][0]).toBe(broadcastTopic);
expect(spy.mock.calls[0][1]).toEqual(payload);

unsubscribe();
},
task.name,
SyncMode.Realtime,
);
});

it('Should not trigger the handler for an unsubscribed broadcast event', async ({
task,
}) => {
await withTwoClientsAndDocuments<{ t: Text }>(
async (c1, d1, c2, d2) => {
const spy = vi.fn();

const broadcastTopic1 = 'test1';
const broadcastTopic2 = 'test2';

const unsubscribe = d2.subscribeBroadcastEvent(broadcastTopic2, spy);

const payload = { a: 1, b: '2' };
await c1.broadcast(d1, broadcastTopic1, payload);

// Assuming that every subscriber can receive the broadcast event within 1000ms.
await new Promise((res) => setTimeout(res, 1000));

expect(spy.mock.calls.length).toBe(0);

unsubscribe();
},
task.name,
SyncMode.Realtime,
);
});

it('Should not trigger the handler for a broadcast event after unsubscribing', async ({
task,
}) => {
await withTwoClientsAndDocuments<{ t: Text }>(
async (c1, d1, c2, d2) => {
const spy = vi.fn();

const broadcastTopic = 'test';
const unsubscribe = d2.subscribeBroadcastEvent(broadcastTopic, spy);

const payload = { a: 1, b: '2' };
await c1.broadcast(d1, broadcastTopic, payload);

// Assuming that every subscriber can receive the broadcast event within 1000ms.
await new Promise((res) => setTimeout(res, 1000));

expect(spy.mock.calls.length).toBe(1);

unsubscribe();

await c1.broadcast(d1, broadcastTopic, payload);

// Assuming that every subscriber can receive the broadcast event within 1000ms.
await new Promise((res) => setTimeout(res, 1000));

// No change in the number of calls
expect(spy.mock.calls.length).toBe(1);
},
task.name,
SyncMode.Realtime,
);
});
5 changes: 3 additions & 2 deletions packages/sdk/test/integration/integration_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function withTwoClientsAndDocuments<T>(
d2: Document<T>,
) => Promise<void>,
title: string,
syncMode: SyncMode = SyncMode.Manual,
): Promise<void> {
const client1 = new yorkie.Client(testRPCAddr);
const client2 = new yorkie.Client(testRPCAddr);
Expand All @@ -31,8 +32,8 @@ export async function withTwoClientsAndDocuments<T>(
const doc1 = new yorkie.Document<T>(docKey);
const doc2 = new yorkie.Document<T>(docKey);

await client1.attach(doc1, { syncMode: SyncMode.Manual });
await client2.attach(doc2, { syncMode: SyncMode.Manual });
await client1.attach(doc1, { syncMode });
await client2.attach(doc2, { syncMode });

await callback(client1, doc1, client2, doc2);

Expand Down

0 comments on commit b0b2766

Please sign in to comment.