|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Autofac; |
| 6 | +using Autofac.Extras.FakeItEasy; |
| 7 | +using Elasticsearch.Net.Connection; |
| 8 | +using Elasticsearch.Net.Connection.Configuration; |
| 9 | +using Elasticsearch.Net.ConnectionPool; |
| 10 | +using Elasticsearch.Net.Exceptions; |
| 11 | +using Elasticsearch.Net.Providers; |
| 12 | +using Elasticsearch.Net.Tests.Unit.Stubs; |
| 13 | +using FakeItEasy; |
| 14 | +using FluentAssertions; |
| 15 | +using NUnit.Framework; |
| 16 | + |
| 17 | +namespace Elasticsearch.Net.Tests.Unit.Failover.Timeout |
| 18 | +{ |
| 19 | + [TestFixture] |
| 20 | + public class DontRetryAfterDefaultTimeoutTests |
| 21 | + { |
| 22 | + [Test] |
| 23 | + public void FailEarlyIfTimeoutIsExhausted() |
| 24 | + { |
| 25 | + using (var fake = new AutoFake()) |
| 26 | + { |
| 27 | + var dateTimeProvider = ProvideDateTimeProvider(fake); |
| 28 | + var config = ProvideConfiguration(dateTimeProvider); |
| 29 | + var connection = ProvideConnection(fake, config, dateTimeProvider); |
| 30 | + |
| 31 | + var getCall = FakeCalls.GetSyncCall(fake); |
| 32 | + var ok = FakeResponse.Ok(config); |
| 33 | + var bad = FakeResponse.Bad(config); |
| 34 | + getCall.ReturnsNextFromSequence( |
| 35 | + bad, //info 1 - 9204 |
| 36 | + bad, //info 2 - 9203 DEAD |
| 37 | + ok //info 2 retry - 9202 |
| 38 | + ); |
| 39 | + |
| 40 | + var seenNodes = new List<Uri>(); |
| 41 | + getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u)); |
| 42 | + |
| 43 | + var pingCall = FakeCalls.PingAtConnectionLevel(fake); |
| 44 | + pingCall.Returns(ok); |
| 45 | + |
| 46 | + var client1 = fake.Resolve<ElasticsearchClient>(); |
| 47 | + |
| 48 | + //event though the third node should have returned ok, the first 2 calls took a minute |
| 49 | + var e = Assert.Throws<MaxRetryException>(() => client1.Info()); |
| 50 | + e.Message.Should() |
| 51 | + .StartWith("Retry timeout 00:01:00 was hit after retrying 1 times:"); |
| 52 | + |
| 53 | + IElasticsearchResponse response = null; |
| 54 | + Assert.DoesNotThrow(() => response = client1.Info() ); |
| 55 | + response.Should().NotBeNull(); |
| 56 | + response.Success.Should().BeTrue(); |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public void FailEarlyIfTimeoutIsExhausted_Async() |
| 63 | + { |
| 64 | + using (var fake = new AutoFake()) |
| 65 | + { |
| 66 | + var dateTimeProvider = ProvideDateTimeProvider(fake); |
| 67 | + var config = ProvideConfiguration(dateTimeProvider); |
| 68 | + var connection = ProvideConnection(fake, config, dateTimeProvider); |
| 69 | + |
| 70 | + var getCall = FakeCalls.GetCall(fake); |
| 71 | + var ok = Task.FromResult(FakeResponse.Ok(config)); |
| 72 | + var bad = Task.FromResult(FakeResponse.Bad(config)); |
| 73 | + getCall.ReturnsNextFromSequence( |
| 74 | + bad, |
| 75 | + bad, |
| 76 | + ok |
| 77 | + ); |
| 78 | + |
| 79 | + var seenNodes = new List<Uri>(); |
| 80 | + getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u)); |
| 81 | + |
| 82 | + var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake); |
| 83 | + pingCall.Returns(ok); |
| 84 | + |
| 85 | + var client1 = fake.Resolve<ElasticsearchClient>(); |
| 86 | + //event though the third node should have returned ok, the first 2 calls took a minute |
| 87 | + var e = Assert.Throws<MaxRetryException>(async () => await client1.InfoAsync()); |
| 88 | + e.Message.Should() |
| 89 | + .StartWith("Retry timeout 00:01:00 was hit after retrying 1 times:"); |
| 90 | + |
| 91 | + IElasticsearchResponse response = null; |
| 92 | + Assert.DoesNotThrow(async () => response = await client1.InfoAsync() ); |
| 93 | + response.Should().NotBeNull(); |
| 94 | + response.Success.Should().BeTrue(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static IConnection ProvideConnection(AutoFake fake, ConnectionConfiguration config, IDateTimeProvider dateTimeProvider) |
| 99 | + { |
| 100 | + fake.Provide<IConnectionConfigurationValues>(config); |
| 101 | + var param = new TypedParameter(typeof(IDateTimeProvider), dateTimeProvider); |
| 102 | + var transport = fake.Provide<ITransport, Transport>(param); |
| 103 | + var connection = fake.Resolve<IConnection>(); |
| 104 | + return connection; |
| 105 | + } |
| 106 | + |
| 107 | + private static ConnectionConfiguration ProvideConfiguration(IDateTimeProvider dateTimeProvider) |
| 108 | + { |
| 109 | + var connectionPool = new StaticConnectionPool(new[] |
| 110 | + { |
| 111 | + new Uri("http://localhost:9204"), |
| 112 | + new Uri("http://localhost:9203"), |
| 113 | + new Uri("http://localhost:9202"), |
| 114 | + new Uri("http://localhost:9201") |
| 115 | + }, randomizeOnStartup: false, dateTimeProvider: dateTimeProvider); |
| 116 | + var config = new ConnectionConfiguration(connectionPool).EnableMetrics(); |
| 117 | + return config; |
| 118 | + } |
| 119 | + |
| 120 | + private static IDateTimeProvider ProvideDateTimeProvider(AutoFake fake) |
| 121 | + { |
| 122 | + var now = DateTime.UtcNow; |
| 123 | + var dateTimeProvider = fake.Resolve<IDateTimeProvider>(); |
| 124 | + var nowCall = A.CallTo(() => dateTimeProvider.Now()); |
| 125 | + nowCall.ReturnsNextFromSequence( |
| 126 | + now, //initital sniff now from constructor |
| 127 | + now, //pool select next node |
| 128 | + now.AddSeconds(30), //info 1 took to long? |
| 129 | + now.AddSeconds(30), //pool select next node? |
| 130 | + now.AddMinutes(1) //info 2 took to long? |
| 131 | + ); |
| 132 | + A.CallTo(() => dateTimeProvider.AliveTime(A<Uri>._, A<int>._)).Returns(new DateTime()); |
| 133 | + //dead time will return a fixed timeout of 1 minute |
| 134 | + A.CallTo(() => dateTimeProvider.DeadTime(A<Uri>._, A<int>._, A<int?>._, A<int?>._)) |
| 135 | + .Returns(DateTime.UtcNow.AddMinutes(1)); |
| 136 | + //make sure the transport layer uses a different datetimeprovider |
| 137 | + fake.Provide<IDateTimeProvider>(new DateTimeProvider()); |
| 138 | + return dateTimeProvider; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments