public void ShouldNotThrowAndNotRetry401()
        {
            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 pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

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

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

                Assert.DoesNotThrow(() => client.Info());
                pingCall.MustHaveHappened(Repeated.Exactly.Once);
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public void ThrowsOutOfNodesException_AndRetriesTheSpecifiedTimes()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                //set up connection configuration that holds a connection pool
                //with '_uris' (see the constructor)
                fake.Provide <IConnectionConfigurationValues>(_config);
                //prove a real HttpTransport with its unspecified dependencies
                //as fakes
                this.ProvideTransport(fake);

                //set up fake for a call on IConnection.GetSync so that it always throws
                //an exception
                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Throws <Exception>();
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(_ok);

                //create a real ElasticsearchClient with it unspecified dependencies
                //as fakes
                var client = fake.Resolve <ElasticsearchClient>();

                //we don't specify our own value so it should be up to the connection pool
                client.Settings.MaxRetries.Should().Be(null);

                //the default for the connection pool should be the number of nodes - 1;
                client.Settings.ConnectionPool.MaxRetries.Should().Be(_retries);

                //calling GET / should throw an OutOfNodesException because our fake
                //will always throw an exception
                Assert.Throws <MaxRetryException>(() => client.Info());
                //the GetSync method must in total have been called the number of nodes times.
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
Beispiel #3
0
            public Requester(
                object responseValue,
                Func <ConnectionConfiguration, ConnectionConfiguration> configSetup,
                Func <ConnectionConfiguration, Stream, ElasticsearchResponse <Stream> > responseSetup,
                Func <IElasticsearchClient, ElasticsearchResponse <T> > call = null
                )
            {
                var responseStream = CreateServerExceptionResponse(responseValue);

                this.Fake = new AutoFake(callsDoNothing: true);
                var connectionConfiguration = configSetup(new ConnectionConfiguration());
                var response = responseSetup(connectionConfiguration, responseStream);

                this.Fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
                FakeCalls.ProvideDefaultTransport(this.Fake);

                this.GetCall = FakeCalls.GetSyncCall(this.Fake);
                this.GetCall.Returns(response);

                var client = this.Fake.Resolve <ElasticsearchClient>();

                this.Result = call != null?call(client) : client.Info <T>();

                this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        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");
            }
        }
Beispiel #5
0
        public void ServerExceptionIsCaught_KeepResponse(int status, string exceptionType, string exceptionMessage)
        {
            var response = CreateServerExceptionResponse(status, exceptionType, exceptionMessage);

            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var connectionConfiguration = new ConnectionConfiguration()
                                              .ExposeRawResponse(true);

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

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

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

                var result = client.Info();
                result.Success.Should().BeFalse();
                AssertServerErrorsOnResponse(result, status, exceptionType, exceptionMessage);

                result.ResponseRaw.Should().NotBeNull();

                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        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);
            }
        }
Beispiel #7
0
        public MemorySetup(
            object responseValue,
            Func <ConnectionConfiguration, ConnectionConfiguration> configSetup,
            Func <ConnectionConfiguration, Stream, ElasticsearchResponse <Stream> > responseSetup,
            Func <IElasticsearchClient, ElasticsearchResponse <T> > call = null
            )
        {
            this.Fake = new AutoFake(callsDoNothing: true);
            var connectionConfiguration = configSetup(new ConnectionConfiguration());

            this.ResponseStream = CreateServerExceptionResponse(responseValue);
            var response = responseSetup(connectionConfiguration, this.ResponseStream);

            this.Fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
            this.MemoryProvider = this.Fake.Resolve <IMemoryStreamProvider>();
            A.CallTo(() => this.MemoryProvider.New()).ReturnsLazily((o) =>
            {
                var memoryStream = new TrackableMemoryStream();
                this._createdMemoryStreams.Add(memoryStream);
                return(memoryStream);
            });

            FakeCalls.ProvideDefaultTransport(this.Fake, memoryStreamProvider: this.MemoryProvider);

            this.GetCall = FakeCalls.GetSyncCall(this.Fake);
            this.GetCall.Returns(response);

            var client = this.Fake.Resolve <ElasticsearchClient>();

            this.Result = call != null?call(client) : client.Info <T>();

            this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
        }
