@@ -103,6 +103,16 @@ class _GateEngine implements TtsEngine {
103103 Future <void > dispose () async {}
104104}
105105
106+ /// 等待 fake 引擎收到指定数量的合成请求,避免测试依赖固定 event queue 次数。
107+ Future <void > _waitForGateCount (_GateEngine engine, int count) async {
108+ await expectLater (
109+ Stream <void >.periodic (
110+ const Duration (milliseconds: 1 ),
111+ ).map ((_) => engine.gates.length),
112+ emitsThrough (count),
113+ );
114+ }
115+
106116class _FakeTtsCacheDao implements TtsCacheDao {
107117 @override
108118 Future <TtsCacheData ?> getByKey (String cacheKey, {Duration ? slideTtl}) async =>
@@ -219,8 +229,7 @@ void main() {
219229 final notifier = c.read (ttsControllerProvider.notifier);
220230 await Future <void >(() {}); // 等首次 configure 落定
221231 final first = notifier.speak ('first text' , key: 'same_voice' );
222- await pumpEventQueue ();
223- expect (engine.gates, hasLength (1 ));
232+ await _waitForGateCount (engine, 1 );
224233
225234 final second = notifier.speak ('second text' , key: 'same_voice' );
226235 await pumpEventQueue ();
@@ -229,8 +238,7 @@ void main() {
229238 expect (c.read (ttsControllerProvider).speakingKey, 'same_voice' );
230239
231240 engine.gates[0 ].complete ();
232- await pumpEventQueue ();
233- expect (engine.gates, hasLength (2 ));
241+ await _waitForGateCount (engine, 2 );
234242 await first;
235243 expect (c.read (ttsControllerProvider).speakingKey, 'same_voice' );
236244
0 commit comments