public void FailEarlyIfTimeoutIsExhausted_Async()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config, dateTimeProvider);

                var getCall = FakeCalls.GetCall(fake);
                var ok      = Task.FromResult(FakeResponse.Ok(config));
                var bad     = Task.FromResult(FakeResponse.Bad(config));
                getCall.ReturnsNextFromSequence(
                    bad,
                    bad,
                    ok
                    );

                var seenNodes = new List <Uri>();
                getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u));

                var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingCall.Returns(ok);

                var client1 = fake.Resolve <ElasticsearchClient>();
                //event though the third node should have returned ok, the first 2 calls took a minute
                var e = Assert.Throws <MaxRetryException>(async() => await client1.InfoAsync());
                e.Message.Should()
                .StartWith("Retry timeout 00:01:20 was hit after retrying 1 times:");

                IElasticsearchResponse response = null;
                Assert.DoesNotThrow(async() => response = await client1.InfoAsync());
                response.Should().NotBeNull();
                response.Success.Should().BeTrue();
            }
        }
        public void ShouldRetryOnPingConnectionException()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var connectionPool = new StaticConnectionPool(_uris, randomizeOnStartup: false);
                var config         = new ConnectionConfiguration(connectionPool);

                fake.Provide <IConnectionConfigurationValues>(config);
                FakeCalls.ProvideDefaultTransport(fake);

                var pingCall  = FakeCalls.PingAtConnectionLevel(fake);
                var seenPorts = new List <int>();
                pingCall.ReturnsLazily((Uri u, IRequestConfiguration c) =>
                {
                    seenPorts.Add(u.Port);
                    throw new Exception("Something bad happened");
                });

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Ok(config));

                var client = fake.Resolve <ElasticsearchClient>();

                var e = Assert.Throws <MaxRetryException>(() => client.Info());
                pingCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
                getCall.MustNotHaveHappened();

                //make sure that if a ping throws an exception it wont
                //keep retrying to ping the same node but failover to the next
                seenPorts.ShouldAllBeEquivalentTo(_uris.Select(u => u.Port));
                var aggregateException = e.InnerException as AggregateException;
                aggregateException.Should().NotBeNull();
                aggregateException.InnerExceptions.Should().Contain(ex => ex.GetType().Name == "PingException");
            }
        }
        public void IfResponseIsKnowError_DoNotRetry_ThrowServerException(int status, string exceptionType, string exceptionMessage)
        {
            var response = CreateServerExceptionResponse(status, exceptionType, exceptionMessage);

            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var connectionPool = new StaticConnectionPool(new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201"),
                });
                var connectionConfiguration = new ConnectionConfiguration(connectionPool)
                                              .ThrowOnElasticsearchServerExceptions()
                                              .ExposeRawResponse(false);

                fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
                FakeCalls.ProvideDefaultTransport(fake);

                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(connectionConfiguration));

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Any(connectionConfiguration, status, response: response));

                var client = fake.Resolve <ElasticsearchClient>();

                var e = Assert.Throws <ElasticsearchServerException>(() => client.Info());
                AssertServerErrorsOnResponse(e, status, exceptionType, exceptionMessage);

                //make sure a know ElasticsearchServerException does not cause a retry
                //In this case we want to fail early

                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public void ShouldNotThrowAndNotRetry401_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var uris = new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201"),
                    new Uri("http://localhost:9202")
                };
                var connectionPool = new StaticConnectionPool(uris, randomizeOnStartup: false);
                var config         = new ConnectionConfiguration(connectionPool);

                fake.Provide <IConnectionConfigurationValues>(config);
                FakeCalls.ProvideDefaultTransport(fake);

                var pingAsyncCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingAsyncCall.Returns(FakeResponse.OkAsync(config));

                //sniffing is always synchronous and in turn will issue synchronous pings
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var getCall = FakeCalls.GetCall(fake);
                getCall.Returns(FakeResponse.Any(config, 401));

                var client = fake.Resolve <ElasticsearchClient>();

                Assert.DoesNotThrow(async() => await client.InfoAsync());
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public StaticConnectionPoolTests()
        {
            _connectionPool = new StaticConnectionPool(_uris);
            _config         = new ConnectionConfiguration(_connectionPool);

            _ok  = FakeResponse.Ok(_config);
            _bad = FakeResponse.Bad(_config);
        }
