@@ -14,115 +14,128 @@ import 'utils/test_utils_impl.dart';
14
14
final testUtils = TestUtils ();
15
15
16
16
void main () {
17
- late TestHttpServerHelper testServer;
18
- late String path;
19
-
20
- setUp (() async {
21
- path = testUtils.dbPath ();
22
- testServer = TestHttpServerHelper ();
23
- await testServer.start ();
24
- });
25
-
26
- tearDown (() async {
27
- await testUtils.cleanDb (path: path);
28
- await testServer.stop ();
29
- });
30
-
31
- test ('should connect to mock PowerSync instance' , () async {
32
- final connector = TestConnector (() async {
33
- return PowerSyncCredentials (
34
- endpoint: testServer.uri.toString (),
35
- token: 'token not used here' ,
36
- expiresAt: DateTime .now ());
17
+ group ('connected tests' , () {
18
+ late String path;
19
+ setUp (() async {
20
+ path = testUtils.dbPath ();
37
21
});
38
22
39
- final db = PowerSyncDatabase .withFactory (
40
- await testUtils.testFactory (path: path),
41
- schema: defaultSchema,
42
- maxReaders: 3 );
43
- await db.initialize ();
44
-
45
- final connectedCompleter = Completer ();
46
-
47
- db.statusStream.listen ((status) {
48
- if (status.connected) {
49
- connectedCompleter.complete ();
50
- }
23
+ tearDown (() async {
24
+ // await testUtils.cleanDb(path: path);
51
25
});
52
26
53
- // Add a basic command for the test server to send
54
- testServer.addEvent ('{"token_expires_in": 3600}\n ' );
55
-
56
- await db.connect (connector: connector);
57
- await connectedCompleter.future;
58
-
59
- expect (db.connected, isTrue);
60
- });
61
-
62
- test ('should trigger uploads when connection is re-established' , () async {
63
- int uploadCounter = 0 ;
64
- Completer uploadTriggeredCompleter = Completer ();
65
-
66
- final connector = TestConnector (() async {
67
- return PowerSyncCredentials (
68
- endpoint: testServer.uri.toString (),
69
- token: 'token not used here' ,
70
- expiresAt: DateTime .now ());
71
- }, uploadData: (database) async {
72
- uploadCounter++ ;
73
- uploadTriggeredCompleter.complete ();
74
- throw Exception ('No uploads occur here' );
27
+ createTestServer () async {
28
+ final testServer = TestHttpServerHelper ();
29
+ await testServer.start ();
30
+ return testServer;
31
+ }
32
+
33
+ test ('should connect to mock PowerSync instance' , () async {
34
+ final testServer = await createTestServer ();
35
+ // Cant do this inside the createTestServer function unfortunately
36
+ addTearDown (() => testServer.stop ());
37
+
38
+ final connector = TestConnector (() async {
39
+ return PowerSyncCredentials (
40
+ endpoint: testServer.uri.toString (),
41
+ token: 'token not used here' ,
42
+ expiresAt: DateTime .now ());
43
+ });
44
+
45
+ final db = PowerSyncDatabase .withFactory (
46
+ await testUtils.testFactory (path: path),
47
+ schema: defaultSchema,
48
+ maxReaders: 3 );
49
+ await db.initialize ();
50
+
51
+ final connectedCompleter = Completer ();
52
+
53
+ db.statusStream.listen ((status) {
54
+ if (status.connected) {
55
+ connectedCompleter.complete ();
56
+ }
57
+ });
58
+
59
+ // Add a basic command for the test server to send
60
+ testServer.addEvent ('{"token_expires_in": 3600}\n ' );
61
+
62
+ await db.connect (connector: connector);
63
+ await connectedCompleter.future;
64
+
65
+ expect (db.connected, isTrue);
66
+ await db.disconnect ();
75
67
});
76
68
77
- final db = PowerSyncDatabase .withFactory (
78
- await testUtils.testFactory (path: path),
79
- schema: defaultSchema,
80
- maxReaders: 3 );
81
- await db.initialize ();
82
-
83
- // Create an item which should trigger an upload.
84
- await db.execute (
85
- 'INSERT INTO customers (id, name) VALUES (uuid(), ?)' , ['steven' ]);
86
-
87
- // Create a new completer to await the next upload
88
- uploadTriggeredCompleter = Completer ();
89
-
90
- // Connect the PowerSync instance
91
- final connectedCompleter = Completer ();
92
- // The first connection attempt will fail
93
- final connectedErroredCompleter = Completer ();
94
-
95
- db.statusStream.listen ((status) {
96
- if (status.connected) {
97
- connectedCompleter.complete ();
98
- }
99
- if (status.downloadError != null &&
100
- ! connectedErroredCompleter.isCompleted) {
101
- connectedErroredCompleter.complete ();
102
- }
69
+ test ('should trigger uploads when connection is re-established' , () async {
70
+ int uploadCounter = 0 ;
71
+ Completer uploadTriggeredCompleter = Completer ();
72
+ final testServer = await createTestServer ();
73
+ // Cant do this inside the createTestServer function unfortunately
74
+ addTearDown (() => testServer.stop ());
75
+
76
+ final connector = TestConnector (() async {
77
+ return PowerSyncCredentials (
78
+ endpoint: testServer.uri.toString (),
79
+ token: 'token not used here' ,
80
+ expiresAt: DateTime .now ());
81
+ }, uploadData: (database) async {
82
+ uploadCounter++ ;
83
+ uploadTriggeredCompleter.complete ();
84
+ throw Exception ('No uploads occur here' );
85
+ });
86
+
87
+ final db = PowerSyncDatabase .withFactory (
88
+ await testUtils.testFactory (path: path),
89
+ schema: defaultSchema,
90
+ maxReaders: 3 );
91
+ await db.initialize ();
92
+
93
+ // Create an item which should trigger an upload.
94
+ await db.execute (
95
+ 'INSERT INTO customers (id, name) VALUES (uuid(), ?)' , ['steven' ]);
96
+
97
+ // Create a new completer to await the next upload
98
+ uploadTriggeredCompleter = Completer ();
99
+
100
+ // Connect the PowerSync instance
101
+ final connectedCompleter = Completer ();
102
+ // The first connection attempt will fail
103
+ final connectedErroredCompleter = Completer ();
104
+
105
+ db.statusStream.listen ((status) {
106
+ if (status.connected && ! connectedCompleter.isCompleted) {
107
+ connectedCompleter.complete ();
108
+ }
109
+ if (status.downloadError != null &&
110
+ ! connectedErroredCompleter.isCompleted) {
111
+ connectedErroredCompleter.complete ();
112
+ }
113
+ });
114
+
115
+ // The first command will not be valid, this simulates a failed connection
116
+ testServer.addEvent ('asdf\n ' );
117
+ await db.connect (connector: connector);
118
+
119
+ // The connect operation should have triggered an upload (even though it fails to connect)
120
+ await uploadTriggeredCompleter.future;
121
+ expect (uploadCounter, equals (1 ));
122
+ // Create a new completer for the next iteration
123
+ uploadTriggeredCompleter = Completer ();
124
+
125
+ // Connection attempt should initially fail
126
+ await connectedErroredCompleter.future;
127
+ expect (db.currentStatus.anyError, isNotNull);
128
+
129
+ // Now send a valid command. Which will result in successful connection
130
+ await testServer.clearEvents ();
131
+ testServer.addEvent ('{"token_expires_in": 3600}\n ' );
132
+ await connectedCompleter.future;
133
+ expect (db.connected, isTrue);
134
+
135
+ await uploadTriggeredCompleter.future;
136
+ expect (uploadCounter, equals (2 ));
137
+
138
+ await db.disconnect ();
103
139
});
104
-
105
- // The first command will not be valid, this simulates a failed connection
106
- testServer.addEvent ('asdf\n ' );
107
- await db.connect (connector: connector);
108
-
109
- // The connect operation should have triggered an upload (even though it fails to connect)
110
- await uploadTriggeredCompleter.future;
111
- expect (uploadCounter, equals (1 ));
112
- // Create a new completer for the next iteration
113
- uploadTriggeredCompleter = Completer ();
114
-
115
- // Connection attempt should initially fail
116
- await connectedErroredCompleter.future;
117
- expect (db.currentStatus.anyError, isNotNull);
118
-
119
- // Now send a valid command. Which will result in successful connection
120
- await testServer.clearEvents ();
121
- testServer.addEvent ('{"token_expires_in": 3600}\n ' );
122
- await connectedCompleter.future;
123
- expect (db.connected, isTrue);
124
-
125
- await uploadTriggeredCompleter.future;
126
- expect (uploadCounter, equals (2 ));
127
140
});
128
141
}
0 commit comments