|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Autofac; |
| 10 | +using Autofac.Core.Activators.Reflection; |
| 11 | +using Autofac.Extras.FakeItEasy; |
| 12 | +using Elasticsearch.Net.Connection; |
| 13 | +using Elasticsearch.Net.ConnectionPool; |
| 14 | +using Elasticsearch.Net.Exceptions; |
| 15 | +using Elasticsearch.Net.Providers; |
| 16 | +using Elasticsearch.Net.Tests.Unit.Stubs; |
| 17 | +using FakeItEasy; |
| 18 | +using FluentAssertions; |
| 19 | +using NUnit.Framework; |
| 20 | + |
| 21 | +namespace Elasticsearch.Net.Tests.Unit.Connection |
| 22 | +{ |
| 23 | + [TestFixture] |
| 24 | + public class SkipDeadNodesTests |
| 25 | + { |
| 26 | + [Test] |
| 27 | + public void DeadNodesAreNotVisited() |
| 28 | + { |
| 29 | + using (var fake = new AutoFake()) |
| 30 | + { |
| 31 | + var now = DateTime.UtcNow; |
| 32 | + var dateTimeProvider = fake.Resolve<IDateTimeProvider>(); |
| 33 | + var nowCall = A.CallTo(()=>dateTimeProvider.Now()); |
| 34 | + nowCall.ReturnsNextFromSequence( |
| 35 | + DateTime.UtcNow, //info 1 |
| 36 | + DateTime.UtcNow, //info 2 |
| 37 | + DateTime.UtcNow, //info 2 retry |
| 38 | + DateTime.UtcNow, //info 3 |
| 39 | + DateTime.UtcNow, //info 4 |
| 40 | + DateTime.UtcNow, //info 5 pass over node 3 |
| 41 | + DateTime.UtcNow, //info 5 |
| 42 | + DateTime.UtcNow, //info 6 |
| 43 | + DateTime.UtcNow.AddMinutes(2), //info 7 |
| 44 | + DateTime.UtcNow.AddMinutes(2), //info 8 |
| 45 | + DateTime.UtcNow.AddMinutes(2) //info 9 |
| 46 | + ); |
| 47 | + A.CallTo(()=>dateTimeProvider.AliveTime(A<Uri>._, A<int>._)) |
| 48 | + .Returns(new DateTime()); |
| 49 | + A.CallTo(() => dateTimeProvider.DeadTime(A<Uri>._, A<int>._)) |
| 50 | + .Returns(DateTime.UtcNow.AddMinutes(1)); |
| 51 | + //make sure the transport layer uses a different datetimeprovider |
| 52 | + fake.Provide<IDateTimeProvider>(new DateTimeProvider()); |
| 53 | + var connectionPool = new StaticConnectionPool(new[] |
| 54 | + { |
| 55 | + new Uri("http://localhost:9204"), |
| 56 | + new Uri("http://localhost:9203"), |
| 57 | + new Uri("http://localhost:9202"), |
| 58 | + new Uri("http://localhost:9201") |
| 59 | + }, randomizeOnStartup: false, dateTimeProvider: dateTimeProvider); |
| 60 | + var config = new ConnectionConfiguration(connectionPool); |
| 61 | + fake.Provide<IConnectionConfigurationValues>(config); |
| 62 | + fake.Provide<ITransport>(fake.Resolve<Transport>()); |
| 63 | + var connection = fake.Resolve<IConnection>(); |
| 64 | + |
| 65 | + var seenNodes = new List<Uri>(); |
| 66 | + var getCall = A.CallTo(() => connection.GetSync(A<Uri>._)); |
| 67 | + getCall.ReturnsNextFromSequence( |
| 68 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 1 - 9204 |
| 69 | + ElasticsearchResponse.Create(config, 503, "GET", "/", null, null), //info 2 - 9203 DEAD |
| 70 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 2 retry - 9202 |
| 71 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 3 - 9201 |
| 72 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 4 - 9204 |
| 73 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 5 - 9202 |
| 74 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 6 - 9201 |
| 75 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 7 - 9204 |
| 76 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null), //info 8 - 9203 (Now > Timeout) |
| 77 | + ElasticsearchResponse.Create(config, 200, "GET", "/", null, null) //info 9 - 9202 |
| 78 | + ); |
| 79 | + getCall.Invokes((Uri u) => seenNodes.Add(u)); |
| 80 | + |
| 81 | + var client1 = fake.Resolve<ElasticsearchClient>(); |
| 82 | + client1.Info(); //info call 1 |
| 83 | + client1.Info(); //info call 2 |
| 84 | + client1.Info(); //info call 3 |
| 85 | + client1.Info(); //info call 4 |
| 86 | + client1.Info(); //info call 5 |
| 87 | + client1.Info(); //info call 6 |
| 88 | + client1.Info(); //info call 7 |
| 89 | + client1.Info(); //info call 8 |
| 90 | + client1.Info(); //info call 9 |
| 91 | + |
| 92 | + seenNodes.Should().NotBeEmpty().And.HaveCount(10); |
| 93 | + seenNodes[0].Port.Should().Be(9204); |
| 94 | + seenNodes[1].Port.Should().Be(9203); |
| 95 | + //after sniff |
| 96 | + seenNodes[2].Port.Should().Be(9202); |
| 97 | + seenNodes[3].Port.Should().Be(9201); |
| 98 | + seenNodes[4].Port.Should().Be(9204); |
| 99 | + seenNodes[5].Port.Should().Be(9202); |
| 100 | + seenNodes[6].Port.Should().Be(9201); |
| 101 | + seenNodes[7].Port.Should().Be(9204); |
| 102 | + seenNodes[8].Port.Should().Be(9203); |
| 103 | + |
| 104 | + //var nowCall = A.CallTo(() => fake.Resolve<IDateTimeProvider>().Sniff(A<Uri>._, A<int>._)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments