Dan*_*eny 5 async-await dart flutter
我正在尝试使用 FakeAsync 编写一个测试,但它似乎挂在我的await
s 上。这是一个精简的示例:
test('danny', () async {
await FakeAsync().run((FakeAsync async) async {
print('1');
final a = Future<bool>.delayed(const Duration(seconds: 5))
.then((_) => print('Delayed future completed!'))
.then((_) => true);
print('2');
async.elapse(const Duration(seconds: 30));
// Tried all this too...
// async.flushMicrotasks();
// async.flushTimers();
// async.elapse(const Duration(seconds: 30));
// async.flushMicrotasks();
// async.flushTimers();
// async.elapseBlocking(const Duration(seconds: 30));
print('3');
await a;
print('4');
expect(1, 2);
});
});
Run Code Online (Sandbox Code Playgroud)
此代码输出:
1
2
Delayed future completed!
3
// hangs and never prints '4'
Run Code Online (Sandbox Code Playgroud)
该async.elapse
呼叫被允许完成未来,但它仍然挂起await a
。为什么?
发生这种情况似乎是因为虽然Future
已完成,但await
调用需要处理微任务队列才能继续(但它不能,因为async.elapse
在 后没有人调用await
)。
作为一种解决方法,在函数运行时持续抽取 microstask 队列似乎可行 - 例如调用此函数代替FakeAsync.run
:
/// Runs a callback using FakeAsync.run while continually pumping the
/// microtask queue. This avoids a deadlock when tests `await` a Future
/// which queues a microtask that will not be processed unless the queue
/// is flushed.
Future<T> runFakeAsync<T>(Future<T> Function(FakeAsync time) f) async {
return FakeAsync().run((FakeAsync time) async {
bool pump = true;
final Future<T> future = f(time).whenComplete(() => pump = false);
while (pump) {
time.flushMicrotasks();
}
return future;
}) as Future<T>;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
277 次 |
最近记录: |