internal async override Task SendAsync(IOperation op, CancellationToken token = default, TimeSpan?timeout = null)
            {
                var mockConnection = new Mock <IConnection>();

                mockConnection.SetupGet(x => x.IsDead).Returns(false);
                mockConnection
                .Setup(x => x.SendAsync(It.IsAny <ReadOnlyMemory <byte> >(), It.IsAny <Func <SocketAsyncState, Task> >()))
                .Returns(Task.CompletedTask);

                var clusterNode = new ClusterNode(new ClusterContext())
                {
                    Connection = mockConnection.Object
                };
                await clusterNode.ExecuteOp(op, token, timeout);

                if (_statuses.TryDequeue(out ResponseStatus status))
                {
                    await op.Completed(new SocketAsyncState
                    {
                        Status = status
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }

                // return Task.CompletedTask;
            }
Beispiel #2
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();
                }
            }
Beispiel #3
0
        public async Task When_Status_Indicates_Failure_ErrorCode_Is_Populated()
        {
            var errorMap = JsonConvert.DeserializeObject <ErrorMap>(ResourceHelper.ReadResource("kv-error-map.json"));

            var node = new ClusterNode(new ClusterContext(new CancellationTokenSource(),
                                                          new ClusterOptions()), new Mock <IConnectionFactory>().Object,
                                       new Mock <ILogger <ClusterNode> >().Object,
                                       new Mock <ITypeTranscoder>().Object,
                                       new Mock <ICircuitBreaker>().Object,
                                       new Mock <ISaslMechanismFactory>().Object)
            {
                ErrorMap = errorMap
            };

            var mockConnection = new Mock <IConnection>();

            mockConnection.Setup(x => x.IsDead).Returns(false);
            node.Connection = mockConnection.Object;

            var insert = new FakeOperation(OpCode.Add, ResponseStatus.KeyExists);

            try
            {
                await node.ExecuteOp(insert, CancellationToken.None, TimeSpan.FromMinutes(5));
            }
            catch (DocumentExistsException)
            {
                //need to resolve from context
                //Assert.NotNull((e.InnerException as DocumentExistsException)?.ErrorCode);
            }
        }
Beispiel #4
0
        public async Task When_Status_Indicates_Failure_ErrorCode_Is_Populated()
        {
            var errorMap = JsonConvert.DeserializeObject <ErrorMap>(ResourceHelper.ReadResource("kv-error-map.json"));

            var node = new ClusterNode(new ClusterContext(new CancellationTokenSource(),
                                                          new ClusterOptions()))
            {
                ErrorMap = errorMap
            };

            var mockConnection = new Mock <IConnection>();

            mockConnection.Setup(x => x.IsDead).Returns(false);
            node.Connection = mockConnection.Object;

            var insert = new FakeOperation(OpCode.Add, ResponseStatus.KeyExists);

            try
            {
                await node.ExecuteOp(insert, CancellationToken.None, TimeSpan.FromMinutes(5));
            }
            catch (KeyExistsException e)
            {
                Assert.NotNull((e.InnerException as KeyValueException)?.ErrorCode);
            }
        }
