-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathsync.test.ts
More file actions
297 lines (248 loc) · 9.66 KB
/
sync.test.ts
File metadata and controls
297 lines (248 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { describe, vi, expect, beforeEach } from 'vitest';
import { MockSyncService, mockSyncServiceTest, TestConnector, waitForSyncStatus } from './utils';
import {
AbstractPowerSyncDatabase,
BucketChecksum,
OplogEntryJSON,
ProgressWithOperations,
SyncStreamConnectionMethod
} from '@powersync/common';
import Logger from 'js-logger';
Logger.useDefaults({ defaultLevel: Logger.WARN });
describe('Sync', () => {
describe('reports progress', () => {
let lastOpId = 0;
beforeEach(() => {
lastOpId = 0;
});
function pushDataLine(service: MockSyncService, bucket: string, amount: number) {
const data: OplogEntryJSON[] = [];
for (let i = 0; i < amount; i++) {
data.push({
op_id: `${++lastOpId}`,
op: 'PUT',
object_type: bucket,
object_id: `${lastOpId}`,
checksum: 0,
data: '{}'
});
}
service.pushLine({
data: {
bucket,
data
}
});
}
function pushCheckpointComplete(service: MockSyncService, priority?: number) {
if (priority != null) {
service.pushLine({
partial_checkpoint_complete: {
last_op_id: `${lastOpId}`,
priority
}
});
} else {
service.pushLine({
checkpoint_complete: {
last_op_id: `${lastOpId}`
}
});
}
}
mockSyncServiceTest('without priorities', async ({ syncService }) => {
const database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [bucket('a', 10)]
}
});
await waitForProgress(database, [0, 10]);
pushDataLine(syncService, 'a', 10);
await waitForProgress(database, [10, 10]);
pushCheckpointComplete(syncService);
await waitForSyncStatus(database, (s) => s.downloadProgress == null);
// Emit new data, progress should be 0/2 instead of 10/12
syncService.pushLine({
checkpoint_diff: {
last_op_id: '12',
updated_buckets: [bucket('a', 12)],
removed_buckets: []
}
});
await waitForProgress(database, [0, 2]);
pushDataLine(syncService, 'a', 2);
await waitForProgress(database, [2, 2]);
pushCheckpointComplete(syncService);
await waitForSyncStatus(database, (s) => s.downloadProgress == null);
});
mockSyncServiceTest('interrupted sync', async ({ syncService }) => {
let database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [bucket('a', 10)]
}
});
await waitForProgress(database, [0, 10]);
pushDataLine(syncService, 'a', 5);
await waitForProgress(database, [5, 10]);
// Close this database before sending the checkpoint...
await database.close();
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(0));
// And open a new one
database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
// Send same checkpoint again
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [bucket('a', 10)]
}
});
// Progress should be restored instead of e.g. saying 0/5 now.
await waitForProgress(database, [5, 10]);
pushCheckpointComplete(syncService);
await waitForSyncStatus(database, (s) => s.downloadProgress == null);
});
mockSyncServiceTest('interrupted sync with new checkpoint', async ({ syncService }) => {
let database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [bucket('a', 10)]
}
});
await waitForProgress(database, [0, 10]);
pushDataLine(syncService, 'a', 5);
await waitForProgress(database, [5, 10]);
// Re-open database
await database.close();
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(0));
database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
// Send checkpoint with new data
syncService.pushLine({
checkpoint: {
last_op_id: '12',
buckets: [bucket('a', 12)]
}
});
await waitForProgress(database, [5, 12]);
pushCheckpointComplete(syncService);
await waitForSyncStatus(database, (s) => s.downloadProgress == null);
});
mockSyncServiceTest('different priorities', async ({ syncService }) => {
let database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [
bucket('a', 5, {priority: 0}),
bucket('b', 5, {priority: 2}),
]
}
});
// Should be at 0/10 for total progress (which is the same as the progress for prio 2), and a 0/5 towards prio 0.
await waitForProgress(database, [0, 10], [[0, [0, 5]], [2, [0, 10]]]);
pushDataLine(syncService, 'a', 5);
await waitForProgress(database, [5, 10], [[0, [5, 5]], [2, [5, 10]]]);
pushCheckpointComplete(syncService, 0);
await waitForProgress(database, [5, 10], [[0, [5, 5]], [2, [5, 10]]]);
pushDataLine(syncService, 'b', 2);
await waitForProgress(database, [7, 10], [[0, [5, 5]], [2, [7, 10]]]);
// Before syncing b fully, send a new checkpoint
syncService.pushLine({
checkpoint: {
last_op_id: '14',
buckets: [
bucket('a', 8, {priority: 0}),
bucket('b', 6, {priority: 2}),
]
}
});
await waitForProgress(database, [7, 14], [[0, [5, 8]], [2, [7, 14]]]);
pushDataLine(syncService, 'a', 3);
await waitForProgress(database, [10, 14], [[0, [8, 8]], [2, [10, 14]]]);
pushCheckpointComplete(syncService, 0);
await waitForProgress(database, [10, 14], [[0, [8, 8]], [2, [10, 14]]]);
pushDataLine(syncService, 'b', 4);
await waitForProgress(database, [14, 14], [[0, [8, 8]], [2, [14, 14]]]);
pushCheckpointComplete(syncService);
await waitForSyncStatus(database, (s) => s.downloadProgress == null);
});
mockSyncServiceTest('uses correct state when reconnecting', async ({syncService}) => {
let database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
syncService.pushLine({
checkpoint: {
last_op_id: '10',
buckets: [
bucket('a', 5, {priority: 0}),
bucket('b', 5, {priority: 3}),
]
}
});
// Sync priority 0 completely, start with rest
pushDataLine(syncService, 'a', 5);
pushDataLine(syncService, 'b', 1);
pushCheckpointComplete(syncService, 0);
await database.waitForFirstSync({priority: 0});
await database.close();
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(0));
database = await syncService.createDatabase();
database.connect(new TestConnector(), { connectionMethod: SyncStreamConnectionMethod.HTTP });
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
expect(syncService.connectedListeners[0].buckets).toStrictEqual([
{"name": "a", "after": "10"},
{"name": "b", "after": "6"},
]);
});
});
});
function bucket(name: string, count: number, options: {priority: number} = {priority: 3}): BucketChecksum {
return {
bucket: name,
count,
checksum: 0,
priority: options.priority,
};
}
async function waitForProgress(
database: AbstractPowerSyncDatabase,
total: [number, number],
forPriorities: [number, [number, number]][] = []
) {
await waitForSyncStatus(database, (status) => {
const progress = status.downloadProgress;
if (!progress) {
return false;
}
//console.log('checking', progress);
const check = (expected: [number, number], actual: ProgressWithOperations): boolean => {
return actual.downloadedOperations == expected[0] && actual.totalOperations == expected[1];
};
if (!check(total, progress)) {
return false;
}
for (const [priority, expected] of forPriorities) {
if (!check(expected, progress.untilPriority(priority))) {
//console.log('failed for', priority, expected, progress);
return false;
}
}
return true;
});
}