Esempio n. 1
0
        public async Task ExecTest()
        {
            var que = new TaskQueue <bool>();

            //失败重试
            que.Add(_ => Run(1000), 0, "cmd1");
            que.Add(_ => Run(500, false), 2, "cmd2");
            que.Add(_ => Run(500), 0, "cmd3");
            await que.Exec();

            que.History.Select(p => p.Result).Should().BeEquivalentTo(new[] { true, false, true });

            //固定超时
            que.Clear();
            que.Add(token => Run(1000, true, token), 0, "cmd1");
            que.Add(token => Run(1000, true, token), 0, "cmd2");
            que.Add(token => Run(1000, true, token), 0, "cmd3");
            await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(1.7));

            que.History.Count(p => p.Result).Should().Be(2);

            //取消任务
            que.Clear();
            que.Add(token => Run(500, true, token), 0, "cmd1");
            que.Add(token => Run(500, true, token), 0, "cmd2");
            que.Add(token => Run(500, true, token), 0, "cmd3");
            que.Add(token => Run(500, true, token), 0, "cmd4");
            que.Add(token => Run(500, true, token), 0, "cmd5");
            que.Add(token => Run(500, true, token), 0, "cmd6");
            var cts = new CancellationTokenSource();

            cts.CancelAfter(2100);
            await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(cts.Token));

            que.History.Count().Should().Be(5);

            //暂停 继续
            que.Build();
            await Assert.ThrowsAsync(typeof(OperationCanceledException), async() => await que.Exec(1.2));

            que.History.Count(p => p.Result).Should().Be(2);
            que.History.Count(p => !p.Result).Should().Be(1);
            que.PendingQueue.Count().Should().Be(4);
            await que.Exec();

            que.History.Count(p => p.Result).Should().Be(6);
            que.History.Count(p => !p.Result).Should().Be(1);
            que.PendingQueue.Count().Should().Be(0);
        }