Beispiel #8
0
        public void Should_Not_Retry_On_IConnection_Exception()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                //set up connection configuration that holds a connection pool
                //with '_uris' (see the constructor)
                fake.Provide <IConnectionConfigurationValues>(_config);
                //prove a real HttpTransport with its unspecified dependencies
                //as fakes
                this.ProvideTransport(fake);

                //set up fake for a call on IConnection.GetSync so that it always throws
                //an exception
                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.AnyWithException(_config, -1, innerException: new Exception("inner")));
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(_ok);

                //create a real ElasticsearchClient with it unspecified dependencies
                //as fakes
                var client = fake.Resolve <ElasticsearchClient>();

                //our settings dictate retrying 2 times
                client.Settings.MaxRetries.Should().Be(2);

                //the single node connection pool always reports 0 for maxretries
                client.Settings.ConnectionPool.MaxRetries.Should().Be(0);

                //
                var exception = Assert.Throws <Exception>(() => client.Info());
                exception.Message.Should().Be("inner");
                //the GetSync method must in total have been called the number of nodes times.
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public void IfAConnectionComesBackToLifeOnItsOwnItShouldBeMarked()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                //Setting up a datetime provider so that can track dead/alive marks
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                A.CallTo(() => dateTimeProvider.Now()).Returns(DateTime.UtcNow);
                var markDeadCall  = A.CallTo(() => dateTimeProvider.DeadTime(A <Uri> ._, A <int> ._, A <int?> ._, A <int?> ._));
                var markAliveCall = A.CallTo(() => dateTimeProvider.AliveTime(A <Uri> ._, A <int> ._));
                markDeadCall.Returns(DateTime.UtcNow.AddSeconds(60));
                markAliveCall.Returns(new DateTime());
                fake.Provide(dateTimeProvider);
                var connectionPool = new StaticConnectionPool(
                    _uris,
                    dateTimeProvider: dateTimeProvider);

                //set retries to 4
                fake.Provide <IConnectionConfigurationValues>(
                    new ConnectionConfiguration(connectionPool)
                    .MaximumRetries(4)
                    );

                //fake getsync handler that return a 503 4 times and then a 200
                //this will cause all 4 nodes to be marked dead on the first client call
                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.ReturnsNextFromSequence(
                    _bad,
                    _bad,
                    _bad,
                    _bad,
                    _ok
                    );
                //provide a transport with all the dependencies resolved
                var transport = this.ProvideTransport(fake);
                var pingCall  = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(_ok);

                //instantiate connection with faked dependencies
                var client = fake.Resolve <ElasticsearchClient>();

                //Do not throw because by miracle the 4th retry manages to give back a 200
                //even if all nodes have been marked dead.
                Assert.DoesNotThrow(() => client.Info());

                //original call + 4 retries is 5
                getCall.MustHaveHappened(Repeated.Exactly.Times(5));
                //4 nodes must be marked dead
                markDeadCall.MustHaveHappened(Repeated.Exactly.Times(4));
                //atleast one of them sprung back to live so markAlive must be called once
                markAliveCall.MustHaveHappened(Repeated.Exactly.Times(1));
            }
        }
        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);
            }
        }
Beispiel #11
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>._));
            }
        }
Beispiel #12
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));
            }
        }
Beispiel #13
0
        public void ThrowsException()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Throws <Exception>();

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

                Assert.Throws <Exception>(() => client.Info());
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public void ShouldNotRetryOn400()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Any(_connectionConfig, 400));

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

                Assert.DoesNotThrow(() => client.Info());
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
        public void ShouldRetryOn503()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Bad(_connectionConfig));

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

                Assert.Throws <MaxRetryException>(() => client.Info());
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
Beispiel #16
0
        public void DeadNodesAreNotVisited_AndPingedAppropiately()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config);

                var getCall = FakeCalls.GetSyncCall(fake);
                var ok      = FakeResponse.Ok(config);
                var bad     = 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.PingAtConnectionLevel(fake);
                pingCall.Returns(ok);

                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
                client1.Info();                 //info call 5
                client1.Info();                 //info call 6
                client1.Info();                 //info call 7
                client1.Info();                 //info call 8
                client1.Info();                 //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));
            }
        }
