Beispiel #1
0
            internal override async Task SendAsync(IOperation op, CancellationToken token = default)
            {
                var mockConnectionPool = new Mock <IConnectionPool>();

                var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

                mockConnectionPoolFactory
                .Setup(m => m.Create(It.IsAny <ClusterNode>()))
                .Returns(mockConnectionPool.Object);

                var clusterNode = new ClusterNode(new ClusterContext(), mockConnectionPoolFactory.Object,
                                                  new Mock <ILogger <ClusterNode> >().Object, new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                                  new Mock <ICircuitBreaker>().Object,
                                                  new Mock <ISaslMechanismFactory>().Object,
                                                  new Mock <IRedactor>().Object,
                                                  new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11210),
                                                  BucketType.Couchbase,
                                                  new NodeAdapter(),
                                                  NullRequestTracer.Instance);

                await clusterNode.ExecuteOp(op, token).ConfigureAwait(false);

                if (_statuses.TryDequeue(out ResponseStatus status))
                {
                    (op as OperationBase)?.HandleOperationCompleted(AsyncState.BuildErrorResponse(0, status));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
 public ValueTask SendAsync(ReadOnlyMemory <byte> buffer, IOperation operation, CancellationToken cancellationToken = default)
 {
     operation.HandleOperationCompleted(AsyncState.BuildErrorResponse(0, ResponseStatus.KeyNotFound));
     return(default);
Beispiel #5
0
 public override Task SendAsync(IConnection connection, CancellationToken cancellationToken = default)
 {
     HandleOperationCompleted(AsyncState.BuildErrorResponse(0, Header.Status));
     return(Task.CompletedTask);
 }