예제 #1
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);
            }
예제 #2
0
        public async void ThrowsOutOfNodesException_AndRetriesTheSpecifiedTimes_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);
                var getCall = FakeCalls.GetCall(fake);

                //return a started task that throws
                Func <ElasticsearchResponse <Stream> > badTask = () => { throw new Exception(); };
                var t = new Task <ElasticsearchResponse <Stream> >(badTask);
                t.Start();
                getCall.Returns(t);

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

                client.Settings.MaxRetries.Should().Be(_retries);
                try
                {
                    var result = await client.InfoAsync();
                }
                catch (MaxRetryException e)
                {
                    Assert.AreEqual(typeof(MaxRetryException), e.GetType());
                }
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
        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);
            }
        }
예제 #4
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);
        }
        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);
            }
        }
예제 #6
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);
            }
        }
예제 #7
0
        public async Task <AsyncMemorySetup <T> > Init()
        {
            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.GetCall(this.Fake);
            this.GetCall.Returns(response);

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

            this.Result = await(_call != null ? _call(client) : client.InfoAsync <T>());

            this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
            return(this);
        }
        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");
            }
        }
        [Ignore]         //TODO Unignore
        public void CallInfo40000TimesOnMultipleThreads()
        {
            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);
                //we want to use our special concurrencytestconnection
                //this randonly throws on any node but 9200 and sniffing will represent a different
                //view of the cluster each time but always holding node 9200
                fake.Provide <IConnection>(new ConcurrencyTestConnection(this._config));
                //prove a real Transport with its unspecified dependencies
                //as fakes
                FakeCalls.ProvideDefaultTransport(fake);

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

                //We'll call Info() 10.000 times on 4 threads
                //This should not throw any exceptions even if connections sometime fail at a node level
                //because node 9200 is always up and running
                Assert.DoesNotThrow(() =>
                {
                    Action a = () =>
                    {
                        for (var i = 0; i < 10000; i++)
                        {
                            client.Info <VoidResponse>();
                            Interlocked.Increment(ref seen);
                        }
                    };
                    var thread1 = new Thread(() => a());
                    var thread2 = new Thread(() => a());
                    var thread3 = new Thread(() => a());
                    var thread4 = new Thread(() => a());
                    thread1.Start();
                    thread2.Start();
                    thread3.Start();
                    thread4.Start();
                    thread1.Join();
                    thread2.Join();
                    thread3.Join();
                    thread4.Join();
                });

                //we should have seen 40.000 increments
                //Sadly we can't use FakeItEasy's to ensure get is called 40.000 times
                //because it internally uses fixed arrays that will overflow :)
                seen.Should().Be(40000);
            }
        }
예제 #10
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");
            }
        }
예제 #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>._));
            }
        }
예제 #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));
            }
        }
예제 #13
0
        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);
            }
        }
예제 #14
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);
            }
        }
예제 #15
0
        public void ShouldRetryOn503_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetCall(fake);
                getCall.Returns(Task.FromResult(FakeResponse.Bad(_connectionConfig)));

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

                Assert.Throws <MaxRetryException>(async() => await client.InfoAsync());
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
예제 #16
0
        public void ShouldNotRetryWhenMaxRetriesIs0_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var connectionConfiguration = new ConnectionConfiguration().MaximumRetries(0);
                fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
                FakeCalls.ProvideDefaultTransport(fake);

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

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

                Assert.DoesNotThrow(async() => await client.InfoAsync());
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
예제 #17
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();
            }
        }
예제 #18
0
        public void Async_CallThrowsHardException_ShouldBubbleToCallee()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);
                var getCall = FakeCalls.GetCall(fake);

                //return a started task that throws
                getCall.Throws((c) => new Exception("hard exception!"));

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

                var e = Assert.Throws <Exception>(async() => await client.InfoAsync());
                e.Message.Should().Be("hard exception!");
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
예제 #19
0
        public async void ShouldNotRetryOn400_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);

                var getCall = FakeCalls.GetCall(fake);
                var task    = Task.FromResult(FakeResponse.Any(_connectionConfig, 400));
                getCall.Returns(task);

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

                var result = await client.InfoAsync();

                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
예제 #20
0
        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));
            }
        }
예제 #21
0
        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);
            }
        }
예제 #22
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);
            }
        }
        public void Hard_IConnectionException_OnAsync_ThrowsMaxRetry_AndRetries()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionPoolConfig);
                FakeCalls.ProvideDefaultTransport(fake);
                var getCall = FakeCalls.GetCall(fake);

                //return a started task that throws
                getCall.Throws((o) => new Exception("inner"));

                var client = fake.Resolve <ElasticsearchClient>();
                client.Settings.MaxRetries.Should().Be(_retries);

                var e = Assert.Throws <MaxRetryException>(async() => await client.InfoAsync());
                this.AssertMaxRetryException(e);
                getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
            }
        }
예제 #24
0
            public async Task Init()
            {
                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.GetCall(this.Fake);
                this.GetCall.Returns(response);

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

                this.Result = await(_call != null ? _call(client) : client.InfoAsync <T>());

                this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
            }
예제 #25
0
        public void ThrowsException_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                fake.Provide <IConnectionConfigurationValues>(_connectionConfig);
                FakeCalls.ProvideDefaultTransport(fake);
                var getCall = FakeCalls.GetCall(fake);

                //return a started task that throws
                Func <ElasticsearchResponse <Stream> > badTask = () => { throw new Exception(); };
                var t = new Task <ElasticsearchResponse <Stream> >(badTask);
                t.Start();
                getCall.Returns(t);

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

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

                //return a started task that throws
                getCall.Throws((o) => new Exception("inner"));

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

                client.Settings.MaxRetries.Should().NotHaveValue();

                var e = Assert.Throws <Exception>(async() => await client.InfoAsync());
                e.Message.Should().Be("inner");
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
예제 #27
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 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);
        }
예제 #29
0
        public void SniffOnStartupCallsSniffOnlyOnce()
        {
            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") };
                var connectionPool = new SniffingConnectionPool(uris);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .DisablePing()
                                     .SniffOnStartup();
                fake.Provide <IConnectionConfigurationValues>(config);
                var sniffCall = FakeCalls.Sniff(fake, config, uris);
                var transport = FakeCalls.ProvideDefaultTransport(fake);
                var client1   = new ElasticsearchClient(config, transport: transport);
                var client2   = new ElasticsearchClient(config, transport: transport);
                var client3   = new ElasticsearchClient(config, transport: transport);
                var client4   = new ElasticsearchClient(config, transport: transport);

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
예제 #30
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));
            }
        }