From 74567a3e7fb4fccfb9ab5875a694a445305ed4e9 Mon Sep 17 00:00:00 2001 From: Ivar Derksen Date: Tue, 20 Feb 2024 17:45:28 +0100 Subject: [PATCH] Test: fix failing integration tests due to lagging bridge events --- integration_test/irma_binding.dart | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/integration_test/irma_binding.dart b/integration_test/irma_binding.dart index 6f8a47eb..4515b0e7 100644 --- a/integration_test/irma_binding.dart +++ b/integration_test/irma_binding.dart @@ -40,13 +40,17 @@ class IntegrationTestIrmaBinding { _preferences ??= await IrmaPreferences.fromInstance(); _bridge.dispatch(AppReadyEvent()); - EnrollmentStatusEvent currEnrollmentStatus = await _expectBridgeEventGuarded(); + EnrollmentStatusEvent currEnrollmentStatus = await _expectBridgeEventGuarded( + (event) => event is ClientPreferencesEvent, + ); // Ensure the app is not enrolled to its keyshare server yet. if (currEnrollmentStatus.enrolledSchemeManagerIds.isNotEmpty) { await tearDown(); _bridge.dispatch(AppReadyEvent()); - currEnrollmentStatus = await _expectBridgeEventGuarded(); + currEnrollmentStatus = await _expectBridgeEventGuarded( + (event) => event is ClientPreferencesEvent, + ); } // Ensure test scheme is available. @@ -99,22 +103,24 @@ class IntegrationTestIrmaBinding { await _repository?.close(); _repository = null; // Make sure there is a listener for the bridge events. - final dataClearedFuture = - _expectBridgeEventGuarded((event) => event.enrolledSchemeManagerIds.isEmpty); + final dataClearedFuture = _expectBridgeEventGuarded( + (event) => event is EnrollmentStatusEvent && event.enrolledSchemeManagerIds.isEmpty, + ); _bridge.dispatch(ClearAllDataEvent()); await _preferences?.clearAll(); await dataClearedFuture; } - /// Returns the first bridge event that matches the given event type and test conditions. + /// Takes bridge events until an event is received that matches the given test conditions and + /// returns the closest event in time that matches the expected return type. /// The bridge event stream is guarded while waiting to detect relevant errors. - Future _expectBridgeEventGuarded([bool Function(T)? test]) => _bridge.events - .where((event) { + Future _expectBridgeEventGuarded([bool Function(Event)? test]) => _bridge.events + .takeWhileInclusive((event) { if (event is ErrorEvent) throw Exception(event.toString()); if (event is EnrollmentFailureEvent) throw Exception(event.error); - return event is T; + if (test == null) return event is! T; + return !test(event); }) - .whereType() - .where(test ?? (_) => true) - .first; + .toList() + .then((receivedEvents) => receivedEvents.whereType().last); }