Flutter Future.timeout(Duration())颤振中的奇怪行为
我试图使Interactor具有生命周期,不考虑结果,因此我创建了一个抽象类:Flutter Future.timeout(Duration())颤振中的奇怪行为,flutter,Flutter,我试图使Interactor具有生命周期,不考虑结果,因此我创建了一个抽象类: 抽象类交互者{ 流调用(P参数, {Duration timeoutMs=defaultTimeoutMs})异步*{ 试一试{ yield callStarted(); wait-doWork(params).timeout(timeoutMs,onttimeout:()=>throw-TimeoutException(“”); 取得成功(); }关于异常捕获(e){ 产生呼叫者错误(e); } } @保护 未来工
抽象类交互者{
流调用(P参数,
{Duration timeoutMs=defaultTimeoutMs})异步*{
试一试{
yield callStarted();
wait-doWork(params).timeout(timeoutMs,onttimeout:()=>throw-TimeoutException(“”);
取得成功();
}关于异常捕获(e){
产生呼叫者错误(e);
}
}
@保护
未来工作(P参数);
静态const Duration defaultTimeoutMs=持续时间(分钟:5);
}
每个子类都可以扩展它并重写doWork()函数:
类TestTimeOutInteractor扩展了Interactor{
@凌驾
未来工作(int参数,{timeoutMs:const Duration(秒:2)}){
返回未来。延迟(持续时间(秒:6));
}
}
但是没有异常抛出,测试等待6秒并返回成功。
测试:
test('failes timeout exception',()异步{
等待expectLater(TestTimeOutInteractor()(1),
emitsInOrder([callStarted(),callError(TimeoutException(“”)));
});
测试输出为
ERROR: Expected: should do the following in order:
• emit an event that callStarted:<callStarted>
• emit an event that callError:<callError>
Actual: <Instance of '_ControllerStream<callStatus>'>
Which: emitted • callStarted
• callSuccess
x Stream closed.
which didn't emit an event that callError:<callError>
错误:应为:应按顺序执行以下操作:
•发出调用已启动的事件:
•发出调用方错误的事件:
实际:
哪个:已启动
•呼叫成功
x流关闭。
未发出调用方错误的事件:
defaultTimeoutMs
设置为5分钟doWork
在6秒钟内完成,并且没有机会超时。顺便说一句,如果持续时间存储为一个数字,则使用Ms
命名持续时间是有意义的,因为否则您将不知道它代表什么单位,但对于为您处理单位的duration
对象则没有意义。它可以工作。谢谢defaultTimeoutMs
设置为5分钟doWork
在6秒钟内完成,并且没有机会超时。顺便说一句,如果持续时间存储为一个数字,则使用Ms
命名持续时间是有意义的,因为否则您将不知道它代表什么单位,但对于为您处理单位的duration
对象则没有意义。它可以工作。非常感谢。