@@ -169,77 +169,124 @@ describe('Sync', () => {
169
169
} ) ;
170
170
171
171
mockSyncServiceTest ( 'different priorities' , async ( { syncService } ) => {
172
- let database = await syncService . createDatabase ( ) ;
173
- database . connect ( new TestConnector ( ) , { connectionMethod : SyncStreamConnectionMethod . HTTP } ) ;
174
- await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
175
-
176
- syncService . pushLine ( {
177
- checkpoint : {
178
- last_op_id : '10' ,
179
- buckets : [
180
- bucket ( 'a' , 5 , { priority : 0 } ) ,
181
- bucket ( 'b' , 5 , { priority : 2 } ) ,
182
- ]
183
- }
184
- } ) ;
185
-
186
- // 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.
187
- await waitForProgress ( database , [ 0 , 10 ] , [ [ 0 , [ 0 , 5 ] ] , [ 2 , [ 0 , 10 ] ] ] ) ;
188
-
189
- pushDataLine ( syncService , 'a' , 5 ) ;
190
- await waitForProgress ( database , [ 5 , 10 ] , [ [ 0 , [ 5 , 5 ] ] , [ 2 , [ 5 , 10 ] ] ] ) ;
191
-
192
- pushCheckpointComplete ( syncService , 0 ) ;
193
- await waitForProgress ( database , [ 5 , 10 ] , [ [ 0 , [ 5 , 5 ] ] , [ 2 , [ 5 , 10 ] ] ] ) ;
172
+ let database = await syncService . createDatabase ( ) ;
173
+ database . connect ( new TestConnector ( ) , { connectionMethod : SyncStreamConnectionMethod . HTTP } ) ;
174
+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
194
175
195
- pushDataLine ( syncService , 'b' , 2 ) ;
196
- await waitForProgress ( database , [ 7 , 10 ] , [ [ 0 , [ 5 , 5 ] ] , [ 2 , [ 7 , 10 ] ] ] ) ;
176
+ syncService . pushLine ( {
177
+ checkpoint : {
178
+ last_op_id : '10' ,
179
+ buckets : [ bucket ( 'a' , 5 , { priority : 0 } ) , bucket ( 'b' , 5 , { priority : 2 } ) ]
180
+ }
181
+ } ) ;
197
182
198
- // Before syncing b fully, send a new checkpoint
199
- syncService . pushLine ( {
200
- checkpoint : {
201
- last_op_id : '14' ,
202
- buckets : [
203
- bucket ( 'a' , 8 , { priority : 0 } ) ,
204
- bucket ( 'b' , 6 , { priority : 2 } ) ,
205
- ]
206
- }
207
- } ) ;
208
- await waitForProgress ( database , [ 7 , 14 ] , [ [ 0 , [ 5 , 8 ] ] , [ 2 , [ 7 , 14 ] ] ] ) ;
183
+ // 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.
184
+ await waitForProgress (
185
+ database ,
186
+ [ 0 , 10 ] ,
187
+ [
188
+ [ 0 , [ 0 , 5 ] ] ,
189
+ [ 2 , [ 0 , 10 ] ]
190
+ ]
191
+ ) ;
209
192
210
- pushDataLine ( syncService , 'a' , 3 ) ;
211
- await waitForProgress ( database , [ 10 , 14 ] , [ [ 0 , [ 8 , 8 ] ] , [ 2 , [ 10 , 14 ] ] ] ) ;
193
+ pushDataLine ( syncService , 'a' , 5 ) ;
194
+ await waitForProgress (
195
+ database ,
196
+ [ 5 , 10 ] ,
197
+ [
198
+ [ 0 , [ 5 , 5 ] ] ,
199
+ [ 2 , [ 5 , 10 ] ]
200
+ ]
201
+ ) ;
212
202
213
- pushCheckpointComplete ( syncService , 0 ) ;
214
- await waitForProgress ( database , [ 10 , 14 ] , [ [ 0 , [ 8 , 8 ] ] , [ 2 , [ 10 , 14 ] ] ] ) ;
203
+ pushCheckpointComplete ( syncService , 0 ) ;
204
+ await waitForProgress (
205
+ database ,
206
+ [ 5 , 10 ] ,
207
+ [
208
+ [ 0 , [ 5 , 5 ] ] ,
209
+ [ 2 , [ 5 , 10 ] ]
210
+ ]
211
+ ) ;
212
+
213
+ pushDataLine ( syncService , 'b' , 2 ) ;
214
+ await waitForProgress (
215
+ database ,
216
+ [ 7 , 10 ] ,
217
+ [
218
+ [ 0 , [ 5 , 5 ] ] ,
219
+ [ 2 , [ 7 , 10 ] ]
220
+ ]
221
+ ) ;
222
+
223
+ // Before syncing b fully, send a new checkpoint
224
+ syncService . pushLine ( {
225
+ checkpoint : {
226
+ last_op_id : '14' ,
227
+ buckets : [ bucket ( 'a' , 8 , { priority : 0 } ) , bucket ( 'b' , 6 , { priority : 2 } ) ]
228
+ }
229
+ } ) ;
230
+ await waitForProgress (
231
+ database ,
232
+ [ 7 , 14 ] ,
233
+ [
234
+ [ 0 , [ 5 , 8 ] ] ,
235
+ [ 2 , [ 7 , 14 ] ]
236
+ ]
237
+ ) ;
238
+
239
+ pushDataLine ( syncService , 'a' , 3 ) ;
240
+ await waitForProgress (
241
+ database ,
242
+ [ 10 , 14 ] ,
243
+ [
244
+ [ 0 , [ 8 , 8 ] ] ,
245
+ [ 2 , [ 10 , 14 ] ]
246
+ ]
247
+ ) ;
215
248
216
- pushDataLine ( syncService , 'b' , 4 ) ;
217
- await waitForProgress ( database , [ 14 , 14 ] , [ [ 0 , [ 8 , 8 ] ] , [ 2 , [ 14 , 14 ] ] ] ) ;
249
+ pushCheckpointComplete ( syncService , 0 ) ;
250
+ await waitForProgress (
251
+ database ,
252
+ [ 10 , 14 ] ,
253
+ [
254
+ [ 0 , [ 8 , 8 ] ] ,
255
+ [ 2 , [ 10 , 14 ] ]
256
+ ]
257
+ ) ;
258
+
259
+ pushDataLine ( syncService , 'b' , 4 ) ;
260
+ await waitForProgress (
261
+ database ,
262
+ [ 14 , 14 ] ,
263
+ [
264
+ [ 0 , [ 8 , 8 ] ] ,
265
+ [ 2 , [ 14 , 14 ] ]
266
+ ]
267
+ ) ;
218
268
219
- pushCheckpointComplete ( syncService ) ;
220
- await waitForSyncStatus ( database , ( s ) => s . downloadProgress == null ) ;
269
+ pushCheckpointComplete ( syncService ) ;
270
+ await waitForSyncStatus ( database , ( s ) => s . downloadProgress == null ) ;
221
271
} ) ;
222
272
223
- mockSyncServiceTest ( 'uses correct state when reconnecting' , async ( { syncService} ) => {
273
+ mockSyncServiceTest ( 'uses correct state when reconnecting' , async ( { syncService } ) => {
224
274
let database = await syncService . createDatabase ( ) ;
225
275
database . connect ( new TestConnector ( ) , { connectionMethod : SyncStreamConnectionMethod . HTTP } ) ;
226
276
await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
227
277
228
278
syncService . pushLine ( {
229
279
checkpoint : {
230
280
last_op_id : '10' ,
231
- buckets : [
232
- bucket ( 'a' , 5 , { priority : 0 } ) ,
233
- bucket ( 'b' , 5 , { priority : 3 } ) ,
234
- ]
281
+ buckets : [ bucket ( 'a' , 5 , { priority : 0 } ) , bucket ( 'b' , 5 , { priority : 3 } ) ]
235
282
}
236
283
} ) ;
237
284
238
285
// Sync priority 0 completely, start with rest
239
286
pushDataLine ( syncService , 'a' , 5 ) ;
240
287
pushDataLine ( syncService , 'b' , 1 ) ;
241
288
pushCheckpointComplete ( syncService , 0 ) ;
242
- await database . waitForFirstSync ( { priority : 0 } ) ;
289
+ await database . waitForFirstSync ( { priority : 0 } ) ;
243
290
244
291
await database . close ( ) ;
245
292
await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 0 ) ) ;
@@ -248,19 +295,56 @@ describe('Sync', () => {
248
295
await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
249
296
250
297
expect ( syncService . connectedListeners [ 0 ] . buckets ) . toStrictEqual ( [
251
- { " name" : "a" , " after" : "10" } ,
252
- { " name" : "b" , " after" : "6" } ,
298
+ { name : 'a' , after : '10' } ,
299
+ { name : 'b' , after : '6' }
253
300
] ) ;
254
301
} ) ;
302
+
303
+ mockSyncServiceTest ( 'interrupt and defrag' , async ( { syncService } ) => {
304
+ let database = await syncService . createDatabase ( ) ;
305
+ database . connect ( new TestConnector ( ) , { connectionMethod : SyncStreamConnectionMethod . HTTP } ) ;
306
+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
307
+
308
+ syncService . pushLine ( {
309
+ checkpoint : {
310
+ last_op_id : '10' ,
311
+ buckets : [ bucket ( 'a' , 10 ) ]
312
+ }
313
+ } ) ;
314
+
315
+ await waitForProgress ( database , [ 0 , 10 ] ) ;
316
+ pushDataLine ( syncService , 'a' , 5 ) ;
317
+ await waitForProgress ( database , [ 5 , 10 ] ) ;
318
+
319
+ // Re-open database
320
+ await database . close ( ) ;
321
+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 0 ) ) ;
322
+ database = await syncService . createDatabase ( ) ;
323
+ database . connect ( new TestConnector ( ) , { connectionMethod : SyncStreamConnectionMethod . HTTP } ) ;
324
+ await vi . waitFor ( ( ) => expect ( syncService . connectedListeners ) . toHaveLength ( 1 ) ) ;
325
+
326
+ // A sync rule deploy could reset buckets, making the new bucket smaller than the existing one.
327
+ syncService . pushLine ( {
328
+ checkpoint : {
329
+ last_op_id : '14' ,
330
+ buckets : [ bucket ( 'a' , 4 ) ]
331
+ }
332
+ } ) ;
333
+
334
+ // In this special case, don't report 5/4 as progress.
335
+ await waitForProgress ( database , [ 0 , 4 ] ) ;
336
+ pushCheckpointComplete ( syncService ) ;
337
+ await waitForSyncStatus ( database , ( s ) => s . downloadProgress == null ) ;
338
+ } ) ;
255
339
} ) ;
256
340
} ) ;
257
341
258
- function bucket ( name : string , count : number , options : { priority : number } = { priority : 3 } ) : BucketChecksum {
342
+ function bucket ( name : string , count : number , options : { priority : number } = { priority : 3 } ) : BucketChecksum {
259
343
return {
260
344
bucket : name ,
261
345
count,
262
346
checksum : 0 ,
263
- priority : options . priority ,
347
+ priority : options . priority
264
348
} ;
265
349
}
266
350
0 commit comments