@@ -102,5 +102,43 @@ void main() {
102
102
}
103
103
await pdb.close ();
104
104
});
105
+
106
+ test ('multiple connect calls' , () async {
107
+ // Test repeatedly creating new PowerSync connections, then disconnect
108
+ // and close the connection.
109
+ final random = Random ();
110
+ var server = await createServer ();
111
+
112
+ credentialsCallback () async {
113
+ return PowerSyncCredentials (endpoint: server.endpoint, token: 'token' );
114
+ }
115
+
116
+ final pdb = await setupPowerSync (path: path);
117
+ pdb.retryDelay = Duration (milliseconds: 5000 );
118
+ var connector = TestConnector (credentialsCallback);
119
+ pdb.connect (connector: connector);
120
+ pdb.connect (connector: connector);
121
+
122
+ final watch = Stopwatch ()..start ();
123
+
124
+ // Wait for at least one connection
125
+ while (server.connectionCount < 1 && watch.elapsedMilliseconds < 500 ) {
126
+ await Future .delayed (Duration (milliseconds: random.nextInt (10 )));
127
+ }
128
+ // Give some time for a second connection if any
129
+ await Future .delayed (Duration (milliseconds: random.nextInt (50 )));
130
+
131
+ await pdb.close ();
132
+
133
+ // Give some time for connections to close
134
+ while (server.connectionCount != 0 && watch.elapsedMilliseconds < 1000 ) {
135
+ await Future .delayed (Duration (milliseconds: random.nextInt (10 )));
136
+ }
137
+
138
+ expect (server.connectionCount, equals (0 ));
139
+ expect (server.maxConnectionCount, equals (1 ));
140
+
141
+ server.close ();
142
+ });
105
143
});
106
144
}
0 commit comments