-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/sync-worker
- Loading branch information
Showing
23 changed files
with
302 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
const String libraryVersion = '1.8.8'; | ||
const String libraryVersion = '1.8.9'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
@TestOn('!browser') | ||
// This test uses a local server which is possible to control in Web via hybrid main, | ||
// but this makes the test significantly more complex. | ||
import 'dart:async'; | ||
|
||
import 'package:powersync/powersync.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'server/sync_server/mock_sync_server.dart'; | ||
import 'streaming_sync_test.dart'; | ||
import 'utils/abstract_test_utils.dart'; | ||
import 'utils/test_utils_impl.dart'; | ||
|
||
final testUtils = TestUtils(); | ||
|
||
void main() { | ||
group('connected tests', () { | ||
late String path; | ||
setUp(() async { | ||
path = testUtils.dbPath(); | ||
}); | ||
|
||
tearDown(() async { | ||
await testUtils.cleanDb(path: path); | ||
}); | ||
|
||
createTestServer() async { | ||
final testServer = TestHttpServerHelper(); | ||
await testServer.start(); | ||
addTearDown(() => testServer.stop()); | ||
return testServer; | ||
} | ||
|
||
test('should connect to mock PowerSync instance', () async { | ||
final testServer = await createTestServer(); | ||
final connector = TestConnector(() async { | ||
return PowerSyncCredentials( | ||
endpoint: testServer.uri.toString(), | ||
token: 'token not used here', | ||
expiresAt: DateTime.now()); | ||
}); | ||
|
||
final db = PowerSyncDatabase.withFactory( | ||
await testUtils.testFactory(path: path), | ||
schema: defaultSchema, | ||
maxReaders: 3); | ||
await db.initialize(); | ||
|
||
final connectedCompleter = Completer(); | ||
|
||
db.statusStream.listen((status) { | ||
if (status.connected) { | ||
connectedCompleter.complete(); | ||
} | ||
}); | ||
|
||
// Add a basic command for the test server to send | ||
testServer.addEvent('{"token_expires_in": 3600}\n'); | ||
|
||
await db.connect(connector: connector); | ||
await connectedCompleter.future; | ||
|
||
expect(db.connected, isTrue); | ||
await db.disconnect(); | ||
}); | ||
|
||
test('should trigger uploads when connection is re-established', () async { | ||
int uploadCounter = 0; | ||
Completer uploadTriggeredCompleter = Completer(); | ||
final testServer = await createTestServer(); | ||
final connector = TestConnector(() async { | ||
return PowerSyncCredentials( | ||
endpoint: testServer.uri.toString(), | ||
token: 'token not used here', | ||
expiresAt: DateTime.now()); | ||
}, uploadData: (database) async { | ||
uploadCounter++; | ||
uploadTriggeredCompleter.complete(); | ||
throw Exception('No uploads occur here'); | ||
}); | ||
|
||
final db = PowerSyncDatabase.withFactory( | ||
await testUtils.testFactory(path: path), | ||
schema: defaultSchema, | ||
maxReaders: 3); | ||
await db.initialize(); | ||
|
||
// Create an item which should trigger an upload. | ||
await db.execute( | ||
'INSERT INTO customers (id, name) VALUES (uuid(), ?)', ['steven']); | ||
|
||
// Create a new completer to await the next upload | ||
uploadTriggeredCompleter = Completer(); | ||
|
||
// Connect the PowerSync instance | ||
final connectedCompleter = Completer(); | ||
// The first connection attempt will fail | ||
final connectedErroredCompleter = Completer(); | ||
|
||
db.statusStream.listen((status) { | ||
if (status.connected && !connectedCompleter.isCompleted) { | ||
connectedCompleter.complete(); | ||
} | ||
if (status.downloadError != null && | ||
!connectedErroredCompleter.isCompleted) { | ||
connectedErroredCompleter.complete(); | ||
} | ||
}); | ||
|
||
// The first command will not be valid, this simulates a failed connection | ||
testServer.addEvent('asdf\n'); | ||
await db.connect(connector: connector); | ||
|
||
// The connect operation should have triggered an upload (even though it fails to connect) | ||
await uploadTriggeredCompleter.future; | ||
expect(uploadCounter, equals(1)); | ||
// Create a new completer for the next iteration | ||
uploadTriggeredCompleter = Completer(); | ||
|
||
// Connection attempt should initially fail | ||
await connectedErroredCompleter.future; | ||
expect(db.currentStatus.anyError, isNotNull); | ||
|
||
// Now send a valid command. Which will result in successful connection | ||
await testServer.clearEvents(); | ||
testServer.addEvent('{"token_expires_in": 3600}\n'); | ||
await connectedCompleter.future; | ||
expect(db.connected, isTrue); | ||
|
||
await uploadTriggeredCompleter.future; | ||
expect(uploadCounter, equals(2)); | ||
|
||
await db.disconnect(); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.