|
| 1 | +import 'package:powersync/powersync.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | +import 'streaming_sync_test.dart'; |
| 4 | +import 'utils/test_utils_impl.dart'; |
| 5 | +import 'watch_test.dart'; |
| 6 | + |
| 7 | +final testUtils = TestUtils(); |
| 8 | + |
| 9 | +void main() { |
| 10 | + group('Disconnect Tests', () { |
| 11 | + late String path; |
| 12 | + |
| 13 | + setUp(() async { |
| 14 | + path = testUtils.dbPath(); |
| 15 | + await testUtils.cleanDb(path: path); |
| 16 | + }); |
| 17 | + |
| 18 | + test('Multiple calls to disconnect', () async { |
| 19 | + // Start with "offline-only" schema. |
| 20 | + // This does not record any operations to the crud queue. |
| 21 | + final db = await testUtils.setupPowerSync(path: path, schema: testSchema); |
| 22 | + |
| 23 | + credentialsCallback() async { |
| 24 | + // A blank endpoint will fail, but that's okay for this test |
| 25 | + final endpoint = ''; |
| 26 | + return PowerSyncCredentials( |
| 27 | + endpoint: endpoint, |
| 28 | + token: 'token', |
| 29 | + userId: 'u1', |
| 30 | + expiresAt: DateTime.now()); |
| 31 | + } |
| 32 | + |
| 33 | + db.retryDelay = Duration(milliseconds: 5000); |
| 34 | + var connector = TestConnector(credentialsCallback); |
| 35 | + await db.connect(connector: connector); |
| 36 | + |
| 37 | + // Call disconnect multiple times, each Future should resolve |
| 38 | + final disconnect1 = db.disconnect(); |
| 39 | + final disconnect2 = db.disconnect(); |
| 40 | + |
| 41 | + await expectLater(disconnect1, completes); |
| 42 | + await expectLater(disconnect2, completes); |
| 43 | + }); |
| 44 | + |
| 45 | + test('disconnectAndClear clears DB', () async { |
| 46 | + // Start with "offline-only" schema. |
| 47 | + // This does not record any operations to the crud queue. |
| 48 | + final db = await testUtils.setupPowerSync(path: path, schema: testSchema); |
| 49 | + |
| 50 | + await db.execute( |
| 51 | + 'INSERT INTO customers (id, name, email) VALUES(uuid(), ?, ?)', |
| 52 | + ['Steven', 'steven@journeyapps.com']); |
| 53 | + |
| 54 | + final getCustomersQuery = 'SELECT * from customers'; |
| 55 | + final initialCustomers = await db.getAll(getCustomersQuery); |
| 56 | + expect(initialCustomers.length, equals(1)); |
| 57 | + |
| 58 | + await db.disconnectAndClear(); |
| 59 | + |
| 60 | + final finalCustomers = await db.getAll(getCustomersQuery); |
| 61 | + expect(finalCustomers.length, equals(0)); |
| 62 | + }); |
| 63 | + }); |
| 64 | +} |
0 commit comments