private RequestHandlerMockResult BuildRequestHandler(
            IStatement statement,
            Action <TestConfigurationBuilder> configBuilderAct,
            IExecutionProfile profile)
        {
            var cts        = new CancellationTokenSource();
            var connection = Mock.Of <IConnection>();

            // create config
            var configBuilder = new TestConfigurationBuilder
            {
                ControlConnectionFactory = new FakeControlConnectionFactory(),
                ConnectionFactory        = new FakeConnectionFactory(() => connection),
                Policies = new Cassandra.Policies(new RoundRobinPolicy(), new ConstantReconnectionPolicy(100), new DefaultRetryPolicy())
            };

            configBuilderAct(configBuilder);
            var config          = configBuilder.Build();
            var initializerMock = Mock.Of <IInitializer>();

            Mock.Get(initializerMock).Setup(i => i.ContactPoints).Returns(new List <IPEndPoint>
            {
                new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042),
                new IPEndPoint(IPAddress.Parse("127.0.0.2"), 9042)
            });
            Mock.Get(initializerMock).Setup(i => i.GetConfiguration()).Returns(config);

            // create cluster
            var cluster = Cluster.BuildFrom(initializerMock, new List <string>());

            cluster.Connect();

            // create session
            var session = new Session(cluster, config, null, SerializerManager.Default, null);

            // create request handler
            var options = profile != null
                ? new RequestOptions(profile, null, config.Policies, config.SocketOptions, config.QueryOptions, config.ClientOptions)
                : config.DefaultRequestOptions;
            var requestHandler = new RequestHandler(
                session,
                new SerializerManager(ProtocolVersion.V3).GetCurrentSerializer(),
                statement,
                options);

            // create mock result object
            var mockResult = new RequestHandlerMockResult(requestHandler);

            // mock connection send
            Mock.Get(connection)
            .Setup(c => c.Send(It.IsAny <IRequest>(), It.IsAny <Action <IRequestError, Response> >(), It.IsAny <int>()))
            .Returns <IRequest, Action <IRequestError, Response>, int>((req, act, timeout) =>
            {
                mockResult.SendResults.Enqueue(new ConnectionSendResult {
                    Request = req, TimeoutMillis = timeout
                });
                Task.Run(async() =>
                {
                    var rp  = (FakeRetryPolicy)(statement.RetryPolicy ?? options.RetryPolicy);
                    var sep = (FakeSpeculativeExecutionPolicy)options.SpeculativeExecutionPolicy;
                    if (Interlocked.Read(ref rp.Count) > 0 && Interlocked.Read(ref sep.Count) > 0)
                    {
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                        act(null, new ProxyResultResponse(ResultResponse.ResultResponseKind.Void));
                        cts.Cancel();
                    }
                    else
                    {
                        try
                        {
                            await Task.Delay(10, cts.Token).ConfigureAwait(false);
                        }
                        finally
                        {
                            act(RequestError.CreateServerError(new OverloadedException(string.Empty)), null);
                        }
                    }
                });
                return(new OperationState(act, req, timeout, NullOperationObserver.Instance));
            });
            Mock.Get(connection)
            .SetupGet(c => c.EndPoint)
            .Returns(new ConnectionEndPoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), config.ServerNameResolver, null));

            return(mockResult);
        }
Example #2
0
 internal static RetryDecision GetRetryDecisionFromServerError(Exception ex, IExtendedRetryPolicy policy, IStatement statement, Configuration config, int retryCount)
 {
     return(RequestExecution.GetRetryDecisionWithReason(RequestError.CreateServerError(ex), policy, statement, config, retryCount).Decision);
 }