public async Task InjectCommonServerError_Should_TransformResponseToError()
        {
            var transport       = Substitute.For <ITransport>();
            var clusterProvider = Substitute.For <IClusterProvider>();

            transport.Capabilities.Returns(TransportCapabilities.None);
            transport
            .SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>())
            .Returns(Task.FromResult(new Response(ResponseCode.Ok)));
            clusterProvider.GetCluster().Returns(new List <Uri> {
                new Uri("http://localhost1")
            });
            var clusterClient = new Vostok.Clusterclient.Core.ClusterClient(null,
                                                                            configuration =>
            {
                configuration.Transport              = transport;
                configuration.ClusterProvider        = clusterProvider;
                configuration.DefaultRequestStrategy = new SingleReplicaRequestStrategy();
                configuration.InjectCommonServerError(() => 1);
            });
            var request = new Request("GET", new Uri("/fakemethod", UriKind.Relative));

            var actualResponse = await clusterClient.SendAsync(request);

            actualResponse.Response.Should().BeEquivalentTo(new Response(ResponseCode.InternalServerError));
        }
        public async Task InjectTotalLatency_Should_AddLatencyOnceForSendMethod()
        {
            var transport       = Substitute.For <ITransport>();
            var clusterProvider = Substitute.For <IClusterProvider>();

            transport.Capabilities.Returns(TransportCapabilities.None);
            transport
            .SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>())
            .Returns(Task.FromResult(new Response(ResponseCode.Ok)));
            clusterProvider.GetCluster().Returns(new List <Uri> {
                new Uri("http://localhost1")
            });

            var delay            = TimeSpan.FromMilliseconds(200);
            var latencyPerformer = new MockLatencyPerformer();
            var clusterClient    = new Vostok.Clusterclient.Core.ClusterClient(null,
                                                                               configuration =>
            {
                configuration.Transport              = transport;
                configuration.ClusterProvider        = clusterProvider;
                configuration.DefaultRequestStrategy = new SingleReplicaRequestStrategy();
                configuration.InjectTotalLatency(latencyPerformer, () => delay, () => 1);
            });
            var request = new Request("GET", new Uri("/fakemethod", UriKind.Relative));

            await clusterClient.SendAsync(request);

            latencyPerformer.TotalAddedLatency.Should().Be(delay);
        }
Beispiel #3
0
 public void OneTimeSetup()
 {
     server = Server.CreateHostBuilder(new string[0]).Build();
     server.Start();
     client = new Vostok.Clusterclient.Core.ClusterClient(new SilentLog(), configuration =>
     {
         configuration.SetupUniversalTransport(new UniversalTransportSettings
         {
             AllowAutoRedirect = true
         });
         configuration.DefaultTimeout  = TimeSpan.FromSeconds(5);
         configuration.ClusterProvider = new FixedClusterProvider("http://localhost:5000");
         configuration.InjectTotalLatency(() => TimeSpan.FromSeconds(1), () => 0.05);
     });
 }
        public async Task InjectLatencyOnEveryRetry_Should_AddLatencyOnEveryRetryCall()
        {
            var transport       = Substitute.For <ITransport>();
            var clusterProvider = Substitute.For <IClusterProvider>();

            var callsCount = 0;

            transport.Capabilities.Returns(TransportCapabilities.None);
            transport
            .SendAsync(Arg.Any <Request>(), Arg.Any <TimeSpan?>(), Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>())
            .Returns(Task.FromResult(new Response(ResponseCode.TooManyRequests)))
            .AndDoes(_ => callsCount++);
            clusterProvider.GetCluster().Returns(new List <Uri>
            {
                new Uri("http://localhost1"),
                new Uri("http://localhost2"),
                new Uri("http://localhost3")
            });


            var latency          = TimeSpan.FromMilliseconds(200);
            var latencyPerformer = new MockLatencyPerformer();
            var clusterClient    = new Vostok.Clusterclient.Core.ClusterClient(null,
                                                                               configuration =>
            {
                configuration.Transport              = transport;
                configuration.ClusterProvider        = clusterProvider;
                configuration.RetryPolicy            = new AdHocRetryPolicy((_, __, ___) => callsCount < 3);
                configuration.RetryStrategy          = new ImmediateRetryStrategy(3);
                configuration.DefaultRequestStrategy = new SingleReplicaRequestStrategy();
                configuration.InjectLatencyOnEveryRetry(latencyPerformer, () => latency, () => 1);
            });

            var request = new Request("GET", new Uri("/fakemethod", UriKind.Relative));


            await clusterClient.SendAsync(request);

            callsCount.Should().Be(3);
            latencyPerformer.TotalAddedLatency.Should().Be(latency * 3);
        }