Exemple #1
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 #2
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 #3
0
        private void CallAsync(int status, string exceptionType, string exceptionMessage, AutoFake fake, MemoryStream response, bool exposeRawResponse = false)
        {
            var connectionConfiguration = new ConnectionConfiguration()
                                          .ThrowOnElasticsearchServerExceptions()
                                          .ExposeRawResponse(exposeRawResponse);

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

            var getCall = FakeCalls.GetCall(fake);

            getCall.Returns(FakeResponse.BadAsync(connectionConfiguration, response: response));

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

            var e = Assert.Throws <ElasticsearchServerException>(async() => await client.InfoAsync());

            AssertServerErrorsException(e, status, exceptionType, exceptionMessage);
            getCall.MustHaveHappened(Repeated.Exactly.Once);
        }
        private async Task <ElasticsearchResponse <DynamicDictionary> > CallAsync(int status, string exceptionType, string exceptionMessage, AutoFake fake, MemoryStream response, bool exposeRawResponse = false)
        {
            var connectionConfiguration = new ConnectionConfiguration()
                                          .ExposeRawResponse(exposeRawResponse);

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

            var getCall = FakeCalls.GetCall(fake);

            getCall.Returns(FakeResponse.BadAsync(connectionConfiguration, response: response));

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

            var result = await client.InfoAsync();

            result.Success.Should().BeFalse();
            AssertServerErrorsOnResponse(result, status, exceptionType, exceptionMessage);
            getCall.MustHaveHappened(Repeated.Exactly.Once);
            return(result);
        }
Exemple #5
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);
            }
        }