Exemple #6
0
        public SingleNodeConnectionPoolTests()
        {
            _config = new ConnectionConfiguration(new Uri("http://localhost:9200"))
                      .MaximumRetries(2);

            _ok  = FakeResponse.Ok(_config);
            _bad = FakeResponse.Any(_config, -1);
        }
 public async Task Stream_Ok_KeepResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <Stream>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ).Init())
         this.ShouldDirectlyStream(request, success: true);
 }
 public async Task String_Ok_KeepResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <string>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ).Init())
         this.ShouldStreamOfCopy(request);
 }
 public async Task Typed_Ok_DiscardResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <StandardResponse>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ).Init())
         this.ShouldDirectlyStream(request);
 }
 public async Task ByteArray_Ok_DiscardResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <byte[]>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ).Init())
         this.ShouldStreamOfCopy(request, keepRaw: false);
 }
Exemple #11
0
 public void Stream_Ok_KeepResponse(object responseValue)
 {
     using (var request = new MemorySetup <Stream>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
         this.ShouldDirectlyStream(request, success: true);
 }
Exemple #12
0
 public void Stream_Ok_DiscardResponse(object responseValue)
 {
     using (var request = new MemorySetup <Stream>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
         this.ShouldDirectlyStream(request);
 }
Exemple #13
0
 public void Typed_Ok_KeepResponse(object responseValue)
 {
     using (var request = new MemorySetup <StandardResponse>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
         this.ShouldStreamOfCopy(request);
 }
Exemple #14
0
 public void String_Ok_DiscardResponse(object responseValue)
 {
     using (var request = new MemorySetup <string>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
         this.ShouldStreamOfCopy(request, keepRaw: false);
 }
Exemple #15
0
 public void DynamicDictionary_Ok_KeepResponse(object responseValue)
 {
     using (var request = new MemorySetup <DynamicDictionary>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream),
                client => client.Info()
                ))
         this.ShouldStreamOfCopy(request);
 }
Exemple #16
0
 public void DynamicDictionary_Ok_DiscardResponse(object responseValue)
 {
     using (var request = new MemorySetup <DynamicDictionary>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream),
                client => client.Info()
                ))
         this.ShouldDirectlyStream(request);
 }
 public async Task DynamicDictionary_Ok_KeepResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <DynamicDictionary>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream),
                c => c.InfoAsync()
                ).Init())
         this.ShouldStreamOfCopy(request);
 }
Exemple #18
0
        public async void DeadNodesAreNotVisited_AndPingedAppropiately_Async()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config);

                var getCall = FakeCalls.GetCall(fake);
                var ok      = Task.FromResult(FakeResponse.Ok(config));
                var bad     = Task.FromResult(FakeResponse.Bad(config));
                getCall.ReturnsNextFromSequence(
                    ok,                      //info 1 - 9204
                    bad,                     //info 2 - 9203 DEAD
                    ok,                      //info 2 retry - 9202
                    ok,                      //info 3 - 9201
                    ok,                      //info 4 - 9204
                    ok,                      //info 5 - 9202
                    ok,                      //info 6 - 9201
                    ok,                      //info 7 - 9204
                    ok,                      //info 8 - 9203 (Now > Timeout)
                    ok                       //info 9 - 9202
                    );

                var seenNodes = new List <Uri>();
                getCall.Invokes((Uri u, IRequestConnectionConfiguration o) => seenNodes.Add(u));

                var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingCall.Returns(ok);

                var client1 = fake.Resolve <ElasticsearchClient>();
                await client1.InfoAsync();                 //info call 1

                await client1.InfoAsync();                 //info call 2

                await client1.InfoAsync();                 //info call 3

                await client1.InfoAsync();                 //info call 4

                await client1.InfoAsync();                 //info call 5

                await client1.InfoAsync();                 //info call 6

                await client1.InfoAsync();                 //info call 7

                await client1.InfoAsync();                 //info call 8

                await client1.InfoAsync();                 //info call 9

                AssertSeenNodesAreInExpectedOrder(seenNodes);

                //4 nodes first time usage + 1 time after the first time 9203 came back to live
                pingCall.MustHaveHappened(Repeated.Exactly.Times(5));
            }
        }
