Skip to content

Commit 8dc5bc8

Browse files
authored
test: improve integration tests stability (#533)
* test: fix failing tests caused by isolate tests * test: make sure subscriptions ad cleanded on navigation tests
1 parent cb7d61e commit 8dc5bc8

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

example/integration_test/t03_navigation_test.dart

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ void main() {
8686
await GoogleMapsNavigator.stopGuidance();
8787
}
8888

89-
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
89+
final StreamSubscription<OnArrivalEvent> onArrivalSubscription =
90+
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
9091

9192
/// Simulate location and test it.
9293
await setSimulatedUserLocationWithCheck(
@@ -132,9 +133,11 @@ void main() {
132133
expectSync(msg.location.longitude, lessThanOrEqualTo(endLng + tolerance));
133134
}
134135

135-
await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener(
136-
onLocationEvent,
137-
);
136+
final StreamSubscription<RoadSnappedLocationUpdatedEvent>
137+
roadSnappedSubscription =
138+
await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener(
139+
onLocationEvent,
140+
);
138141

139142
/// Start simulation.
140143
await GoogleMapsNavigator.simulator.simulateLocationsAlongExistingRoute();
@@ -143,6 +146,9 @@ void main() {
143146
await hasArrived.future;
144147
expect(await GoogleMapsNavigator.isGuidanceRunning(), false);
145148

149+
// Cancel subscriptions before cleanup
150+
await onArrivalSubscription.cancel();
151+
await roadSnappedSubscription.cancel();
146152
await GoogleMapsNavigator.cleanup();
147153
});
148154

@@ -228,7 +234,8 @@ void main() {
228234
}
229235
}
230236

231-
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
237+
final StreamSubscription<OnArrivalEvent> onArrivalSubscription =
238+
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
232239

233240
/// Simulate location and test it.
234241
await setSimulatedUserLocationWithCheck(
@@ -291,9 +298,11 @@ void main() {
291298
}
292299
}
293300

294-
await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener(
295-
onLocationEvent,
296-
);
301+
final StreamSubscription<RoadSnappedLocationUpdatedEvent>
302+
roadSnappedSubscription =
303+
await GoogleMapsNavigator.setRoadSnappedLocationUpdatedListener(
304+
onLocationEvent,
305+
);
297306

298307
/// Start simulation.
299308
$.log('Starting simulation with speedMultiplier: 5');
@@ -311,6 +320,9 @@ void main() {
311320
await navigationFinished.future;
312321
expect(await GoogleMapsNavigator.isGuidanceRunning(), false);
313322

323+
// Cancel subscriptions before cleanup
324+
await onArrivalSubscription.cancel();
325+
await roadSnappedSubscription.cancel();
314326
await GoogleMapsNavigator.cleanup();
315327
},
316328
variant: multipleDestinationsVariants,
@@ -471,8 +483,8 @@ void main() {
471483
await finishTest.future;
472484
$.log('Loop with simulator$loopIteration finished.');
473485

474-
await GoogleMapsNavigator.cleanup();
475486
await subscription.cancel();
487+
await GoogleMapsNavigator.cleanup();
476488
await $.pumpAndSettle();
477489
}
478490
});
@@ -630,7 +642,13 @@ void main() {
630642
expect(GoogleMapsNavigator.isGuidanceRunning(), false);
631643
}
632644

633-
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
645+
final StreamSubscription<OnArrivalEvent> onArrivalSubscription =
646+
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
647+
648+
// The subscription will be automatically cleaned up when the test ends.
649+
addTearDown(() async {
650+
await onArrivalSubscription.cancel();
651+
});
634652
});
635653

636654
patrol('Test error during navigation if no network connection', (
@@ -753,7 +771,8 @@ void main() {
753771
hasArrived.complete();
754772
}
755773

756-
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
774+
final StreamSubscription<OnArrivalEvent> onArrivalSubscription =
775+
GoogleMapsNavigator.setOnArrivalListener(onArrivalEvent);
757776

758777
/// Simulate location (1298 California St)
759778
const double tolerance = 0.001;
@@ -850,6 +869,9 @@ void main() {
850869

851870
/// Check that the last segment is near target destination.
852871
expect(endSegment!.destinationLatLng.longitude, closeTo(-122.412, 0.002));
872+
873+
// Cancel subscription before test ends
874+
await onArrivalSubscription.cancel();
853875
});
854876

855877
patrol('Test that the navigation session is attached to existing map', (

example/integration_test/t09_isolates_test.dart

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,56 @@ void main() {
3232
final RootIsolateToken rootIsolateToken = RootIsolateToken.instance!;
3333
const int numIsolates = 3;
3434
final List<ReceivePort> receivePorts = [];
35+
final List<Isolate> isolates = [];
3536

36-
for (int i = 0; i < numIsolates; i++) {
37-
final ReceivePort receivePort = ReceivePort();
38-
receivePorts.add(receivePort);
37+
try {
38+
for (int i = 0; i < numIsolates; i++) {
39+
final ReceivePort receivePort = ReceivePort();
40+
receivePorts.add(receivePort);
3941

40-
await Isolate.spawn(
41-
_isolateVersionCheckMain,
42-
_IsolateData(
43-
rootIsolateToken: rootIsolateToken,
44-
sendPort: receivePort.sendPort,
45-
),
46-
);
47-
}
42+
final Isolate isolate = await Isolate.spawn(
43+
_isolateVersionCheckMain,
44+
_IsolateData(
45+
rootIsolateToken: rootIsolateToken,
46+
sendPort: receivePort.sendPort,
47+
),
48+
);
49+
isolates.add(isolate);
50+
}
4851

49-
final List<_IsolateResult> results = [];
50-
for (final receivePort in receivePorts) {
51-
final dynamic result = await receivePort.first;
52-
expect(result, isA<_IsolateResult>());
53-
results.add(result as _IsolateResult);
54-
}
52+
final List<_IsolateResult> results = [];
53+
for (final receivePort in receivePorts) {
54+
final dynamic result = await receivePort.first;
55+
expect(result, isA<_IsolateResult>());
56+
results.add(result as _IsolateResult);
57+
}
5558

56-
for (int i = 0; i < results.length; i++) {
57-
expect(
58-
results[i].error,
59-
isNull,
60-
reason: 'Isolate $i should not throw an error',
61-
);
62-
expect(results[i].version, isNotNull);
63-
expect(results[i].version!.length, greaterThan(0));
64-
}
59+
for (int i = 0; i < results.length; i++) {
60+
expect(
61+
results[i].error,
62+
isNull,
63+
reason: 'Isolate $i should not throw an error',
64+
);
65+
expect(results[i].version, isNotNull);
66+
expect(results[i].version!.length, greaterThan(0));
67+
}
6568

66-
final String firstVersion = results[0].version!;
67-
for (int i = 1; i < results.length; i++) {
68-
expect(
69-
results[i].version,
70-
equals(firstVersion),
71-
reason: 'All isolates should return the same SDK version',
72-
);
69+
final String firstVersion = results[0].version!;
70+
for (int i = 1; i < results.length; i++) {
71+
expect(
72+
results[i].version,
73+
equals(firstVersion),
74+
reason: 'All isolates should return the same SDK version',
75+
);
76+
}
77+
} finally {
78+
// Clean up resources
79+
for (final receivePort in receivePorts) {
80+
receivePort.close();
81+
}
82+
for (final isolate in isolates) {
83+
isolate.kill(priority: Isolate.immediate);
84+
}
7385
}
7486
},
7587
);

0 commit comments

Comments
 (0)