private async Task AssertRetryThenSuccessAsync(OperationBase op, Exception exp)
        {
            var retryOrchestrator = CreateRetryOrchestrator();

            var bucketMock = new Mock <BucketBase>("fake", new ClusterContext(), new Mock <IScopeFactory>().Object,
                                                   retryOrchestrator, new Mock <ILogger>().Object, new Mock <IRedactor>().Object,
                                                   new Mock <IBootstrapperFactory>().Object, NullRequestTracer.Instance);

            bucketMock.Setup(x => x.SendAsync(op, It.IsAny <CancellationToken>())).Callback((IOperation op, CancellationToken ct) =>
            {
                if (op.Completed.IsCompleted)
                {
                    Assert.True(false, "operation result should be reset before retry");
                }
                // complete the operation (ResponseStatus does not matter for this test)
                op.HandleOperationCompleted(AsyncState.BuildErrorResponse(op.Opaque, ResponseStatus.TemporaryFailure));
                if (op.Attempts == 1)
                {
                    throw exp;
                }
            }).Returns(op.Completed);

            var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(2500));

            try
            {
                await retryOrchestrator.RetryAsync(bucketMock.Object, op, tokenSource.Token).ConfigureAwait(false);
            }
            catch (Exception)
            {
                Assert.True(false, "Expected operation to succeed after retry");
            }
            Assert.True(op.Attempts > 1);
        }
예제 #2
0
        private async Task AssertRetryThenSuccessAsync(OperationBase op, Exception exp)
        {
            var retryOrchestrator = CreateRetryOrchestrator();

            var bucketMock = new Mock <BucketBase>("fake", new ClusterContext(), new Mock <Couchbase.Core.DI.IScopeFactory>().Object,
                                                   retryOrchestrator, new Mock <ILogger>().Object, new Mock <IRedactor>().Object,
                                                   new Mock <IBootstrapperFactory>().Object, NoopRequestTracer.Instance, new Mock <IOperationConfigurator>().Object,
                                                   new BestEffortRetryStrategy());

            bucketMock.Setup(x => x.SendAsync(op, It.IsAny <CancellationTokenPair>())).Callback((IOperation op1, CancellationTokenPair ct) =>
            {
                if (op1.Completed.IsCompleted)
                {
                    Assert.True(false, "operation result should be reset before retry");
                }

                // complete the operation if circuit breaker is not open (ResponseStatus does not matter for this test)
                if (exp.GetType() != typeof(CircuitBreakerException) || op.Attempts != 1)
                {
                    op.HandleOperationCompleted(AsyncState.BuildErrorResponse(op.Opaque, ResponseStatus.TemporaryFailure));
                }

                if (op1.Attempts == 1)
                {
                    throw exp;
                }
            }).Returns(op.Completed.AsTask());

            var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(2500));

            try
            {
                await retryOrchestrator.RetryAsync(bucketMock.Object, op, CancellationTokenPair.FromInternalToken(tokenSource.Token)).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                Assert.True(false, "Expected operation to succeed after retry");
            }
            Assert.True(op.Attempts > 1);
        }