public void IfResponseIsKnowError_DoNotRetry_ThrowServerException_Async(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.PingAtConnectionLevelAsync(fake);
                pingCall.Returns(FakeResponse.OkAsync(connectionConfiguration));

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

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

                var e = Assert.Throws <ElasticsearchServerException>(async() => await client.InfoAsync());
                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 void ShouldRetryOnPingConnectionException_Async()
        {
            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.PingAtConnectionLevelAsync(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.GetCall(fake);
                getCall.Returns(FakeResponse.OkAsync(config));

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

                var e = Assert.Throws <MaxRetryException>(async() => await client.InfoAsync());
                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 ae = e.InnerException as AggregateException;
                ae = ae.Flatten();
                ae.InnerException.Message.Should().Contain("Pinging");
            }
        }
Exemple #4
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 #5
0
        public void ShouldThrowAndNotRetrySniffOnConnectionFault401_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()
                                     .ThrowOnElasticsearchServerExceptions();

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

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

                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);
                    return(FakeResponse.Any(config, 401));
                });

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

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

                var e = Assert.Throws <ElasticsearchServerException>(async() => await client.InfoAsync());
                e.Status.Should().Be(401);
                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
Exemple #6
0
        public async void HostsReturnedBySniffAreVisited_Async()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                var nowCall          = A.CallTo(() => dateTimeProvider.Now());
                nowCall.Returns(DateTime.UtcNow);

                var connectionPool = new SniffingConnectionPool(new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201")
                }, randomizeOnStartup: false);
                var config = new ConnectionConfiguration(connectionPool)
                             .SniffOnConnectionFault();
                fake.Provide <IConnectionConfigurationValues>(config);
                FakeCalls.ProvideDefaultTransport(fake);
                FakeCalls.PingAtConnectionLevelAsync(fake)
                .ReturnsLazily(() =>
                               FakeResponse.OkAsync(config)
                               );

                var sniffCall = FakeCalls.Sniff(fake, config, new List <Uri>()
                {
                    new Uri("http://localhost:9204"),
                    new Uri("http://localhost:9203"),
                    new Uri("http://localhost:9202"),
                    new Uri("http://localhost:9201")
                });

                var connection = fake.Resolve <IConnection>();
                var seenNodes  = new List <Uri>();
                //var getCall =  FakeResponse.GetSyncCall(fake);
                var getCall = A.CallTo(() => connection.Get(
                                           A <Uri> .That.Not.Matches(u => u.AbsolutePath.StartsWith("/_nodes")),
                                           A <IRequestConfiguration> ._));
                getCall.ReturnsNextFromSequence(
                    FakeResponse.OkAsync(config),                    //info 1
                    FakeResponse.BadAsync(config),                   //info 2
                    FakeResponse.OkAsync(config),                    //info 2 retry
                    FakeResponse.OkAsync(config),                    //info 3
                    FakeResponse.OkAsync(config),                    //info 4
                    FakeResponse.OkAsync(config),                    //info 5
                    FakeResponse.OkAsync(config),                    //info 6
                    FakeResponse.OkAsync(config),                    //info 7
                    FakeResponse.OkAsync(config),                    //info 8
                    FakeResponse.OkAsync(config)                     //info 9
                    );
                getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u));

                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

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                seenNodes.Should().NotBeEmpty().And.HaveCount(10);
                seenNodes[0].Port.Should().Be(9200);
                seenNodes[1].Port.Should().Be(9201);
                //after sniff
                seenNodes[2].Port.Should().Be(9202, string.Join(",", seenNodes.Select(n => n.Port)));
                seenNodes[3].Port.Should().Be(9204);
                seenNodes[4].Port.Should().Be(9203);
                seenNodes[5].Port.Should().Be(9202);
                seenNodes[6].Port.Should().Be(9201);
            }
        }