コード例 #1
0
        public void Should_cancel_remaining_requests_and_delays_when_receiving_accepted_result()
        {
            var tokens = new List <CancellationToken>();

            sender
            .When(s => s.SendToReplicaAsync(Arg.Any <Uri>(), Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()))
            .Do(info => tokens.Add(info.Arg <CancellationToken>()));

            delaysPlanner
            .When(p => p.Plan(Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()))
            .Do(info => tokens.Add(info.Arg <CancellationToken>()));

            strategy = new ForkingRequestStrategy(delaysProvider, delaysPlanner, int.MaxValue);

            var sendTask = strategy.SendAsync(request, parameters, sender, Budget.WithRemaining(5.Seconds()), replicas, replicas.Length, token);

            CompleteForkingDelay();
            CompleteForkingDelay();
            CompleteForkingDelay();

            CompleteRequest(replicas.First(), ResponseVerdict.Accept);

            sendTask.GetAwaiter().GetResult();

            tokens.Should().HaveCount(8);

            foreach (var t in tokens)
            {
                t.IsCancellationRequested.Should().BeTrue();
            }
        }
        public void Should_cancel_remaining_requests_when_receiving_accepted_result()
        {
            DropSynchronizationContext();

            var tokens = new List <CancellationToken>();

            sender
            .When(s => s.SendToReplicaAsync(Arg.Any <Uri>(), Arg.Any <Request>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()))
            .Do(info => tokens.Add(info.Arg <CancellationToken>()));

            strategy = new ParallelRequestStrategy(int.MaxValue);

            var sendTask = strategy.SendAsync(request, sender, Budget.WithRemaining(5.Seconds()), replicas, replicas.Length, token);

            CompleteRequest(replicas.Last(), ResponseVerdict.Accept);

            sendTask.GetAwaiter().GetResult();

            tokens.Should().HaveCount(replicas.Length);

            foreach (var t in tokens)
            {
                t.IsCancellationRequested.Should().BeTrue();
            }
        }