1

我有以下要测试的功能

/// Items are processed asynchronously via fn as they arrive. However
/// if an item arrives before the last asynchronous operation has
/// completed then the cancellation token passed to fn will be
/// triggered enabling the task to be canceled in a best effort
/// way.
public static IObservable<U> SelectWithCancellation<T, U>
    ( this IObservable<T> This
    , Func<CancellationToken, T, Task<U>> fn 
    )
{
    return This
        .Select(v=>Observable.FromAsync(token=>fn(token, v)))
        .Switch();
}

我希望对其进行测试,我能想到的最好的作品如下。首先我创建一个可以取消的长时间运行的任务

public Task<string> JobTask
    ( CancellationToken token
    , string input
    )
{
    return Task.Factory.StartNew(() =>
        {
            if ( input == "C" || input == "E" )
            {
                while ( !token.IsCancellationRequested ) ;
            }
            return input;
        }
    );
}

然后我测试它是否真的可以工作

public class SelectWithCancelationSpec : ReactiveTest
{
    TestScheduler _Scheduler = new TestScheduler();

    [Fact]
    public void ShouldWork()
    {
        var o = _Scheduler.CreateHotObservable
            ( OnNext(100, "A")
            , OnNext(200, "B")
            , OnNext(300, "C")
            , OnNext(400, "D")
            , OnNext(500, "E")
            , OnNext(500, "F")
            );

        List<string> actual = new List<string>();

        o
            .SelectWithCancellation(JobTask)
            .Subscribe(v => actual.Add(v));

        var delay = 100;
        _Scheduler.AdvanceTo(150);
        Thread.Sleep(delay);
        _Scheduler.AdvanceTo(250);
        Thread.Sleep(delay);
        _Scheduler.AdvanceTo(350);
        Thread.Sleep(delay);
        _Scheduler.AdvanceTo(450);
        Thread.Sleep(delay);
        _Scheduler.AdvanceTo(550);
        Thread.Sleep(delay);
        _Scheduler.AdvanceTo(650);


        var expected = new[] { "A", "B", "D", "F" };

        actual
            .ShouldBeEquivalentTo(expected);

    }
}

问题是我不得不引入real time测试。这是因为我模拟的 JobTask 运行在线程池之外的真实线程上,并且不尊重测试调度程序的虚拟时间。发生的情况是,如果我不在AdvanceTo两次调用之间设置延迟,我在测试中丢弃的消息比我预期的要多,因为 JobTask 处理时间太长。

问题是。如何创建一个尊重虚拟时间并允许我测试是否可以成功删除预期消息的 JobTask。

4

1 回答 1

1

关键是创建一个 TestScheduler 知道的滴答事件流。为此,我创建了一个扩展方法

public static class TestSchedulerExtensions
{
    public static IObservable<Unit> CreateTickObserver(this TestScheduler s, int startTick, int endTick, int tickStep)
    {
        var ticks = Enumerable.Repeat(1, Int32.MaxValue)
            .Select(( v, i ) => i * tickStep + startTick)
            .TakeWhile(v => v <= endTick)
            .Select(tick => ReactiveTest.OnNext(tick, Unit.Default));

        return s.CreateColdObservable(ticks.ToArray());

    }  
}

然后是另一个扩展方法来帮助在测试条件下创建任务

    public static Func<CancellationToken,U,Task<T>>
        AsyncSelectorFactory<T, U>
        ( this TestScheduler s
        , int duration
        , int interval
        , Func<CancellationToken, U, IObservable<Unit>, Task<T>> fn 
        )
    {
        var ticker = s.CreateTickObserver(0, duration, interval);
        return ( c, u ) =>
        {
            return fn(c, u, ticker);
        };
    }

TaskFactory 生成的函数可以生成任务,但在测试调度程序的控制下通过代码传递。该代码可用于导致延误或其他事情。

请注意,我们在 _Ticker 来源的 observable 上等待以在任务中创建延迟。现在我们的测试用例看起来像

现在测试很简单

public class SelectWithCancelationSpec : ReactiveTest
{
    TestScheduler _Scheduler = new TestScheduler();

    [Fact]
    public void ShouldWork()
    {

        var o = _Scheduler.CreateColdObservable
            ( OnNext(100, "A")
            , OnNext(200, "B")
            , OnNext(300, "C")
            , OnNext(400, "D")
            , OnNext(500, "E")
            , OnNext(600, "F")
            );

        int cancelCount = 0;
        var job = _Scheduler.AsyncSelectorFactory<string,string>
            ( 1000
            , 10
            , async ( token, input, ticker ) => { 
                if ( input == "C" || input == "E" )
                {
                    await ticker.TakeWhile(v => !token.IsCancellationRequested);
                    cancelCount++;
                }
                return input;
            });


        var actual = _Scheduler.Start(() =>
        {
            return o.SelectWithCancellation(job);
        }
        , created: 0
        , subscribed: 1
        , disposed: 1000
        );

        var expected = new[] { "A", "B", "D", "F" };

        cancelCount.Should().Be(2);


        actual.Messages.Select(v=>v.Value.Value)
            .ShouldBeEquivalentTo(expected);

    }



}
于 2013-07-25T09:04:47.760 回答