Skip to content

Commit

Permalink
added tests for disconnectAndClear
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Mar 5, 2024
1 parent 5efb6ab commit 16ada17
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/powersync/lib/src/streaming_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class StreamingSyncImplementation {
var invalidCredentials = false;
while (true) {
if (abortController?.aborted == true) {
abortController!.completeAbort();
return;
}
_updateStatus(connecting: true);
Expand Down
64 changes: 64 additions & 0 deletions packages/powersync/test/disconnect_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:powersync/powersync.dart';
import 'package:test/test.dart';
import 'streaming_sync_test.dart';
import 'utils/test_utils_impl.dart';
import 'watch_test.dart';

final testUtils = TestUtils();

void main() {
group('Disconnect Tests', () {
late String path;

setUp(() async {
path = testUtils.dbPath();
await testUtils.cleanDb(path: path);
});

test('Multiple calls to disconnect', () async {
// Start with "offline-only" schema.
// This does not record any operations to the crud queue.
final db = await testUtils.setupPowerSync(path: path, schema: testSchema);

credentialsCallback() async {
// A blank endpoint will fail, but that's okay for this test
final endpoint = '';
return PowerSyncCredentials(
endpoint: endpoint,
token: 'token',
userId: 'u1',
expiresAt: DateTime.now());
}

db.retryDelay = Duration(milliseconds: 5000);
var connector = TestConnector(credentialsCallback);
await db.connect(connector: connector);

// Call disconnect multiple times, each Future should resolve
final disconnect1 = db.disconnect();
final disconnect2 = db.disconnect();

await expectLater(disconnect1, completes);
await expectLater(disconnect2, completes);
});

test('disconnectAndClear clears DB', () async {
// Start with "offline-only" schema.
// This does not record any operations to the crud queue.
final db = await testUtils.setupPowerSync(path: path, schema: testSchema);

await db.execute(
'INSERT INTO customers (id, name, email) VALUES(uuid(), ?, ?)',
['Steven', 'steven@journeyapps.com']);

final getCustomersQuery = 'SELECT * from customers';
final initialCustomers = await db.getAll(getCustomersQuery);
expect(initialCustomers.length, equals(1));

await db.disconnectAndClear();

final finalCustomers = await db.getAll(getCustomersQuery);
expect(finalCustomers.length, equals(0));
});
});
}

0 comments on commit 16ada17

Please sign in to comment.