Beispiel #17
0
        public void ShouldNotRetryWhenMaxRetriesIs0()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var connectionConfiguration = new ConnectionConfiguration().MaximumRetries(0);
                fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Bad(connectionConfiguration));

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

                Assert.DoesNotThrow(() => client.Info());
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
Beispiel #18
0
        public void ShouldRetryOnSniff500()
        {
            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);
                FakeCalls.ProvideDefaultTransport(fake);

                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.Bad(config));
                });

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

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

                var e = Assert.Throws <MaxRetryException>(() => client.Info());
                sniffCall.MustHaveHappened(Repeated.Exactly.Times(uris.Count()));
                getCall.MustHaveHappened(Repeated.Exactly.Once);

                //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 sniffException = e.InnerException as SniffException;
                sniffException.Should().NotBeNull();
            }
        }
        public void ThrowsMaxRetryException_AndRetriesTheSpecifiedTimes()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Throws <Exception>();

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

                client.Settings.MaxRetries.Should().Be(_retries);

                Assert.Throws <MaxRetryException>(() => client.Info());
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
        public void OnConnectionException_WithoutPooling_DoNotRetry()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetSyncCall(fake);
                getCall.Throws((o) => new Exception("inner"));

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

                client.Settings.MaxRetries.Should().Be(_retries);

                var e = Assert.Throws <Exception>(() => client.Info());
                e.Message.Should().Be("inner");
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
Beispiel #21
0
        private void Call(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.GetSyncCall(fake);

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

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

            var e = Assert.Throws <ElasticsearchServerException>(() => client.Info());

            AssertServerErrorsException(e, status, exceptionType, exceptionMessage);
            getCall.MustHaveHappened(Repeated.Exactly.Once);
        }
        private ElasticsearchResponse <DynamicDictionary> Call(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.GetSyncCall(fake);

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

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

            var result = client.Info();

            result.Success.Should().BeFalse();
            AssertServerErrorsOnResponse(result, status, exceptionType, exceptionMessage);
            getCall.MustHaveHappened(Repeated.Exactly.Once);
            return(result);
        }
Beispiel #23
0
        public void ShouldThrowAndNotRetrySniffInformationIsTooOld401()
        {
            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)
                                     .SniffLifeSpan(new TimeSpan(1))
                                     .ThrowOnElasticsearchServerExceptions();

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

                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.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Bad(config));

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

                var e = Assert.Throws <ElasticsearchServerException>(() => client.Info());
                e.Status.Should().Be(401);
                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                getCall.MustNotHaveHappened();
            }
        }
Beispiel #24
0
        public void SniffIsCalledAfterItHasGoneOutOfDate()
        {
            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(20),      //info call 4
                    DateTime.UtcNow.AddMinutes(20),      //set now after sniff 4
                    DateTime.UtcNow.AddMinutes(22)       //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));
                fake.Provide <IConnectionConfigurationValues>(config);
                var transport  = FakeCalls.ProvideDefaultTransport(fake, dateTimeProvider);
                var connection = fake.Resolve <IConnection>();
                var sniffCall  = FakeCalls.Sniff(fake, config, uris);

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

                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
                client1.Info();                 //info call 5

                sniffCall.MustHaveHappened(Repeated.Exactly.Twice);
                nowCall.MustHaveHappened(Repeated.Exactly.Times(8));
            }
        }
