Skip to content

Commit 054a345

Browse files
committed
Sync test deflaking
1 parent 42733fd commit 054a345

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

objectbox/test/isolates_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void main() {
8888
final query = env.box.query().build();
8989
final futureFirst = query.findStream().first; // starts a subscription
9090
expect(await call(['put', 'Bar']), equals(2));
91-
List<TestEntity> found = await futureFirst.timeout(Duration(seconds: 1));
91+
List<TestEntity> found = await futureFirst.timeout(defaultTimeout);
9292
expect(found.length, equals(2));
9393
expect(found.last.tString, equals('Bar'));
9494
query.close();

objectbox/test/observer_test.dart

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ void main() async {
1212
/*late final*/
1313
Box<TestEntity> box;
1414

15-
final defaultTimeout = Duration(milliseconds: 100);
16-
1715
final simpleStringItems = () => <String>[
1816
'One',
1917
'Two',

objectbox/test/sync_test.dart

+27-5
Original file line numberDiff line numberDiff line change
@@ -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() {
349355
class 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;

objectbox/test/test_env.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ class TestEnv {
3333
}
3434
}
3535

36+
const defaultTimeout = Duration(milliseconds: 1000);
37+
3638
/// "Busy-waits" until the predicate returns true.
37-
bool waitUntil(bool Function() predicate, {int timeoutMs = 1000}) {
39+
bool waitUntil(bool Function() predicate, {Duration timeout = defaultTimeout}) {
3840
var success = false;
39-
final until = DateTime.now().millisecondsSinceEpoch + timeoutMs;
41+
final until = DateTime.now().add(timeout);
4042

41-
while (!(success = predicate()) &&
42-
until > DateTime.now().millisecondsSinceEpoch) {
43+
while (!(success = predicate()) && until.isAfter(DateTime.now())) {
4344
sleep(Duration(milliseconds: 1));
4445
}
4546
return success;

0 commit comments

Comments
 (0)