@@ -20,22 +20,26 @@ void main() {
20
20
late CommonDatabase raw;
21
21
late PowerSyncDatabase database;
22
22
late MockSyncService syncService;
23
- late StreamingSyncImplementation syncClient;
23
+ late StreamingSync syncClient;
24
+ var credentialsCallbackCount = 0 ;
24
25
25
26
setUp (() async {
27
+ credentialsCallbackCount = 0 ;
26
28
final (client, server) = inMemoryServer ();
27
29
syncService = MockSyncService ();
28
30
server.mount (syncService.router.call);
29
31
30
32
factory = await testUtils.testFactory ();
31
33
(raw, database) = await factory .openInMemoryDatabase ();
32
34
await database.initialize ();
35
+
33
36
syncClient = database.connectWithMockService (
34
37
client,
35
38
TestConnector (() async {
39
+ credentialsCallbackCount++ ;
36
40
return PowerSyncCredentials (
37
41
endpoint: server.url.toString (),
38
- token: 'token not used here ' ,
42
+ token: 'token$ credentialsCallbackCount ' ,
39
43
expiresAt: DateTime .now (),
40
44
);
41
45
}),
@@ -312,18 +316,37 @@ void main() {
312
316
},
313
317
);
314
318
});
319
+
320
+ test ('reconnects when token expires' , () async {
321
+ await waitForConnection ();
322
+ expect (credentialsCallbackCount, 1 );
323
+ // When the sync service says the token has expired
324
+ syncService
325
+ ..addLine ({'token_expires_in' : 0 })
326
+ ..endCurrentListener ();
327
+
328
+ final nextRequest = await syncService.waitForListener;
329
+ expect (nextRequest.headers['Authorization' ], 'Token token2' );
330
+ expect (credentialsCallbackCount, 2 );
331
+ });
315
332
});
316
333
}
317
334
318
335
TypeMatcher <SyncStatus > isSyncStatus (
319
- {Object ? downloading, Object ? connected, Object ? hasSynced}) {
336
+ {Object ? downloading,
337
+ Object ? connected,
338
+ Object ? connecting,
339
+ Object ? hasSynced}) {
320
340
var matcher = isA <SyncStatus >();
321
341
if (downloading != null ) {
322
342
matcher = matcher.having ((e) => e.downloading, 'downloading' , downloading);
323
343
}
324
344
if (connected != null ) {
325
345
matcher = matcher.having ((e) => e.connected, 'connected' , connected);
326
346
}
347
+ if (connecting != null ) {
348
+ matcher = matcher.having ((e) => e.connecting, 'connecting' , connecting);
349
+ }
327
350
if (hasSynced != null ) {
328
351
matcher = matcher.having ((e) => e.hasSynced, 'hasSynced' , hasSynced);
329
352
}
0 commit comments