示例#1
0
    public async Task Test4ThensAsync500MsEach2000MsTotal()
    {
        // Start the runner asynchronously.
        Task t = Runner.Run(S).Then(S, S, S).Async();

        // Take a lap right away. This should be only a few milliseconds in
        // if the runner tasks really started asynchronously.
        Laps.Add(Lapper.Lap());
        // Wait for all of the runner tasks to finish.
        await t.ConfigureAwait(false);

        Lapper.Stop();
        // There should be 5 laps; one that we took right away and one each per runner task.
        Assert.Equal(5, Laps.Count);
        // The first task should have been lapped right away, a few milliseconds after starting.
        // Let's give a 500% epsilon since the expected time is so short.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(10), Lapper.Laps[0], 5);
        // The rest should each take about 500ms.
        foreach (var lap in Laps.Skip(1))
        {
            TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
        }
        // ... And the total time should have been about 4 x 500ms = 2000ms.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(2000), Lapper.Elapsed, 0.1);
    }
示例#2
0
    public async Task Test4AndsAsync500MsEach500MsTotal()
    {
        // Start the runner asynchronously.
        Task t = Runner.Run(S).And(S, S, S).Async();

        // Take a lap right away. This should be only a few milliseconds in
        // if the runner tasks really started asynchronously.
        Laps.Add(Lapper.Lap());
        // Wait for the runner to finish.
        await t.ConfigureAwait(false);

        Lapper.Stop();
        // The async lap (first lap) should have been only a few ms after its stopwatch started.
        TimeAssert.EpsilonEquals(TimeSpan.FromMilliseconds(25), Laps[0], TimeSpan.FromMilliseconds(50));
        // We should have one lap per task run.
        Assert.Equal(5, Laps.Count);
        // Each lap should be about 500ms...
        // Skip the first lap because it was the one we took right away.
        foreach (TimeSpan lap in Laps.Skip(1))
        {
            TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
        }
        // And the elapsed time should be 500ms since the tasks were all run in parallel.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), Lapper.Elapsed, 0.1);
    }