Beispiel #5
0
            internal override async Task SendAsync(IOperation op, CancellationToken token = default, TimeSpan?timeout = null)
            {
                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 Mock <ITypeTranscoder>().Object,
                                                  new Mock <ICircuitBreaker>().Object,
                                                  new Mock <ISaslMechanismFactory>().Object,
                                                  new Mock <IRedactor>().Object,
                                                  new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11210));

                await clusterNode.ExecuteOp(op, token, timeout);

                if (_statuses.TryDequeue(out ResponseStatus status))
                {
                    (op as OperationBase)?.HandleOperationCompleted(null, status);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
Beispiel #6
0
        public async Task When_Status_Indicates_Failure_Context_Is_Populated()
        {
            var errorMap = new ErrorMap(JsonConvert.DeserializeObject <ErrorMapDto>(ResourceHelper.ReadResource("kv-error-map.json")));

            var mockConnection = new Mock <IConnection>();

            var mockConnectionPool = new Mock <IConnectionPool>();

            mockConnectionPool
            .Setup(m => m.SendAsync(It.IsAny <IOperation>(), It.IsAny <CancellationToken>()))
            .Returns((IOperation operation, CancellationToken _) => operation.SendAsync(mockConnection.Object));

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

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

            var node = new ClusterNode(new ClusterContext(new CancellationTokenSource(),
                                                          new ClusterOptions()), 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
            {
                Hostname = "127.0.0.1"
            },
                                       NullRequestTracer.Instance)
            {
                ErrorMap = errorMap
            };

            var insert = new FakeOperation(OpCode.Add, ResponseStatus.KeyExists)
            {
                SName = "TheScope",
                CName = "TheCollection"
            };

            try
            {
                await node.ExecuteOp(insert, CancellationToken.None).ConfigureAwait(false);
            }
            catch (DocumentExistsException e)
            {
                var context = e.Context as KeyValueErrorContext;
                Assert.NotNull(e.Context);
                var message =
                    "KV Error: {Name=\"KEY_EEXISTS\", Description=\"key already exists, or CAS mismatch\", Attributes=\"item-only\"}";
                Assert.Equal(message, e.Context.Message);
                Assert.Equal("TheScope", context.ScopeName);
                Assert.Equal("TheCollection", context.CollectionName);
                Assert.NotEqual("0", context.ClientContextId);
            }
        }
Beispiel #7
0
        public async Task Test_ClusterMap_Version2(ResponseStatus status, string errorCode)
        {
            var errorMap = new ErrorMap(JsonSerializer.Deserialize(ResourceHelper.ReadResource("kv-error-map-v2.json"),
                                                                   InternalSerializationContext.Default.ErrorMapDto) !);

            var mockConnection = new Mock <IConnection>();

            var mockConnectionPool = new Mock <IConnectionPool>();

            mockConnectionPool
            .Setup(m => m.SendAsync(It.IsAny <IOperation>(), It.IsAny <CancellationToken>()))
            .Returns((IOperation operation, CancellationToken _) => operation.SendAsync(mockConnection.Object));

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

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

            var node = new ClusterNode(new ClusterContext(new CancellationTokenSource(),
                                                          new ClusterOptions()), mockConnectionPoolFactory.Object,
                                       new Mock <ILogger <ClusterNode> >().Object,
                                       new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                       new Mock <ICircuitBreaker>().Object,
                                       new Mock <ISaslMechanismFactory>().Object,
                                       new TypedRedactor(RedactionLevel.None),
                                       new HostEndpointWithPort("127.0.0.1", 11210),
                                       new NodeAdapter
            {
                Hostname = "127.0.0.1"
            },
                                       NoopRequestTracer.Instance)
            {
                ErrorMap = errorMap
            };

            var insert = new FakeOperation(OpCode.Add, status)
            {
                SName = "TheScope",
                CName = "TheCollection"
            };

            try
            {
                await node.ExecuteOp(insert).ConfigureAwait(false);
            }
            catch (CouchbaseException e)
            {
                var context = e.Context as KeyValueErrorContext;
                Assert.NotNull(e.Context);

                Assert.Contains(errorCode, e.Context.Message);
                Assert.Equal("TheScope", context.ScopeName);
                Assert.Equal("TheCollection", context.CollectionName);
                Assert.NotEqual("0", context.ClientContextId);
            }
        }
        public async Task When_Status_Indicates_Failure_ErrorCode_Is_Populated()
        {
            var errorMap = JsonConvert.DeserializeObject <ErrorMap>(ResourceHelper.ReadResource("kv-error-map.json"));

            var mockConnection = new Mock <IConnection>();

            var mockConnectionPool = new Mock <IConnectionPool>();

            mockConnectionPool
            .Setup(m => m.SendAsync(It.IsAny <IOperation>(), It.IsAny <CancellationToken>()))
            .Returns((IOperation operation, CancellationToken _) => operation.SendAsync(mockConnection.Object));

            var mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();

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

            var node = new ClusterNode(new ClusterContext(new CancellationTokenSource(),
                                                          new ClusterOptions()), mockConnectionPoolFactory.Object,
                                       new Mock <ILogger <ClusterNode> >().Object,
                                       new Mock <ITypeTranscoder>().Object,
                                       new Mock <ICircuitBreaker>().Object,
                                       new Mock <ISaslMechanismFactory>().Object,
                                       new Mock <IRedactor>().Object,
                                       new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11210))
            {
                ErrorMap = errorMap
            };

            var insert = new FakeOperation(OpCode.Add, ResponseStatus.KeyExists);

            try
            {
                await node.ExecuteOp(insert, CancellationToken.None, TimeSpan.FromMinutes(5));
            }
            catch (DocumentExistsException)
            {
                //need to resolve from context
                //Assert.NotNull((e.InnerException as DocumentExistsException)?.ErrorCode);
            }
        }