Skip to content

Commit 77385ef

Browse files
committed
Test close during request
1 parent bb9f4fb commit 77385ef

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

packages/powersync/test/web/sync_worker_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@TestOn('browser')
22
library;
33

4+
import 'dart:async';
5+
46
import 'package:logging/logging.dart';
57
import 'package:powersync/powersync.dart';
68
import 'package:powersync/src/sync/streaming_sync.dart';
@@ -69,11 +71,41 @@ void main() {
6971
expect(syncRunner.sync, isNull);
7072
expect(syncRunner.connections, isEmpty);
7173
});
74+
75+
test('handles tabs closing while serving a request', () async {
76+
late SyncWorkerHandle handle;
77+
final didRequestCredentials = Completer<void>();
78+
handle = createWorkerHandle(connector: _ThrowingBackendConnector(() {
79+
// When the fetchCredentials request is sent, ther should be a sync
80+
// process.
81+
final syncRunner = syncWorker.requestedSyncTasks.values.single;
82+
expect(syncRunner.sync, isNotNull);
83+
expect(syncRunner.connections, hasLength(1));
84+
85+
// Close the handle while the fetchCredentials request is active, meaning
86+
// the sync worker will never receive a response.
87+
handle.closeChannel();
88+
didRequestCredentials.complete();
89+
}));
90+
91+
await handle.streamingSync('test_database');
92+
await didRequestCredentials.future;
93+
await pumpEventQueue();
94+
95+
final syncRunner = syncWorker.requestedSyncTasks.values.single;
96+
expect(syncRunner.sync, isNull);
97+
expect(syncRunner.connections, isEmpty);
98+
});
7299
}
73100

74101
final class _ThrowingBackendConnector extends PowerSyncBackendConnector {
102+
void Function()? onFetchCredentials;
103+
104+
_ThrowingBackendConnector([this.onFetchCredentials]);
105+
75106
@override
76107
Future<PowerSyncCredentials?> fetchCredentials() async {
108+
onFetchCredentials?.call();
77109
throw UnsupportedError('Expected error from fetchCredentials');
78110
}
79111

0 commit comments

Comments
 (0)