public void TestSetup() { request = Request.Get("http://foo/bar"); response = new Response(ResponseCode.Ok); parameters = RequestParameters.Empty.WithConnectionTimeout(1.Seconds()); var budget = Budget.WithRemaining(5.Seconds()); transport = Substitute.For <ITransport>(); transport.SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => response); context = Substitute.For <IRequestContext>(); context.Request.Returns(_ => request); context.Budget.Returns(_ => budget); context.Transport.Returns(_ => transport); context.Parameters.Returns(_ => parameters); responseCriteria = new List <IResponseCriterion>(); responseClassifier = Substitute.For <IResponseClassifier>(); responseClassifier.Decide(Arg.Any <Response>(), Arg.Any <IList <IResponseCriterion> >()).Returns(ResponseVerdict.Accept); resultStatusSelector = Substitute.For <IClusterResultStatusSelector>(); resultStatusSelector.Select(null, null).ReturnsForAnyArgs(ClusterResultStatus.Success); module = new AbsoluteUrlSenderModule(responseClassifier, responseCriteria, resultStatusSelector); }
public async Task <ReplicaResult> SendToReplicaAsync( ITransport transport, IReplicaOrdering replicaOrdering, Uri replica, Request request, int connectionAttempts, TimeSpan?connectionTimeout, TimeSpan timeout, CancellationToken cancellationToken) { if (configuration.Logging.LogReplicaRequests) { LogRequest(replica, timeout); } var timeBudget = TimeBudget.StartNew(timeout, TimeSpan.FromMilliseconds(1)); var absoluteRequest = requestConverter.TryConvertToAbsolute(request, replica); var response = await SendRequestAsync(transport, absoluteRequest, timeBudget, connectionAttempts, connectionTimeout, cancellationToken).ConfigureAwait(false); var responseVerdict = responseClassifier.Decide(response, configuration.ResponseCriteria); var result = new ReplicaResult(replica, response, responseVerdict, timeBudget.Elapsed); if (configuration.Logging.LogReplicaResults) { LogResult(result); } replicaOrdering.Learn(result, storageProvider); return(result); }
public async Task <ReplicaResult> SendToReplicaAsync(Uri replica, Request request, TimeSpan timeout, CancellationToken cancellationToken) { if (configuration.LogReplicaRequests) { LogRequest(replica, timeout); } var watch = Stopwatch.StartNew(); var absoluteRequest = requestConverter.TryConvertToAbsolute(request, replica); var response = await SendRequestAsync(absoluteRequest, timeout, cancellationToken).ConfigureAwait(false); var responseVerdict = responseClassifier.Decide(response, configuration.ResponseCriteria); var result = new ReplicaResult(replica, response, responseVerdict, watch.Elapsed); if (configuration.LogReplicaResults) { LogResult(result); } configuration.ReplicaOrdering.Learn(result, storageProvider); return(result); }
public void SetUp() { replica = new Uri("http://replica/"); relativeRequest = Request.Get("foo/bar"); absoluteRequest = Request.Get("http://replica/foo/bar"); response = new Response(ResponseCode.Ok); timeout = 5.Seconds(); log = Substitute.For <ILog>(); log .When(l => l.Log(Arg.Any <LogEvent>())) .Do(info => new ConsoleLog().Log(info.Arg <LogEvent>())); configuration = Substitute.For <IClusterClientConfiguration>(); configuration.ResponseCriteria.Returns(new List <IResponseCriterion> { Substitute.For <IResponseCriterion>() }); configuration.LogReplicaRequests.Returns(true); configuration.LogReplicaResults.Returns(true); configuration.ReplicaOrdering.Returns(Substitute.For <IReplicaOrdering>()); configuration.Log.Returns(log); storageProvider = Substitute.For <IReplicaStorageProvider>(); responseClassifier = Substitute.For <IResponseClassifier>(); responseClassifier.Decide(Arg.Any <Response>(), Arg.Any <IList <IResponseCriterion> >()).Returns(ResponseVerdict.Accept); requestConverter = Substitute.For <IRequestConverter>(); requestConverter.TryConvertToAbsolute(relativeRequest, replica).Returns(_ => absoluteRequest); transport = Substitute.For <ITransport>(); transport.SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).Returns(_ => response); sender = new RequestSender(configuration, storageProvider, responseClassifier, requestConverter, transport); }
public void Setup() { request = Request.Get("http://foo/bar"); response = new Response(ResponseCode.Ok); var budget = Budget.WithRemaining(5.Seconds()); context = Substitute.For <IRequestContext>(); context.Request.Returns(_ => request); context.Budget.Returns(_ => budget); transport = Substitute.For <ITransport>(); transport.SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>()).ReturnsTask(_ => response); responseCriteria = new List <IResponseCriterion>(); responseClassifier = Substitute.For <IResponseClassifier>(); responseClassifier.Decide(Arg.Any <Response>(), Arg.Any <IList <IResponseCriterion> >()).Returns(ResponseVerdict.Accept); resultStatusSelector = Substitute.For <IClusterResultStatusSelector>(); // ReSharper disable AssignNullToNotNullAttribute resultStatusSelector.Select(null, null).ReturnsForAnyArgs(ClusterResultStatus.Success); // ReSharper restore AssignNullToNotNullAttribute module = new AbsoluteUrlSenderModule(transport, responseClassifier, responseCriteria, resultStatusSelector); }
private async Task <ClusterResult> SendAsync(IRequestContext context) { var elapsedBefore = context.Budget.Elapsed; var response = await context.Transport.SendAsync(context.Request, null, context.Budget.Remaining, context.CancellationToken).ConfigureAwait(false); if (response.Code == ResponseCode.Canceled) { return(ClusterResult.Canceled(context.Request)); } var responseVerdict = responseClassifier.Decide(response, responseCriteria); var replicaResult = new ReplicaResult(context.Request.Url, response, responseVerdict, context.Budget.Elapsed - elapsedBefore); var replicaResults = new[] { replicaResult }; var resultStatus = resultStatusSelector.Select(replicaResults, context.Budget); return(new ClusterResult(resultStatus, replicaResults, response, context.Request)); }
public void Should_return_result_with_correct_verdict(ResponseVerdict verdict) { responseClassifier.Decide(response, configuration.ResponseCriteria).Returns(verdict); Send().Verdict.Should().Be(verdict); }