public void SetUp()
        {
            replica1 = new Uri("http://replica1");
            replica2 = new Uri("http://replica2");

            response1        = new Response(ResponseCode.Ok);
            response2        = new Response(ResponseCode.Ok);
            selectedResponse = new Response(ResponseCode.Ok);

            result1 = new ReplicaResult(replica1, response1, ResponseVerdict.DontKnow, TimeSpan.Zero);
            result2 = new ReplicaResult(replica2, response2, ResponseVerdict.DontKnow, TimeSpan.Zero);

            var log = new ConsoleLog();

            context = new RequestContext(Request.Get("foo/bar"), Substitute.For <IRequestStrategy>(), Budget.Infinite, log, CancellationToken.None, null, int.MaxValue);
            // ReSharper disable AssignNullToNotNullAttribute
            context.Strategy.SendAsync(null, null, null, null, 0, default(CancellationToken))
            // ReSharper restore AssignNullToNotNullAttribute
            .ReturnsForAnyArgs(
                async info =>
            {
                var replicas = info.Arg <IEnumerable <Uri> >();
                var sender   = info.Arg <IRequestSender>();

                foreach (var replica in replicas)
                {
                    await sender.SendToReplicaAsync(replica, context.Request, TimeSpan.Zero, CancellationToken.None);
                }
            });

            clusterProvider = Substitute.For <IClusterProvider>();
            clusterProvider.GetCluster().Returns(new[] { replica1, replica2 });

            requestSender = Substitute.For <IRequestSender>();
            requestSender.SendToReplicaAsync(replica1, Arg.Any <Request>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => result1);
            requestSender.SendToReplicaAsync(replica2, Arg.Any <Request>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => result2);

            replicaOrdering = Substitute.For <IReplicaOrdering>();
            // ReSharper disable AssignNullToNotNullAttribute
            replicaOrdering.Order(null, null, null).ReturnsForAnyArgs(info => info.Arg <IList <Uri> >().Reverse());
            // ReSharper restore AssignNullToNotNullAttribute

            responseSelector = Substitute.For <IResponseSelector>();
            // ReSharper disable once AssignNullToNotNullAttribute
            responseSelector.Select(null).ReturnsForAnyArgs(_ => selectedResponse);

            resultStatusSelector = Substitute.For <IClusterResultStatusSelector>();
            // ReSharper disable AssignNullToNotNullAttribute
            resultStatusSelector.Select(null, null).ReturnsForAnyArgs(ClusterResultStatus.Success);
            // ReSharper restore AssignNullToNotNullAttribute

            storageProvider = Substitute.For <IReplicaStorageProvider>();
            module          = new RequestExecutionModule(
                clusterProvider,
                replicaOrdering,
                responseSelector,
                storageProvider,
                requestSender,
                resultStatusSelector);
        }
Ejemplo n.º 2
0
        public async Task <ClusterResult> ExecuteAsync(IRequestContext context, Func <IRequestContext, Task <ClusterResult> > next)
        {
            var replicas = clusterProvider.GetCluster();

            if (replicas == null || replicas.Count == 0)
            {
                LogReplicasNotFound(context);
                return(ClusterResult.ReplicasNotFound(context.Request));
            }

            var contextImpl      = (RequestContext)context;
            var contextualSender = new ContextualRequestSender(requestSender, contextImpl);

            var maxReplicasToUse = context.MaximumReplicasToUse;
            var orderedReplicas  = replicaOrdering.Order(replicas, storageProvider, contextImpl.Request);
            var limitedReplicas  = orderedReplicas.Take(maxReplicasToUse);

            await contextImpl.Strategy.SendAsync(contextImpl.Request, contextualSender, contextImpl.Budget, limitedReplicas,
                                                 Math.Min(replicas.Count, maxReplicasToUse), context.CancellationToken).ConfigureAwait(false);

            context.CancellationToken.ThrowIfCancellationRequested();

            var replicaResults = contextImpl.FreezeReplicaResults();

            var selectedResponse = responseSelector.Select(replicaResults);

            var resultStatus = resultStatusSelector.Select(replicaResults, contextImpl.Budget);

            return(new ClusterResult(resultStatus, replicaResults, selectedResponse, context.Request));
        }
