Visitar URL original
fix(sql_connect): e2e listen test (#18496) · firebase/flutterfire@e512d39 · GitHub
Skip to content

Commit e512d39

Browse files
fix(sql_connect): e2e listen test (#18496)
1 parent 361f719 commit e512d39

1 file changed

Lines changed: 69 additions & 55 deletions

File tree

  • packages/firebase_data_connect/firebase_data_connect/example/integration_test

‎packages/firebase_data_connect/firebase_data_connect/example/integration_test/listen_e2e.dart‎

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -87,76 +87,90 @@ void runListenTests() {
8787
expect(initialValue.data.movies.length, 0,
8888
reason: 'Initial movie list should be empty');
8989

90-
final Completer<void> isReady = Completer<void>();
91-
final Completer<bool> hasBeenListened = Completer<bool>();
92-
int count = 0;
90+
final listener1Ready = Completer<void>();
91+
final listener2Ready = Completer<void>();
92+
final listener1ReceivedUpdate = Completer<void>();
93+
final listener2ReceivedUpdate = Completer<void>();
94+
95+
int count1 = 0;
96+
int count2 = 0;
9397

9498
final listener1 = MoviesConnector.instance
9599
.listMovies()
96100
.ref()
97101
.subscribe()
98102
.listen((value) {
99-
final movies = value.data.movies;
100-
101-
if (count == 0) {
102-
expect(movies.length, 0,
103-
reason: 'First emission should contain an empty list');
104-
isReady.complete();
105-
} else {
106-
expect(movies.length, 1,
107-
reason: 'Second emission should contain one movie');
108-
expect(movies[0].title, 'The Matrix',
109-
reason: 'The movie should be The Matrix');
110-
hasBeenListened.complete(true);
103+
count1++;
104+
if (count1 == 1 && !listener1Ready.isCompleted) {
105+
listener1Ready.complete();
106+
} else if (count1 == 2 && !listener1ReceivedUpdate.isCompleted) {
107+
listener1ReceivedUpdate.complete();
111108
}
112-
count++;
113109
});
114-
int listener2Count = 0;
110+
115111
final listener2 = MoviesConnector.instance
116112
.listMovies()
117113
.ref()
118114
.subscribe()
119115
.listen((value) {
120-
listener2Count++;
116+
count2++;
117+
if (count2 == 1 && !listener2Ready.isCompleted) {
118+
listener2Ready.complete();
119+
} else if (count2 == 3 && !listener2ReceivedUpdate.isCompleted) {
120+
listener2ReceivedUpdate.complete();
121+
}
121122
});
122123

123-
// Wait for the listener to be ready
124-
await isReady.future;
125-
126-
// Create the movie
127-
await MoviesConnector.instance
128-
.createMovie(
129-
genre: 'Action',
130-
title: 'The Matrix',
131-
releaseYear: 1999,
132-
)
133-
.rating(4.5)
134-
.ref()
135-
.execute();
136-
137-
await MoviesConnector.instance.listMovies().ref().execute();
138-
139-
// Wait for the listener to receive the movie update
140-
final bool hasListenerReceived = await hasBeenListened.future;
141-
142-
// Cancel the listener and wait for it to finish
143-
await listener1.cancel();
144-
expect(hasListenerReceived, isTrue,
145-
reason: 'The stream should have emitted new data');
146-
// Create the movie
147-
await MoviesConnector.instance
148-
.createMovie(
149-
genre: 'Adventure',
150-
title: 'Raiders of the Lost Arc',
151-
releaseYear: 1999,
152-
)
153-
.rating(4.5)
154-
.ref()
155-
.execute();
156-
await Future.delayed(const Duration(seconds: 5));
157-
expect(count, equals(2));
158-
expect(listener2Count, equals(3));
159-
await listener2.cancel();
124+
try {
125+
// Wait for both listeners to be ready with initial emission
126+
await Future.wait([
127+
listener1Ready.future,
128+
listener2Ready.future,
129+
]).timeout(_listenTimeout);
130+
131+
// Create first movie
132+
await MoviesConnector.instance
133+
.createMovie(
134+
genre: 'Action',
135+
title: 'The Matrix',
136+
releaseYear: 1999,
137+
)
138+
.rating(4.5)
139+
.ref()
140+
.execute();
141+
142+
await MoviesConnector.instance.listMovies().ref().execute();
143+
144+
// Wait for listener1 to receive the update
145+
await listener1ReceivedUpdate.future.timeout(_listenTimeout);
146+
147+
// Cancel listener1
148+
await listener1.cancel();
149+
150+
// Create second movie
151+
await MoviesConnector.instance
152+
.createMovie(
153+
genre: 'Adventure',
154+
title: 'Raiders of the Lost Arc',
155+
releaseYear: 1999,
156+
)
157+
.rating(4.5)
158+
.ref()
159+
.execute();
160+
161+
await MoviesConnector.instance.listMovies().ref().execute();
162+
163+
// Wait deterministically for listener2's 3rd emission
164+
await listener2ReceivedUpdate.future.timeout(_listenTimeout);
165+
166+
expect(count1, equals(2),
167+
reason: 'Canceled listener should not receive further updates');
168+
expect(count2, equals(3),
169+
reason: 'Active listener should receive all updates');
170+
} finally {
171+
await listener1.cancel();
172+
await listener2.cancel();
173+
}
160174
});
161175
},
162176
);

0 commit comments

Comments
 (0)