Exemple #19
0
 public void VoidResponse_Ok_KeepResponse(object responseValue)
 {
     using (var request = new MemorySetup <VoidResponse>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
         //voidResponse NEVER reads the body so Raw is always false
         //and no intermediate stream should be created
         this.ShouldDirectlyStream(request);
 }
 public async Task VoidResponse_Ok_DiscardResponse(object responseValue)
 {
     using (var request = await new AsyncMemorySetup <VoidResponse>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ).Init())
         //voidResponse NEVER reads the body so Raw is always false
         //and no intermediate stream should be created
         this.ShouldDirectlyStream(request);
 }
        public void SniffCalledOnceAndEachEnpointPingedOnce()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                //It's recommended to only have on instance of your connection pool
                //Be sure to register it as Singleton in your IOC
                var uris           = new[] { new Uri("http://localhost:9200"), new Uri("http://localhost:9201") };
                var connectionPool = new SniffingConnectionPool(uris);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffOnStartup();
                fake.Provide <IConnectionConfigurationValues>(config);

                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));
                var sniffCall = FakeCalls.Sniff(fake, config, uris);
                var getCall   = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Ok(config));

                var transport1 = FakeCalls.ProvideRealTranportInstance(fake);
                var transport2 = FakeCalls.ProvideRealTranportInstance(fake);
                var transport3 = FakeCalls.ProvideRealTranportInstance(fake);
                var transport4 = FakeCalls.ProvideRealTranportInstance(fake);

                transport1.Should().NotBe(transport2);

                var client1 = new ElasticsearchClient(config, transport: transport1);
                client1.Info();
                client1.Info();
                client1.Info();
                client1.Info();
                var client2 = new ElasticsearchClient(config, transport: transport2);
                client2.Info();
                client2.Info();
                client2.Info();
                client2.Info();
                var client3 = new ElasticsearchClient(config, transport: transport3);
                client3.Info();
                client3.Info();
                client3.Info();
                client3.Info();
                var client4 = new ElasticsearchClient(config, transport: transport4);
                client4.Info();
                client4.Info();
                client4.Info();
                client4.Info();

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                //sniff validates first node, one new node should be pinged before usage.
                pingCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