Ejemplo n.º 3
0
        public void TestSetup()
        {
            replica1 = new Uri("http://replica1");
            replica2 = new Uri("http://replica2");

            response1        = new Response(ResponseCode.Ok);
            response2        = new Response(ResponseCode.Ok);
            selectedResponse = new Response(ResponseCode.Ok);

            result1 = new ReplicaResult(replica1, response1, ResponseVerdict.DontKnow, TimeSpan.Zero);
            result2 = new ReplicaResult(replica2, response2, ResponseVerdict.DontKnow, TimeSpan.Zero);

            var parameters = RequestParameters.Empty
                             .WithStrategy(Substitute.For <IRequestStrategy>());

            clusterProvider = Substitute.For <IClusterProvider>();
            clusterProvider.GetCluster().Returns(new[] { replica1, replica2 });

            replicaOrdering = Substitute.For <IReplicaOrdering>();
            replicaOrdering.Order(null, null, null, null).ReturnsForAnyArgs(info => info.Arg <IList <Uri> >().Reverse());

            context = new RequestContext(
                Request.Get("foo/bar"),
                parameters,
                Budget.Infinite,
                new SilentLog(),
                clusterProvider,
                replicaOrdering,
                transport: default,
        public void TestSetup()
        {
            replica1 = new Uri("http://replica1");
            replica2 = new Uri("http://replica2");

            response1        = new Response(ResponseCode.Ok);
            response2        = new Response(ResponseCode.Ok);
            selectedResponse = new Response(ResponseCode.Ok);

            result1 = new ReplicaResult(replica1, response1, ResponseVerdict.DontKnow, TimeSpan.Zero);
            result2 = new ReplicaResult(replica2, response2, ResponseVerdict.DontKnow, TimeSpan.Zero);

            var parameters = RequestParameters.Empty
                             .WithStrategy(Substitute.For <IRequestStrategy>());

            context = new RequestContext(Request.Get("foo/bar"), parameters, Budget.Infinite, new SilentLog(), null, int.MaxValue);
            context.Parameters.Strategy.SendAsync(null, null, null, null, null, 0, default)
            .ReturnsForAnyArgs(
                async info =>
            {
                var replicas = info.Arg <IEnumerable <Uri> >();
                var sender   = info.Arg <IRequestSender>();

                foreach (var replica in replicas)
                {
                    await sender.SendToReplicaAsync(replica, context.Request, null, TimeSpan.Zero, CancellationToken.None);
                }
            });

            clusterProvider = Substitute.For <IClusterProvider>();
            clusterProvider.GetCluster().Returns(new[] { replica1, replica2 });

            requestSender = Substitute.For <IRequestSenderInternal>();
            requestSender.SendToReplicaAsync(Arg.Any <ITransport>(), replica1, Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => result1);
            requestSender.SendToReplicaAsync(Arg.Any <ITransport>(), replica2, Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => result2);

            replicaOrdering = Substitute.For <IReplicaOrdering>();
            replicaOrdering.Order(null, null, null, null).ReturnsForAnyArgs(info => info.Arg <IList <Uri> >().Reverse());

            responseSelector = Substitute.For <IResponseSelector>();
            responseSelector.Select(null, null, null).ReturnsForAnyArgs(_ => selectedResponse);

            resultStatusSelector = Substitute.For <IClusterResultStatusSelector>();
            resultStatusSelector.Select(null, null).ReturnsForAnyArgs(ClusterResultStatus.Success);

            storageProvider = Substitute.For <IReplicaStorageProvider>();
            module          = new RequestExecutionModule(
                clusterProvider,
                replicaOrdering,
                responseSelector,
                storageProvider,
                requestSender,
                resultStatusSelector);
        }