|
1 | 1 | import 'dart:async'; |
2 | | -import 'dart:convert'; |
3 | 2 |
|
4 | | -import 'package:http/http.dart' as http; |
| 3 | +import 'package:powersync/powersync.dart'; |
5 | 4 | import 'package:test/test.dart'; |
6 | 5 |
|
7 | 6 | import 'server/sync_server/mock_sync_server.dart'; |
| 7 | +import 'streaming_sync_test.dart'; |
| 8 | +import 'utils/abstract_test_utils.dart'; |
| 9 | +import 'utils/test_utils_impl.dart'; |
| 10 | + |
| 11 | +final testUtils = TestUtils(); |
8 | 12 |
|
9 | 13 | void main() { |
10 | 14 | late TestHttpServerHelper testServer; |
| 15 | + late String path; |
11 | 16 |
|
12 | 17 | setUp(() async { |
| 18 | + path = testUtils.dbPath(); |
13 | 19 | testServer = TestHttpServerHelper(); |
14 | 20 | await testServer.start(); |
15 | 21 | }); |
16 | 22 |
|
17 | 23 | tearDown(() async { |
| 24 | + await testUtils.cleanDb(path: path); |
18 | 25 | await testServer.stop(); |
19 | 26 | }); |
20 | 27 |
|
21 | | - test('should receive events from the sync stream without waiting for close', |
22 | | - () async { |
23 | | - final client = http.Client(); |
24 | | - final request = |
25 | | - http.Request('POST', testServer.uri.replace(path: '/sync/stream')); |
26 | | - request.headers['Content-Type'] = 'application/json'; |
27 | | - |
28 | | - // Send the request and get the response stream |
29 | | - final responseStream = await client.send(request); |
30 | | - |
31 | | - final expectedEvents = ['event1', 'event2', 'event3']; |
32 | | - final receivedEvents = <String>[]; |
33 | | - final completer = Completer<void>(); |
34 | | - |
35 | | - // Listen to the response stream for real-time processing of incoming events |
36 | | - final subscription = responseStream.stream |
37 | | - .transform(utf8.decoder) |
38 | | - .transform(LineSplitter()) |
39 | | - .listen( |
40 | | - (event) { |
41 | | - receivedEvents.add(event); |
42 | | - if (receivedEvents.length == expectedEvents.length) { |
43 | | - completer.complete(); // Complete once all events are received |
44 | | - } |
45 | | - }, |
46 | | - onError: (e) => completer.completeError(e), |
47 | | - ); |
48 | | - |
49 | | - // Programmatically trigger events on the server |
50 | | - for (final event in expectedEvents) { |
51 | | - testServer.addEvent('$event\n'); |
52 | | - await Future.delayed( |
53 | | - Duration(milliseconds: 100)); // Small delay for each event |
54 | | - } |
55 | | - |
56 | | - // Wait for the events to be received |
57 | | - await completer.future.timeout(Duration(seconds: 5)); |
58 | | - await subscription.cancel(); |
59 | | - client.close(); |
60 | | - |
61 | | - expect(receivedEvents.toSet().containsAll(expectedEvents.toSet()), isTrue); |
| 28 | + test('should connect to mock PowerSync instance', () async { |
| 29 | + final connector = TestConnector(() async { |
| 30 | + return PowerSyncCredentials( |
| 31 | + endpoint: testServer.uri.toString(), |
| 32 | + token: 'token not used here', |
| 33 | + expiresAt: DateTime.now()); |
| 34 | + }); |
| 35 | + |
| 36 | + final db = PowerSyncDatabase.withFactory( |
| 37 | + await testUtils.testFactory(path: path), |
| 38 | + schema: defaultSchema, |
| 39 | + maxReaders: 3); |
| 40 | + await db.initialize(); |
| 41 | + |
| 42 | + final connectedCompleter = Completer(); |
| 43 | + |
| 44 | + db.statusStream.listen((status) { |
| 45 | + if (status.connected) { |
| 46 | + connectedCompleter.complete(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // Add a basic command for the test server to send |
| 51 | + testServer.addEvent('{"token_expires_in": 3600}\n'); |
| 52 | + |
| 53 | + await db.connect(connector: connector); |
| 54 | + await connectedCompleter.future; |
| 55 | + |
| 56 | + expect(db.connected, isTrue); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should trigger uploads when connection is established', () async { |
| 60 | + int uploadCounter = 0; |
| 61 | + Completer uploadTriggeredCompleter = Completer(); |
| 62 | + |
| 63 | + final connector = TestConnector(() async { |
| 64 | + return PowerSyncCredentials( |
| 65 | + endpoint: testServer.uri.toString(), |
| 66 | + token: 'token not used here', |
| 67 | + expiresAt: DateTime.now()); |
| 68 | + }, uploadData: (database) async { |
| 69 | + uploadCounter++; |
| 70 | + uploadTriggeredCompleter.complete(); |
| 71 | + throw Exception('No uploads occur here'); |
| 72 | + }); |
| 73 | + |
| 74 | + final db = PowerSyncDatabase.withFactory( |
| 75 | + await testUtils.testFactory(path: path), |
| 76 | + schema: defaultSchema, |
| 77 | + maxReaders: 3); |
| 78 | + await db.initialize(); |
| 79 | + |
| 80 | + // Create an item which should trigger an upload. |
| 81 | + await db.execute( |
| 82 | + 'INSERT INTO customers (id, name) VALUES (uuid(), ?)', ['steven']); |
| 83 | + |
| 84 | + // Create a new completer to await the next upload |
| 85 | + uploadTriggeredCompleter = Completer(); |
| 86 | + |
| 87 | + // Connect the PowerSync instance |
| 88 | + final connectedCompleter = Completer(); |
| 89 | + // The first connection attempt will fail |
| 90 | + final connectedErroredCompleter = Completer(); |
| 91 | + |
| 92 | + db.statusStream.listen((status) { |
| 93 | + // print('status updated: ${status.connected}, ${status.downloadError}'); |
| 94 | + if (status.connected) { |
| 95 | + connectedCompleter.complete(); |
| 96 | + } |
| 97 | + if (status.downloadError != null && |
| 98 | + !connectedErroredCompleter.isCompleted) { |
| 99 | + connectedErroredCompleter.complete(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // The first command will not be valid, this simulates a failed connection |
| 104 | + testServer.addEvent('asdf\n'); |
| 105 | + await db.connect(connector: connector); |
| 106 | + |
| 107 | + // The connect operation should have triggered an upload (even though it fails to connect) |
| 108 | + await uploadTriggeredCompleter.future; |
| 109 | + expect(uploadCounter, equals(1)); |
| 110 | + // Create a new completer for the next iteration |
| 111 | + uploadTriggeredCompleter = Completer(); |
| 112 | + |
| 113 | + // Connection attempt should initially fail |
| 114 | + await connectedErroredCompleter.future; |
| 115 | + expect(db.currentStatus.anyError, isNotNull); |
| 116 | + |
| 117 | + // Now send a valid command. Which will result in successful connection |
| 118 | + await testServer.clearEvents(); |
| 119 | + testServer.addEvent('{"token_expires_in": 3600}\n'); |
| 120 | + await connectedCompleter.future; |
| 121 | + expect(db.connected, isTrue); |
| 122 | + |
| 123 | + await uploadTriggeredCompleter.future; |
| 124 | + expect(uploadCounter, equals(2)); |
62 | 125 | }); |
63 | 126 | } |
0 commit comments