From e65a399fe772273726ac7c75c6a7276ae1cb1f12 Mon Sep 17 00:00:00 2001 From: Joonas Kerttula Date: Wed, 19 Nov 2025 16:27:56 +0200 Subject: [PATCH 1/2] test: fix failing tests caused by isolate tests --- .../integration_test/t09_isolates_test.dart | 78 +++++++++++-------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/example/integration_test/t09_isolates_test.dart b/example/integration_test/t09_isolates_test.dart index f53b999..b4d24e2 100644 --- a/example/integration_test/t09_isolates_test.dart +++ b/example/integration_test/t09_isolates_test.dart @@ -32,44 +32,56 @@ void main() { final RootIsolateToken rootIsolateToken = RootIsolateToken.instance!; const int numIsolates = 3; final List receivePorts = []; + final List isolates = []; - for (int i = 0; i < numIsolates; i++) { - final ReceivePort receivePort = ReceivePort(); - receivePorts.add(receivePort); + try { + for (int i = 0; i < numIsolates; i++) { + final ReceivePort receivePort = ReceivePort(); + receivePorts.add(receivePort); - await Isolate.spawn( - _isolateVersionCheckMain, - _IsolateData( - rootIsolateToken: rootIsolateToken, - sendPort: receivePort.sendPort, - ), - ); - } + final Isolate isolate = await Isolate.spawn( + _isolateVersionCheckMain, + _IsolateData( + rootIsolateToken: rootIsolateToken, + sendPort: receivePort.sendPort, + ), + ); + isolates.add(isolate); + } - final List<_IsolateResult> results = []; - for (final receivePort in receivePorts) { - final dynamic result = await receivePort.first; - expect(result, isA<_IsolateResult>()); - results.add(result as _IsolateResult); - } + final List<_IsolateResult> results = []; + for (final receivePort in receivePorts) { + final dynamic result = await receivePort.first; + expect(result, isA<_IsolateResult>()); + results.add(result as _IsolateResult); + } - for (int i = 0; i < results.length; i++) { - expect( - results[i].error, - isNull, - reason: 'Isolate $i should not throw an error', - ); - expect(results[i].version, isNotNull); - expect(results[i].version!.length, greaterThan(0)); - } + for (int i = 0; i < results.length; i++) { + expect( + results[i].error, + isNull, + reason: 'Isolate $i should not throw an error', + ); + expect(results[i].version, isNotNull); + expect(results[i].version!.length, greaterThan(0)); + } - final String firstVersion = results[0].version!; - for (int i = 1; i < results.length; i++) { - expect( - results[i].version, - equals(firstVersion), - reason: 'All isolates should return the same SDK version', - ); + final String firstVersion = results[0].version!; + for (int i = 1; i < results.length; i++) { + expect( + results[i].version, + equals(firstVersion), + reason: 'All isolates should return the same SDK version', + ); + } + } finally { + // Clean up resources + for (final receivePort in receivePorts) { + receivePort.close(); + } + for (final isolate in isolates) { + isolate.kill(priority: Isolate.immediate); + } } }, ); From 6a1c23300a266eae4c0752027035c1382a20126c Mon Sep 17 00:00:00 2001 From: Joonas Kerttula Date: Wed, 19 Nov 2025 16:47:52 +0200 Subject: [PATCH 2/2] test: make sure subscriptions ad cleanded on navigation tests --- .../integration_test/t03_navigation_test.dart | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/example/integration_test/t03_navigation_test.dart b/example/integration_test/t03_navigation_test.dart index 5b1e58e..9dd7185 100644 --- a/example/integration_test/t03_navigation_test.dart +++ b/example/integration_test/t03_navigation_test.dart @@ -86,7 +86,8 @@ void main() { await GoogleMapsNavigator.stopGuidance(); } - GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); + final StreamSubscription onArrivalSubscription = + GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); /// Simulate location and test it. await setSimulatedUserLocationWithCheck( @@ -132,9 +133,11 @@ void main() { expectSync(msg.location.longitude, lessThanOrEqualTo(endLng + tolerance)); } - await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener( - onLocationEvent, - ); + final StreamSubscription + roadSnappedSubscription = + await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener( + onLocationEvent, + ); /// Start simulation. await GoogleMapsNavigator.simulator.simulateLocationsAlongExistingRoute(); @@ -143,6 +146,9 @@ void main() { await hasArrived.future; expect(await GoogleMapsNavigator.isGuidanceRunning(), false); + // Cancel subscriptions before cleanup + await onArrivalSubscription.cancel(); + await roadSnappedSubscription.cancel(); await GoogleMapsNavigator.cleanup(); }); @@ -228,7 +234,8 @@ void main() { } } - GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); + final StreamSubscription onArrivalSubscription = + GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); /// Simulate location and test it. await setSimulatedUserLocationWithCheck( @@ -291,9 +298,11 @@ void main() { } } - await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener( - onLocationEvent, - ); + final StreamSubscription + roadSnappedSubscription = + await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener( + onLocationEvent, + ); /// Start simulation. $.log('Starting simulation with speedMultiplier: 5'); @@ -311,6 +320,9 @@ void main() { await navigationFinished.future; expect(await GoogleMapsNavigator.isGuidanceRunning(), false); + // Cancel subscriptions before cleanup + await onArrivalSubscription.cancel(); + await roadSnappedSubscription.cancel(); await GoogleMapsNavigator.cleanup(); }, variant: multipleDestinationsVariants, @@ -471,8 +483,8 @@ void main() { await finishTest.future; $.log('Loop with simulator$loopIteration finished.'); - await GoogleMapsNavigator.cleanup(); await subscription.cancel(); + await GoogleMapsNavigator.cleanup(); await $.pumpAndSettle(); } }); @@ -630,7 +642,13 @@ void main() { expect(GoogleMapsNavigator.isGuidanceRunning(), false); } - GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); + final StreamSubscription onArrivalSubscription = + GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); + + // The subscription will be automatically cleaned up when the test ends. + addTearDown(() async { + await onArrivalSubscription.cancel(); + }); }); patrol('Test error during navigation if no network connection', ( @@ -753,7 +771,8 @@ void main() { hasArrived.complete(); } - GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); + final StreamSubscription onArrivalSubscription = + GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent); /// Simulate location (1298 California St) const double tolerance = 0.001; @@ -850,6 +869,9 @@ void main() { /// Check that the last segment is near target destination. expect(endSegment!.destinationLatLng.longitude, closeTo(-122.412, 0.002)); + + // Cancel subscription before test ends + await onArrivalSubscription.cancel(); }); patrol('Test that the navigation session is attached to existing map', (