@@ -176,6 +176,7 @@ void main() {
176176 });
177177
178178 test ('SyncClient data sync' , () async {
179+ await server.online ();
179180 loggedInClient (env.store);
180181 loggedInClient (env2.store);
181182
@@ -201,6 +202,7 @@ void main() {
201202 final events2 = < SyncConnectionEvent > [];
202203 final streamSub2 = client.connectionEvents.listen (events2.add);
203204
205+ await server.online ();
204206 client.start ();
205207
206208 expect (waitUntil (() => client.state () == SyncState .loggedIn), isTrue);
@@ -223,6 +225,7 @@ void main() {
223225 ]));
224226
225227 await server.start (keepDb: true );
228+ await server.online ();
226229
227230 expect (waitUntil (() => client.state () == SyncState .loggedIn), isTrue);
228231 await yieldExecution ();
@@ -249,9 +252,10 @@ void main() {
249252 final events = < SyncLoginEvent > [];
250253 client.loginEvents.listen (events.add);
251254
255+ await server.online ();
252256 client.start ();
253257
254- expect (await client.loginEvents.first.timeout (Duration (seconds : 1 ) ),
258+ expect (await client.loginEvents.first.timeout (defaultTimeout ),
255259 equals (SyncLoginEvent .credentialsRejected));
256260
257261 client.setCredentials (SyncCredentials .none ());
@@ -267,6 +271,7 @@ void main() {
267271 });
268272
269273 test ('SyncClient listeners: completion' , () async {
274+ await server.online ();
270275 final client = loggedInClient (store);
271276 expect (env.box.isEmpty (), isTrue);
272277 int id = env.box.put (TestEntity (tLong: 100 ));
@@ -277,13 +282,14 @@ void main() {
277282 client.close ();
278283
279284 final client2 = loggedInClient (env2.store);
280- await client2.completionEvents.first.timeout (Duration (seconds : 1 ) );
285+ await client2.completionEvents.first.timeout (defaultTimeout );
281286 client2.close ();
282287
283288 expect (env2.box.get (id) /*!*/ .tLong, 100 );
284289 });
285290
286291 test ('SyncClient listeners: changes' , () async {
292+ await server.online ();
287293 final client = loggedInClient (store);
288294 final client2 = loggedInClient (env2.store);
289295
@@ -349,7 +355,7 @@ void main() {
349355class SyncServer {
350356 Directory /*?*/ dir;
351357 int /*?*/ port;
352- Process /*?*/ process;
358+ Future < Process > /*?*/ process;
353359
354360 static bool isAvailable () {
355361 try {
@@ -367,7 +373,7 @@ class SyncServer {
367373 dir ?? = Directory ('testdata-sync-server-$port ' );
368374 if (! keepDb) _deleteDb ();
369375
370- process = await Process .start ('sync-server' , [
376+ process = Process .start ('sync-server' , [
371377 '--unsecured-no-authentication' ,
372378 '--db-directory=${dir .path }' ,
373379 '--model=${Directory .current .path }/test/objectbox-model.json' ,
@@ -376,9 +382,25 @@ class SyncServer {
376382 ]);
377383 }
378384
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+
379401 void stop ({bool keepDb = false }) async {
380402 if (process == null ) return ;
381- final proc = process /*!*/ ;
403+ final proc = await process /*!*/ ;
382404 process = null ;
383405 proc.kill (ProcessSignal .sigint);
384406 final exitCode = await proc.exitCode;
0 commit comments