예제 #1
0
        public async Task ExecuteAsync_ShouldCancelAction_WhenCancellationToken_IsCancelled()
        {
            // Arrange
            var policy = new ServiceRetryPolicy(0, TimeSpan.Zero);

            using (var cancellationSource = new CancellationTokenSource())
                using (var breakpoint = new Breakpoint())
                {
                    var cancelled = false;

                    // Act
                    var task = policy.ExecuteAsync(
                        async ct =>
                    {
                        // ReSharper disable once AccessToDisposedClosure
                        await breakpoint.Hit().WaitForContinue();
                        cancelled = ct.IsCancellationRequested;
                        ct.ThrowIfCancellationRequested();
                        return(Result.Ok());
                    },
                        cancellationSource.Token);

                    var hit = await breakpoint.WaitForHit().TimeoutAfter(TestTimeout);

                    cancellationSource.Cancel();
                    hit.Continue();
                    var result = await task.TimeoutAfter(TestTimeout);

                    // Assert
                    result.Success.Should().BeFalse();
                    cancelled.Should().BeTrue();
                }
        }
예제 #2
0
        public void Execute_ShouldBeFailure_WhenActionReturnsFailure()
        {
            // Arrange
            var policy = new ServiceRetryPolicy(2, TimeSpan.Zero);

            // Act
            var result = policy.Value().Execute(() => Result.Fail());

            // Assert
            result.Failure.Should().BeTrue();
        }
예제 #3
0
        public void Execute_ShouldBeSuccess_WhenActionReturnsSuccess()
        {
            // Arrange
            var policy = new ServiceRetryPolicy(2, TimeSpan.Zero);

            // Act
            var result = policy.Value().Execute(Result.Ok);

            // Assert
            result.Success.Should().BeTrue();
        }
예제 #4
0
        public async Task ExecuteAsync_ShouldBeFailure_WhenActionReturnsFailure()
        {
            // Arrange
            var policy = new ServiceRetryPolicy(0, TimeSpan.Zero);

            // Act
            var result = await policy.ExecuteAsync(ct => Task.FromResult(Result.Fail()), CancellationToken.None);

            // Assert
            result.Failure.Should().BeTrue();
        }
예제 #5
0
        public async Task ExecuteAsync_ShouldBe_ActionResult()
        {
            // Arrange
            var expected = Result.Ok();
            var policy   = new ServiceRetryPolicy(2, TimeSpan.Zero);

            // Act
            var result = await policy.ExecuteAsync(ct => Task.FromResult(expected), CancellationToken.None);

            // Assert
            result.Should().Be(expected);
        }
예제 #6
0
        public void Execute_ShouldCallAction_RetryCountPlusOneTime_WhenActionReturnsFailure()
        {
            // Arrange
            const int retryCount  = 2;
            var       timesCalled = 0;
            var       policy      = new ServiceRetryPolicy(retryCount, TimeSpan.Zero);

            // Act
            policy.Value().Execute(() =>
            {
                timesCalled++;
                return(Result.Fail());
            });

            // Assert
            timesCalled.Should().Be(1 + retryCount);
        }
예제 #7
0
        public async Task ExecuteAsync_ShouldCallAction_RetryCountPlusOneTime_WhenActionReturnsFailure()
        {
            // Arrange
            const int retryCount  = 2;
            var       timesCalled = 0;
            var       policy      = new ServiceRetryPolicy(retryCount, TimeSpan.Zero);

            // Act
            await policy.ExecuteAsync(
                ct =>
            {
                timesCalled++;
                return(Task.FromResult(Result.Fail()));
            },
                CancellationToken.None);

            // Assert
            timesCalled.Should().Be(1 + retryCount);
        }