Exemple #22
0
 public void String_Ok_KeepResponse(object responseValue)
 {
     using (var request = new Requester <string>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
     {
         var r = request.Result;
         r.Success.Should().BeTrue();
         Encoding.UTF8.GetBytes(r.Response).Should().NotBeNull().And.BeEquivalentTo(request.ResponseBytes);
         r.ResponseRaw.Should().NotBeNull().And.BeEquivalentTo(request.ResponseBytes);
     }
 }
Exemple #23
0
        public void SniffIsCalledAfterItHasGoneOutOfDate_NotWhenItSeesA503()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                var nowCall          = A.CallTo(() => dateTimeProvider.Now());
                nowCall.ReturnsNextFromSequence(
                    DateTime.UtcNow,                     //initial sniff time (set even if not sniff_on_startup
                    DateTime.UtcNow,                     //info call 1
                    DateTime.UtcNow,                     //info call 2
                    DateTime.UtcNow.AddMinutes(10),      //info call 3
                    DateTime.UtcNow.AddMinutes(10),      //set now after sniff 3
                    DateTime.UtcNow.AddMinutes(10),      //info call 4
                    DateTime.UtcNow.AddMinutes(12)       //info call 5
                    );
                var uris           = new[] { new Uri("http://localhost:9200") };
                var connectionPool = new SniffingConnectionPool(uris);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffLifeSpan(TimeSpan.FromMinutes(4))
                                     .ExposeRawResponse();
                fake.Provide <IConnectionConfigurationValues>(config);
                var transport = FakeCalls.ProvideDefaultTransport(fake, dateTimeProvider);

                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var sniffCall = FakeCalls.Sniff(fake, config, uris);
                var getCall   = FakeCalls.GetSyncCall(fake);
                getCall.ReturnsNextFromSequence(
                    FakeResponse.Ok(config),                     //info 1
                    FakeResponse.Ok(config),                     //info 2
                    FakeResponse.Ok(config),                     //info 3
                    FakeResponse.Ok(config),                     //sniff
                    FakeResponse.Ok(config),                     //info 4
                    FakeResponse.Bad(config)                     //info 5
                    );

                var client1 = fake.Resolve <ElasticsearchClient>();
                var result  = client1.Info();            //info call 1
                result = client1.Info();                 //info call 2
                result = client1.Info();                 //info call 3
                result = client1.Info();                 //info call 4
                result = client1.Info();                 //info call 5

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                nowCall.MustHaveHappened(Repeated.Exactly.Times(7));

                //var nowCall = A.CallTo(() => fake.Resolve<IDateTimeProvider>().Sniff(A<Uri>._, A<int>._));
            }
        }
Exemple #24
0
        public void ShouldRetryOnSniffConnectionException_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var uris = new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201"),
                    new Uri("http://localhost:9202")
                };
                var connectionPool = new SniffingConnectionPool(uris, randomizeOnStartup: false);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffOnConnectionFault();

                fake.Provide <IConnectionConfigurationValues>(config);

                var pingAsyncCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingAsyncCall.Returns(FakeResponse.OkAsync(config));

                //sniffing is always synchronous and in turn will issue synchronous pings
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var sniffCall = FakeCalls.Sniff(fake);
                var seenPorts = new List <int>();
                sniffCall.ReturnsLazily((Uri u, IRequestConfiguration c) =>
                {
                    seenPorts.Add(u.Port);
                    throw new Exception("Something bad happened");
                });

                var getCall = FakeCalls.GetCall(fake);
                getCall.Returns(FakeResponse.BadAsync(config));

                FakeCalls.ProvideDefaultTransport(fake);

                var client = fake.Resolve <ElasticsearchClient>();

                var e = Assert.Throws <MaxRetryException>(async() => await client.NodesHotThreadsAsync("nodex"));

                //all nodes must be tried to sniff for more information
                sniffCall.MustHaveHappened(Repeated.Exactly.Times(uris.Count()));
                //make sure we only saw one call to hot threads (the one that failed initially)
                getCall.MustHaveHappened(Repeated.Exactly.Once);

                //make sure the sniffs actually happened on all the individual nodes
                seenPorts.ShouldAllBeEquivalentTo(uris.Select(u => u.Port));
                e.InnerException.Message.Should().Contain("Sniffing known nodes");
            }
        }
