gpt4 book ai didi

c# - 如何使用 TestScheduler 完成超时行为?

转载 作者:行者123 更新时间:2023-11-30 16:49:26 25 4
gpt4 key购买 nike

我创建了以下辅助方法来协助测试可观察对象:

    public static void ExecuteObservableTest<T>(IObservable<T> observable, Action action, Action assert, bool expectTimeout, int timeout = 500)
{
Exception ex = null;
var scheduler = new TestScheduler();
observable.Timeout(TimeSpan.FromMilliseconds(timeout)).ObserveOn(scheduler).Subscribe(_ => { }, e => ex = e);
action();
scheduler.AdvanceBy(TimeSpan.FromMilliseconds(timeout).Ticks + 1);
assert();
if (expectTimeout) Assert.IsNotNull(ex);
else Assert.IsNull(ex);
}

只要我不期待超时,一切都会很好。对于我确实期望超时的测试用例,此方法永远不会到达 OnException 方法。使用 TestScheduler 实现此目的的正确方法是什么?

我还应该注意,超时是一个软超时,这意味着如果可观察对象没有在分配的时间内完成,它永远不会完成 - 但在可观察对象本身中没有定义相应的超时。

最佳答案

您需要使用 this overloadTestScheduler 提供给 Timeout

var scheduler = new TestScheduler();
observable.Timeout( TimeSpan.FromMilliseconds(timeout), scheduler)
.Subscribe(_ => { }, e => ex = e);

关于c# - 如何使用 TestScheduler 完成超时行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36700965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com