Skip to content

Commit

Permalink
Add snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Nov 5, 2024
1 parent e635d2f commit c6c41e2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ describe.sequential('Client', function () {
await c2.sync();

// 01. c1 increases the counter for creating snapshot.
for (let i = 0; i < 500; i++) {
for (let i = 0; i < 1000; i++) {
d1.update((r) => r.counter.increase(1));
}
await c1.sync();
Expand Down
38 changes: 37 additions & 1 deletion packages/sdk/test/unit/document/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import {

import { Document, DocEventType } from '@yorkie-js-sdk/src/document/document';
import { OperationInfo } from '@yorkie-js-sdk/src/document/operation/operation';
import { JSONArray, Text, Counter, Tree } from '@yorkie-js-sdk/src/yorkie';
import yorkie, {
JSONArray,
Text,
Counter,
Tree,
} from '@yorkie-js-sdk/src/yorkie';
import { CounterType } from '@yorkie-js-sdk/src/document/crdt/counter';
import { withTwoClientsAndDocuments } from '@yorkie-js-sdk/test/integration/integration_helper';

describe.sequential('Document', function () {
afterEach(() => {
Expand Down Expand Up @@ -1490,4 +1496,34 @@ describe.sequential('Document', function () {
});
});
});

it('should publish snapshot event with up-to-date document', async function ({
task,
}) {
type TestDoc = { counter: Counter };
await withTwoClientsAndDocuments<TestDoc>(async (c1, d1, c2, d2) => {
const eventCollector = new EventCollector<number>();
d2.subscribe((event) => {
if (event.type === DocEventType.Snapshot) {
eventCollector.add(d2.toJSForTest().value.counter.value);
}
});

d1.update((r) => (r.counter = new Counter(yorkie.IntType, 0)));
await c1.sync();
await c2.sync();

// 01. c1 increases the counter for creating snapshot.
for (let i = 0; i < 1000; i++) {
d1.update((r) => r.counter.increase(1));
}
await c1.sync();

// 02. c2 receives the snapshot and increases the counter simultaneously.
c2.sync();
d2.update((r) => r.counter.increase(1));

await eventCollector.waitAndVerifyNthEvent(1, 1001);
}, task.name);
});
});

0 comments on commit c6c41e2

Please sign in to comment.