Exemple #25
0
 public void ByteArray_Ok_DiscardResponse(object responseValue)
 {
     using (var request = new Requester <byte[]>(
                responseValue,
                settings => settings.ExposeRawResponse(false),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
     {
         var r = request.Result;
         r.Success.Should().BeTrue();
         r.Response.Should().NotBeNull().And.BeEquivalentTo(request.ResponseBytes);
         r.ResponseRaw.Should().BeNull();
     }
 }
Exemple #26
0
 public void VoidResponse_Ok_KeepResponse(object responseValue)
 {
     using (var request = new Requester <VoidResponse>(
                responseValue,
                settings => settings.ExposeRawResponse(true),
                (settings, stream) => FakeResponse.Ok(settings, response: stream)
                ))
     {
         var r = request.Result;
         r.Success.Should().BeTrue();
         //Response and rawresponse should ALWAYS be null for VoidResponse responses
         r.Response.Should().BeNull();
         r.ResponseRaw.Should().BeNull();
     }
 }
Exemple #27
0
        public void SniffOnConnectionFaultCausesSniffOn503()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                var nowCall          = A.CallTo(() => dateTimeProvider.Now());
                nowCall.Invokes(() =>
                {
                });
                nowCall.Returns(DateTime.UtcNow);
                var nodes          = new[] { new Uri("http://localhost:9200") };
                var connectionPool = new SniffingConnectionPool(nodes);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffOnConnectionFault();
                fake.Provide <IConnectionConfigurationValues>(config);
                var transport  = FakeCalls.ProvideDefaultTransport(fake, dateTimeProvider);
                var connection = fake.Resolve <IConnection>();

                var sniffNewNodes = new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201")
                };
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var sniffCall = FakeCalls.Sniff(fake, config, sniffNewNodes);
                var getCall   = FakeCalls.GetSyncCall(fake);
                getCall.ReturnsNextFromSequence(

                    FakeResponse.Ok(config),                     //info 1
                    FakeResponse.Ok(config),                     //info 2
                    FakeResponse.Ok(config),                     //info 3
                    FakeResponse.Ok(config),                     //info 4
                    FakeResponse.Bad(config)                     //info 5
                    );

                var client1 = fake.Resolve <ElasticsearchClient>();
                client1.Info();                                          //info call 1
                client1.Info();                                          //info call 2
                client1.Info();                                          //info call 3
                client1.Info();                                          //info call 4
                Assert.Throws <MaxRetryException>(() => client1.Info()); //info call 5

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                nowCall.MustHaveHappened(Repeated.Exactly.Times(8));
            }
        }
Exemple #28
0
        public void Typed_Ok_KeepResponse(object responseValue)
        {
            using (var request = new Requester <StandardResponse>(
                       responseValue,
                       settings => settings.ExposeRawResponse(true),
                       (settings, stream) => FakeResponse.Ok(settings, response: stream)
                       ))
            {
                var r = request.Result;
                r.Success.Should().BeTrue();
                object v = r.Response.value;

                v.ShouldBeEquivalentTo(responseValue);
                r.ResponseRaw.Should().NotBeNull().And.BeEquivalentTo(request.ResponseBytes);
            }
        }
Exemple #29
0
        public void Typed_Ok_DiscardResponse(object responseValue)
        {
            using (var request = new Requester <Document>(
                       responseValue,
                       settings => settings.ExposeRawResponse(false),
                       (settings, stream) => FakeResponse.Ok(settings, response: stream)
                       ))
            {
                var r = request.Result;
                r.Success.Should().BeTrue();
                object v = r.Response.value;

                v.ShouldBeEquivalentTo(responseValue);
                r.ResponseRaw.Should().BeNull();
            }
        }
Exemple #30
0
        public void DynamicDictionary_Ok_KeepResponse(object responseValue)
        {
            using (var request = new Requester <DynamicDictionary>(
                       responseValue,
                       settings => settings.ExposeRawResponse(true),
                       (settings, stream) => FakeResponse.Ok(settings, response: stream),
                       client => client.Info()
                       ))
            {
                var r = request.Result;
                r.Success.Should().BeTrue();
                object v = r.Response["value"];

                v.ShouldBeEquivalentTo(responseValue);
                r.ResponseRaw.Should().NotBeNull().And.BeEquivalentTo(request.ResponseBytes);
            }
        }