Skip to content

Commit 2abcb03

Browse files
committed
Test disconnect while fetching credentials
1 parent 82c774f commit 2abcb03

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

packages/powersync_core/lib/src/abort_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AbortController {
2525
_abortRequested.complete();
2626
}
2727

28-
await _abortCompleter.future;
28+
await onCompletion;
2929
}
3030

3131
/// Signal that an abort has completed.

packages/powersync_core/lib/src/database/powersync_db_mixin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
372372
_abortActiveSync = null;
373373
} else {
374374
/// Wait for the abort to complete. Continue updating the sync status after completed
375-
await disconnector.onAbort;
375+
await disconnector.onCompletion;
376376
}
377377
}
378378
}

packages/powersync_core/test/streaming_sync_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:logging/logging.dart';
99
import 'package:powersync_core/powersync_core.dart';
1010
import 'package:test/test.dart';
1111

12+
import 'server/sync_server/in_memory_sync_server.dart';
1213
import 'test_server.dart';
1314
import 'utils/abstract_test_utils.dart';
1415
import 'utils/test_utils_impl.dart';
@@ -61,6 +62,40 @@ void main() {
6162
server.close();
6263
});
6364

65+
test('can disconnect in fetchCredentials', () async {
66+
final service = MockSyncService();
67+
final server = await createServer(mockSyncService: service);
68+
final ignoreLogger = Logger.detached('powersync.test');
69+
70+
final pdb =
71+
await testUtils.setupPowerSync(path: path, logger: ignoreLogger);
72+
pdb.retryDelay = Duration(milliseconds: 50);
73+
final connector = TestConnector(expectAsync0(() async {
74+
return PowerSyncCredentials(endpoint: server.endpoint, token: 'token');
75+
}));
76+
77+
await pdb.connect(connector: connector);
78+
while (server.connectionCount != 1) {
79+
await Future<void>.delayed(const Duration(milliseconds: 100));
80+
}
81+
82+
service.addKeepAlive(60);
83+
84+
final didDisconnect = Completer<void>();
85+
86+
connector.fetchCredentialsCallback = expectAsync0(() async {
87+
Zone.current.parent!.scheduleMicrotask(() {
88+
didDisconnect.complete(pdb.disconnect());
89+
});
90+
91+
throw 'deliberate disconnect';
92+
});
93+
94+
service.addKeepAlive(0);
95+
await didDisconnect.future;
96+
expect(pdb.currentStatus.connected, isFalse);
97+
});
98+
6499
test('can connect as initial operation', () async {
65100
final server = await createServer();
66101
final ignoreLogger = Logger.detached('powersync.test');

packages/powersync_core/test/test_server.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import 'package:shelf/shelf.dart';
88
import 'package:shelf/shelf_io.dart' as shelf_io;
99
import 'package:shelf_router/shelf_router.dart';
1010

11-
class TestServer {
11+
import 'server/sync_server/in_memory_sync_server.dart';
12+
13+
final class TestServer {
1214
late HttpServer server;
1315
Router app = Router();
1416
int maxConnectionCount = 0;
1517
int tokenExpiresIn;
1618

1719
TestServer({this.tokenExpiresIn = 65});
1820

19-
Future<void> init() async {
21+
Future<void> init({MockSyncService? mockSyncService}) async {
2022
app.post('/sync/stream', handleSyncStream);
2123
// Open on an arbitrary open port
22-
server = await shelf_io.serve(app.call, 'localhost', 0);
24+
server = await shelf_io.serve(
25+
mockSyncService?.router.call ?? app.call, 'localhost', 0);
2326
}
2427

2528
String get endpoint {
@@ -34,6 +37,9 @@ class TestServer {
3437
return server.connectionsInfo();
3538
}
3639

40+
/// The default response if no [MockSyncService] has been passed to [init].
41+
///
42+
/// This will emit keepalive messages frequently.
3743
Future<Response> handleSyncStream(Request request) async {
3844
maxConnectionCount = max(connectionCount, maxConnectionCount);
3945

@@ -61,9 +67,9 @@ class TestServer {
6167
}
6268
}
6369

64-
Future<TestServer> createServer() async {
70+
Future<TestServer> createServer({MockSyncService? mockSyncService}) async {
6571
var server = TestServer();
66-
await server.init();
72+
await server.init(mockSyncService: mockSyncService);
6773
return server;
6874
}
6975

packages/powersync_core/test/utils/abstract_test_utils.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,21 @@ abstract class AbstractTestUtils {
125125
}
126126

127127
class TestConnector extends PowerSyncBackendConnector {
128-
final Future<PowerSyncCredentials> Function() _fetchCredentials;
129-
final Future<void> Function(PowerSyncDatabase)? _uploadData;
128+
Future<PowerSyncCredentials> Function() fetchCredentialsCallback;
129+
Future<void> Function(PowerSyncDatabase)? uploadDataCallback;
130130

131-
TestConnector(this._fetchCredentials,
131+
TestConnector(this.fetchCredentialsCallback,
132132
{Future<void> Function(PowerSyncDatabase)? uploadData})
133-
: _uploadData = uploadData;
133+
: uploadDataCallback = uploadData;
134134

135135
@override
136136
Future<PowerSyncCredentials?> fetchCredentials() {
137-
return _fetchCredentials();
137+
return fetchCredentialsCallback();
138138
}
139139

140140
@override
141141
Future<void> uploadData(PowerSyncDatabase database) async {
142-
await _uploadData?.call(database);
142+
await uploadDataCallback?.call(database);
143143
}
144144
}
145145

0 commit comments

Comments
 (0)