Beispiel #25
0
        public void ShouldThrowAndNotRetrySniffOnStartup401()
        {
            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)
                                     .SniffOnStartup();

                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.GetSyncCall(fake);
                getCall.Returns(FakeResponse.Bad(config));

                fake.Provide <IConnectionConfigurationValues>(config);

                var e = Assert.Throws <ElasticsearchAuthenticationException>(() => FakeCalls.ProvideRealTranportInstance(fake));

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                getCall.MustNotHaveHappened();
            }
        }
        public void FailEarlyIfTimeoutIsExhausted()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config, dateTimeProvider);

                var getCall = FakeCalls.GetSyncCall(fake);
                var ok      = FakeResponse.Ok(config);
                var bad     = FakeResponse.Bad(config);
                getCall.ReturnsNextFromSequence(
                    bad,                    //info 1 - 9204
                    bad,                    //info 2 - 9203 DEAD
                    ok                      //info 2 retry - 9202
                    );

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

                var pingCall = FakeCalls.PingAtConnectionLevel(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>(() => client1.Info());
                e.Message.Should()
                .StartWith("Retry timeout 00:01:20 was hit after retrying 1 times:");

                IElasticsearchResponse response = null;
                Assert.DoesNotThrow(() => response = client1.Info());
                response.Should().NotBeNull();
                response.Success.Should().BeTrue();
            }
        }
        public void DeadNodeIsSkipped_AndRevivedAfterTimeout()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config);

                var getCall = FakeCalls.GetSyncCall(fake);
                var ok      = FakeResponse.Ok(config);
                var bad     = 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, IRequestConfiguration o) => seenNodes.Add(u));

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

                var client1 = fake.Resolve <ElasticsearchClient>();
                var result  = client1.Info();                //info call 1//first time node is used so a ping is sent first
                result.Metrics.Requests.Count.Should().Be(2);
                result.Metrics.Requests.First().RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests.First().Node.Port.Should().Be(9204);
                result.Metrics.Requests.Last().RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests.Last().Node.Port.Should().Be(9204);

                result = client1.Info();                 //info call 2
                //using 9203 for the first time ping succeeds
                result.Metrics.Requests.First().RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests.First().Node.Port.Should().Be(9203);
                result.Metrics.Requests.First().Success.Should().BeTrue();

                //call on 9203 fails
                result.Metrics.Requests[1].RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests[1].Node.Port.Should().Be(9203);
                result.Metrics.Requests[1].Success.Should().BeFalse();
                result.Metrics.Requests[1].HttpStatusCode.Should().Be(503);

                //using 9202 for the first time ping succeeds
                result.Metrics.Requests[2].RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests[2].Node.Port.Should().Be(9202);

                //call on 9203 fails
                result.Metrics.Requests[3].RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests[3].Node.Port.Should().Be(9202);
                result.Metrics.Requests[3].Success.Should().BeTrue();
                result.Metrics.Requests[3].HttpStatusCode.Should().Be(200);
                result = client1.Info();                 //info call 3
                result = client1.Info();                 //info call 4
                result = client1.Info();                 //info call 5
                result = client1.Info();                 //info call 6
                result = client1.Info();                 //info call 7
                result = client1.Info();                 //info call 8
                result = client1.Info();                 //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));
            }
        }
        public void IfAllButOneConnectionDiesSubsequentRequestsMustUseTheOneAliveConnection()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                //Setting up a datetime provider so that we can measure which
                //nodes have been marked alive and dead.
                //we provide a different fake for the method call with the last node
                //as argument.
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                A.CallTo(() => dateTimeProvider.Now()).Returns(DateTime.UtcNow);
                var markOthersDeadCall = A.CallTo(() => dateTimeProvider
                                                  .DeadTime(A <Uri> .That.Not.Matches(u => u.Port == 9203), A <int> ._, A <int?> ._, A <int?> ._));
                var markLastDead = A.CallTo(() => dateTimeProvider
                                            .DeadTime(A <Uri> .That.Matches(u => u.Port == 9203), A <int> ._, A <int?> ._, A <int?> ._));
                var markOthersAliveCall = A.CallTo(() => dateTimeProvider
                                                   .AliveTime(A <Uri> .That.Not.Matches(u => u.Port == 9203), A <int> ._));
                var markLastAlive = A.CallTo(() => dateTimeProvider
                                             .AliveTime(A <Uri> .That.Matches(u => u.Port == 9203), A <int> ._));
                markOthersDeadCall.Returns(DateTime.UtcNow.AddSeconds(60));
                markLastAlive.Returns(new DateTime());
                fake.Provide(dateTimeProvider);

                //Creating the connection pool making sure nodes are not randomized
                //So we are sure 9203 is that last node in the pool
                var connectionPool = new StaticConnectionPool(
                    _uris,
                    randomizeOnStartup: false,
                    dateTimeProvider: dateTimeProvider
                    );
                var config = new ConnectionConfiguration(connectionPool);
                fake.Provide <IConnectionConfigurationValues>(config);

                // provide a simple fake for synchronous get
                var getCall = FakeCalls.GetSyncCall(fake);

                //The first three tries get a 503 causing the first 3 nodes to be marked dead
                //all the subsequent requests should be handled by 9203 which gives a 200 4 times
                getCall.ReturnsNextFromSequence(
                    FakeResponse.Bad(config),
                    FakeResponse.Bad(config),
                    FakeResponse.Bad(config),
                    FakeResponse.Ok(config),
                    FakeResponse.Ok(config),
                    FakeResponse.Ok(config),
                    FakeResponse.Ok(config)
                    );
                //provide a transport with all the dependencies resolved
                var transport = this.ProvideTransport(fake);
                var pingCall  = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(_ok);

                //instantiate connection with faked dependencies
                var client = fake.Resolve <ElasticsearchClient>();

                //We call the root for each node 4 times, eventhough the first 3 nodes
                //give back a timeout the default ammount of retries is 4 (each provided node)
                //and the last retry will hit our 9203 node.
                Assert.DoesNotThrow(() => client.Info());
                //These calls should not hit the dead nodes and go straight to the active 9203
                Assert.DoesNotThrow(() => client.Info());
                Assert.DoesNotThrow(() => client.Info());
                Assert.DoesNotThrow(() => client.Info());

                //The last node should never be marked dead
                markLastDead.MustNotHaveHappened();
                //the other nodes should never be marked alive
                markOthersAliveCall.MustNotHaveHappened();
                //marking the other 3 nodes dead should only happen once for each
                markOthersDeadCall.MustHaveHappened(Repeated.Exactly.Times(3));
                //every time a connection succeeds on a node it will be marked
                //alive therefor the last node should be marked alive 4 times
                markLastAlive.MustHaveHappened(Repeated.Exactly.Times(4));
            }
        }