|
1 | 1 | @TestOn('browser') |
2 | 2 | library; |
3 | 3 |
|
| 4 | +import 'dart:async'; |
| 5 | + |
4 | 6 | import 'package:logging/logging.dart'; |
5 | 7 | import 'package:powersync/powersync.dart'; |
6 | 8 | import 'package:powersync/src/sync/streaming_sync.dart'; |
@@ -69,11 +71,41 @@ void main() { |
69 | 71 | expect(syncRunner.sync, isNull); |
70 | 72 | expect(syncRunner.connections, isEmpty); |
71 | 73 | }); |
| 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 | + }); |
72 | 99 | } |
73 | 100 |
|
74 | 101 | final class _ThrowingBackendConnector extends PowerSyncBackendConnector { |
| 102 | + void Function()? onFetchCredentials; |
| 103 | + |
| 104 | + _ThrowingBackendConnector([this.onFetchCredentials]); |
| 105 | + |
75 | 106 | @override |
76 | 107 | Future<PowerSyncCredentials?> fetchCredentials() async { |
| 108 | + onFetchCredentials?.call(); |
77 | 109 | throw UnsupportedError('Expected error from fetchCredentials'); |
78 | 110 | } |
79 | 111 |
|
|
0 commit comments