|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.IO;
|
4 | 4 | using System.Linq;
|
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Threading.Tasks; |
5 | 7 | using Autofac;
|
6 | 8 | using Autofac.Extras.FakeItEasy;
|
7 | 9 | using Elasticsearch.Net.Connection;
|
@@ -372,5 +374,77 @@ public void IfAllButOneConnectionDiesSubsequentRequestsMustUseTheOneAliveConnect
|
372 | 374 | markLastAlive.MustHaveHappened(Repeated.Exactly.Times(4));
|
373 | 375 | }
|
374 | 376 | }
|
| 377 | + |
| 378 | + [Test] |
| 379 | + public void ShouldRetryOnPingConnectionException_Async() |
| 380 | + { |
| 381 | + using (var fake = new AutoFake(callsDoNothing: true)) |
| 382 | + { |
| 383 | + var connectionPool = new StaticConnectionPool(_uris, randomizeOnStartup: false); |
| 384 | + var config = new ConnectionConfiguration(connectionPool); |
| 385 | + |
| 386 | + fake.Provide<IConnectionConfigurationValues>(config); |
| 387 | + FakeCalls.ProvideDefaultTransport(fake); |
| 388 | + |
| 389 | + var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake); |
| 390 | + var seenPorts = new List<int>(); |
| 391 | + pingCall.ReturnsLazily((Uri u, IRequestConfiguration c) => |
| 392 | + { |
| 393 | + seenPorts.Add(u.Port); |
| 394 | + throw new Exception("Something bad happened"); |
| 395 | + }); |
| 396 | + |
| 397 | + var getCall = FakeCalls.GetCall(fake); |
| 398 | + getCall.Returns(FakeResponse.OkAsync(config)); |
| 399 | + |
| 400 | + var client = fake.Resolve<ElasticsearchClient>(); |
| 401 | + |
| 402 | + var e = Assert.Throws<MaxRetryException>(async () => await client.InfoAsync()); |
| 403 | + pingCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1)); |
| 404 | + getCall.MustNotHaveHappened(); |
| 405 | + |
| 406 | + //make sure that if a ping throws an exception it wont |
| 407 | + //keep retrying to ping the same node but failover to the next |
| 408 | + seenPorts.ShouldAllBeEquivalentTo(_uris.Select(u=>u.Port)); |
| 409 | + var ae = e.InnerException as AggregateException; |
| 410 | + ae = ae.Flatten(); |
| 411 | + ae.InnerException.Message.Should().Be("Something bad happened"); |
| 412 | + } |
| 413 | + } |
| 414 | + |
| 415 | + [Test] |
| 416 | + public void ShouldRetryOnPingConnectionException() |
| 417 | + { |
| 418 | + using (var fake = new AutoFake(callsDoNothing: true)) |
| 419 | + { |
| 420 | + var connectionPool = new StaticConnectionPool(_uris, randomizeOnStartup: false); |
| 421 | + var config = new ConnectionConfiguration(connectionPool); |
| 422 | + |
| 423 | + fake.Provide<IConnectionConfigurationValues>(config); |
| 424 | + FakeCalls.ProvideDefaultTransport(fake); |
| 425 | + |
| 426 | + var pingCall = FakeCalls.PingAtConnectionLevel(fake); |
| 427 | + var seenPorts = new List<int>(); |
| 428 | + pingCall.ReturnsLazily((Uri u, IRequestConfiguration c) => |
| 429 | + { |
| 430 | + seenPorts.Add(u.Port); |
| 431 | + throw new Exception("Something bad happened"); |
| 432 | + }); |
| 433 | + |
| 434 | + var getCall = FakeCalls.GetSyncCall(fake); |
| 435 | + getCall.Returns(FakeResponse.Ok(config)); |
| 436 | + |
| 437 | + var client = fake.Resolve<ElasticsearchClient>(); |
| 438 | + |
| 439 | + var e = Assert.Throws<MaxRetryException>(() => client.Info()); |
| 440 | + pingCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1)); |
| 441 | + getCall.MustNotHaveHappened(); |
| 442 | + |
| 443 | + //make sure that if a ping throws an exception it wont |
| 444 | + //keep retrying to ping the same node but failover to the next |
| 445 | + seenPorts.ShouldAllBeEquivalentTo(_uris.Select(u=>u.Port)); |
| 446 | + e.InnerException.Message.Should().Be("Something bad happened"); |
| 447 | + } |
| 448 | + } |
375 | 449 | }
|
376 | 450 | }
|
0 commit comments