-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsync_test.dart
More file actions
340 lines (297 loc) · 9.75 KB
/
sync_test.dart
File metadata and controls
340 lines (297 loc) · 9.75 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import 'dart:convert';
import 'package:fake_async/fake_async.dart';
import 'package:file/local.dart';
import 'package:sqlite3/common.dart';
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_test/sqlite3_test.dart';
import 'package:test/test.dart';
import 'utils/native_test_utils.dart';
void main() {
// Needs an unique name per test file to avoid concurrency issues
final vfs =
TestSqliteFileSystem(fs: const LocalFileSystem(), name: 'sync-test-vfs');
setUpAll(() {
loadExtension();
sqlite3.registerVirtualFileSystem(vfs, makeDefault: false);
});
tearDownAll(() => sqlite3.unregisterVirtualFileSystem(vfs));
group('sync tests', () {
late CommonDatabase db;
setUp(() async {
db = openTestDatabase(vfs: vfs)
..select('select powersync_init();')
..select('select powersync_replace_schema(?)', [json.encode(_schema)]);
});
tearDown(() {
db.dispose();
});
void pushSyncData(
String bucket,
String opId,
String rowId,
Object op,
Object? data, {
Object? descriptions = _bucketDescriptions,
}) {
final encoded = json.encode({
'buckets': [
{
'bucket': bucket,
'data': [
{
'op_id': opId,
'op': op,
'object_type': 'items',
'object_id': rowId,
'checksum': 0,
'data': data,
}
],
}
],
if (descriptions != null) 'descriptions': descriptions,
});
db.execute('insert into powersync_operations (op, data) VALUES (?, ?);',
['save', encoded]);
}
bool pushCheckpointComplete(
String lastOpId, String? writeCheckpoint, List<Object?> checksums,
{int? priority}) {
final [row] = db.select('select powersync_validate_checkpoint(?) as r;', [
json.encode({
'last_op_id': lastOpId,
'write_checkpoint': writeCheckpoint,
'buckets': [
for (final cs in checksums.cast<Map<String, dynamic>>())
if (priority == null || cs['priority'] <= priority) cs
],
'priority': priority,
})
]);
final decoded = json.decode(row['r']);
if (decoded['valid'] != true) {
fail(row['r']);
}
db.execute(
'UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))',
[
lastOpId,
json.encode(checksums.map((e) => (e as Map)['bucket']).toList())
],
);
db.execute('INSERT INTO powersync_operations(op, data) VALUES (?, ?)', [
'sync_local',
priority != null
? jsonEncode({
'priority': priority,
'buckets': [
for (final cs in checksums.cast<Map<String, dynamic>>())
if (cs['priority'] <= priority) cs['bucket']
],
})
: null,
]);
return db.lastInsertRowId == 1;
}
ResultSet fetchRows() {
return db.select('select * from items');
}
test('does not publish until reaching checkpoint', () {
expect(fetchRows(), isEmpty);
pushSyncData('prio1', '1', 'row-0', 'PUT', {'col': 'hi'});
expect(fetchRows(), isEmpty);
expect(
pushCheckpointComplete(
'1', null, [_bucketChecksum('prio1', 1, checksum: 0)]),
isTrue);
expect(fetchRows(), [
{'id': 'row-0', 'col': 'hi'}
]);
});
test('does not publish with pending local data', () {
expect(fetchRows(), isEmpty);
db.execute("insert into items (id, col) values ('local', 'data');");
expect(fetchRows(), isNotEmpty);
pushSyncData('prio1', '1', 'row-0', 'PUT', {'col': 'hi'});
expect(
pushCheckpointComplete(
'1', null, [_bucketChecksum('prio1', 1, checksum: 0)]),
isFalse);
expect(fetchRows(), [
{'id': 'local', 'col': 'data'}
]);
});
test('publishes with local data for prio=0 buckets', () {
expect(fetchRows(), isEmpty);
db.execute("insert into items (id, col) values ('local', 'data');");
expect(fetchRows(), isNotEmpty);
pushSyncData('prio0', '1', 'row-0', 'PUT', {'col': 'hi'});
expect(
pushCheckpointComplete(
'1',
null,
[_bucketChecksum('prio0', 0, checksum: 0)],
priority: 0,
),
isTrue,
);
expect(fetchRows(), [
{'id': 'local', 'col': 'data'},
{'id': 'row-0', 'col': 'hi'},
]);
});
test('can publish partial checkpoints under different priorities', () {
for (var i = 0; i < 4; i++) {
pushSyncData('prio$i', '1', 'row-$i', 'PUT', {'col': '$i'});
}
expect(fetchRows(), isEmpty);
// Simulate a partial checkpoint complete for each of the buckets.
for (var i = 0; i < 4; i++) {
expect(
pushCheckpointComplete(
'1',
null,
[
for (var j = 0; j <= 4; j++)
_bucketChecksum(
'prio$j',
j,
// Give buckets outside of the current priority a wrong
// checksum. They should not be validated yet.
checksum: j <= i ? 0 : 1234,
),
],
priority: i,
),
isTrue,
);
expect(fetchRows(), [
for (var j = 0; j <= i; j++) {'id': 'row-$j', 'col': '$j'},
]);
expect(db.select('select 1 from ps_sync_state where priority = ?', [i]),
isNotEmpty);
// A sync at this priority includes all higher priorities too, so they
// should be cleared.
expect(db.select('select 1 from ps_sync_state where priority < ?', [i]),
isEmpty);
}
});
test('can sync multiple times', () {
fakeAsync((controller) {
for (var i = 0; i < 10; i++) {
for (var prio in const [1, 2, 3, null]) {
pushCheckpointComplete('1', null, [], priority: prio);
// Make sure there's only a single row in last_synced_at
expect(
db.select(
"SELECT datetime(last_synced_at, 'localtime') AS last_synced_at FROM ps_sync_state WHERE priority = ?",
[prio ?? 2147483647]),
[
{'last_synced_at': '2025-03-01 ${10 + i}:00:00'}
],
);
if (prio == null) {
expect(
db.select(
"SELECT datetime(powersync_last_synced_at(), 'localtime') AS last_synced_at"),
[
{'last_synced_at': '2025-03-01 ${10 + i}:00:00'}
],
);
}
}
controller.elapse(const Duration(hours: 1));
}
}, initialTime: DateTime(2025, 3, 1, 10));
});
test('clearing database clears sync status', () {
pushSyncData('prio1', '1', 'row-0', 'PUT', {'col': 'hi'});
expect(
pushCheckpointComplete(
'1', null, [_bucketChecksum('prio1', 1, checksum: 0)]),
isTrue);
expect(db.select('SELECT powersync_last_synced_at() AS r').single,
{'r': isNotNull});
expect(db.select('SELECT priority FROM ps_sync_state').single,
{'priority': 2147483647});
db.execute('SELECT powersync_clear(0)');
expect(db.select('SELECT powersync_last_synced_at() AS r').single,
{'r': isNull});
expect(db.select('SELECT * FROM ps_sync_state'), hasLength(0));
});
test('tracks download progress', () {
const bucket = 'bkt';
void expectProgress(int atLast, int sinceLast) {
final [row] = db.select(
'SELECT count_at_last, count_since_last FROM ps_buckets WHERE name = ?',
[bucket],
);
final [actualAtLast, actualSinceLast] = row.values;
expect(actualAtLast, atLast, reason: 'count_at_last mismatch');
expect(actualSinceLast, sinceLast, reason: 'count_since_last mismatch');
}
pushSyncData(bucket, '1', 'row-0', 'PUT', {'col': 'hi'});
expectProgress(0, 1);
pushSyncData(bucket, '2', 'row-1', 'PUT', {'col': 'hi'});
expectProgress(0, 2);
expect(
pushCheckpointComplete(
'2',
null,
[_bucketChecksum(bucket, 1, checksum: 0)],
priority: 1,
),
isTrue,
);
// Running partial or complete checkpoints should not reset stats, client
// SDKs are responsible for that.
expectProgress(0, 2);
expect(db.select('SELECT * FROM items'), isNotEmpty);
expect(
pushCheckpointComplete(
'2',
null,
[_bucketChecksum(bucket, 1, checksum: 0)],
),
isTrue,
);
expectProgress(0, 2);
db.execute('''
UPDATE ps_buckets SET count_since_last = 0, count_at_last = ?1->name
WHERE ?1->name IS NOT NULL
''', [
json.encode({bucket: 2}),
]);
expectProgress(2, 0);
// Run another iteration of this
pushSyncData(bucket, '3', 'row-3', 'PUT', {'col': 'hi'});
expectProgress(2, 1);
db.execute('''
UPDATE ps_buckets SET count_since_last = 0, count_at_last = ?1->name
WHERE ?1->name IS NOT NULL
''', [
json.encode({bucket: 3}),
]);
expectProgress(3, 0);
});
});
}
Object? _bucketChecksum(String bucket, int prio, {int checksum = 0}) {
return {'bucket': bucket, 'priority': prio, 'checksum': checksum};
}
const _schema = {
'tables': [
{
'name': 'items',
'columns': [
{'name': 'col', 'type': 'text'}
],
}
]
};
const _bucketDescriptions = {
'prio0': {'priority': 0},
'prio1': {'priority': 1},
'prio2': {'priority': 2},
'prio3': {'priority': 3},
};