@@ -176,6 +176,7 @@ void main() {
176
176
});
177
177
178
178
test ('SyncClient data sync' , () async {
179
+ await server.online ();
179
180
loggedInClient (env.store);
180
181
loggedInClient (env2.store);
181
182
@@ -201,6 +202,7 @@ void main() {
201
202
final events2 = < SyncConnectionEvent > [];
202
203
final streamSub2 = client.connectionEvents.listen (events2.add);
203
204
205
+ await server.online ();
204
206
client.start ();
205
207
206
208
expect (waitUntil (() => client.state () == SyncState .loggedIn), isTrue);
@@ -223,6 +225,7 @@ void main() {
223
225
]));
224
226
225
227
await server.start (keepDb: true );
228
+ await server.online ();
226
229
227
230
expect (waitUntil (() => client.state () == SyncState .loggedIn), isTrue);
228
231
await yieldExecution ();
@@ -249,9 +252,10 @@ void main() {
249
252
final events = < SyncLoginEvent > [];
250
253
client.loginEvents.listen (events.add);
251
254
255
+ await server.online ();
252
256
client.start ();
253
257
254
- expect (await client.loginEvents.first.timeout (Duration (seconds : 1 ) ),
258
+ expect (await client.loginEvents.first.timeout (defaultTimeout ),
255
259
equals (SyncLoginEvent .credentialsRejected));
256
260
257
261
client.setCredentials (SyncCredentials .none ());
@@ -267,6 +271,7 @@ void main() {
267
271
});
268
272
269
273
test ('SyncClient listeners: completion' , () async {
274
+ await server.online ();
270
275
final client = loggedInClient (store);
271
276
expect (env.box.isEmpty (), isTrue);
272
277
int id = env.box.put (TestEntity (tLong: 100 ));
@@ -277,13 +282,14 @@ void main() {
277
282
client.close ();
278
283
279
284
final client2 = loggedInClient (env2.store);
280
- await client2.completionEvents.first.timeout (Duration (seconds : 1 ) );
285
+ await client2.completionEvents.first.timeout (defaultTimeout );
281
286
client2.close ();
282
287
283
288
expect (env2.box.get (id) /*!*/ .tLong, 100 );
284
289
});
285
290
286
291
test ('SyncClient listeners: changes' , () async {
292
+ await server.online ();
287
293
final client = loggedInClient (store);
288
294
final client2 = loggedInClient (env2.store);
289
295
@@ -349,7 +355,7 @@ void main() {
349
355
class SyncServer {
350
356
Directory /*?*/ dir;
351
357
int /*?*/ port;
352
- Process /*?*/ process;
358
+ Future < Process > /*?*/ process;
353
359
354
360
static bool isAvailable () {
355
361
try {
@@ -367,7 +373,7 @@ class SyncServer {
367
373
dir ?? = Directory ('testdata-sync-server-$port ' );
368
374
if (! keepDb) _deleteDb ();
369
375
370
- process = await Process .start ('sync-server' , [
376
+ process = Process .start ('sync-server' , [
371
377
'--unsecured-no-authentication' ,
372
378
'--db-directory=${dir .path }' ,
373
379
'--model=${Directory .current .path }/test/objectbox-model.json' ,
@@ -376,9 +382,25 @@ class SyncServer {
376
382
]);
377
383
}
378
384
385
+ /// Wait for the server to respond to a simple http request.
386
+ /// This simple check speeds up test by only trying to log in after the server
387
+ /// has started, avoiding the reconnect backoff intervals altogether.
388
+ Future <void > online () async => Future (() async {
389
+ while (true ) {
390
+ try {
391
+ await HttpClient ().get ('127.0.0.1' , port, '' );
392
+ break ;
393
+ } on SocketException catch (e) {
394
+ // only retry if "connection refused"
395
+ if (e.osError.errorCode != 111 ) rethrow ;
396
+ await Future <void >.delayed (Duration (milliseconds: 1 ));
397
+ }
398
+ }
399
+ }).timeout (defaultTimeout);
400
+
379
401
void stop ({bool keepDb = false }) async {
380
402
if (process == null ) return ;
381
- final proc = process /*!*/ ;
403
+ final proc = await process /*!*/ ;
382
404
process = null ;
383
405
proc.kill (ProcessSignal .sigint);
384
406
final exitCode = await proc.exitCode;
0 commit comments