Skip to content

Commit 0718f0f

Browse files
committed
started on skip nodes tests
1 parent 5db1ce4 commit 0718f0f

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

src/Elasticsearch.Net.Tests.Unit/Elasticsearch.Net.Tests.Unit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Reference Include="System.Xml" />
6161
</ItemGroup>
6262
<ItemGroup>
63+
<Compile Include="Connection\SkipDeadNodesTests.cs" />
6364
<Compile Include="Connection\SniffingConnectionPoolTests.cs" />
6465
<Compile Include="Connection\ConcurrencyTests.cs" />
6566
<Compile Include="Connection\StaticConnectionPoolRetryTests.cs" />

src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public virtual Uri GetNext(int? initialSeed, out int seed)
4949
var state = this._uriLookup[uri];
5050
lock (state)
5151
{
52-
if (state.date <= _dateTimeProvider.Now())
52+
var now = _dateTimeProvider.Now();
53+
if (state.date <= now)
5354
{
5455
state._attempts = 0;
5556
return uri;
@@ -58,10 +59,11 @@ public virtual Uri GetNext(int? initialSeed, out int seed)
5859
Interlocked.Increment(ref state._attempts);
5960
++attempts;
6061
i = (++initialOffset) % count;
62+
seed = i;
6163
} while (attempts < count);
6264

6365
//could not find a suitable node retrying on node that has been dead longest.
64-
return this._nodeUris[0]; //todo random;
66+
return this._nodeUris[i]; //todo random;
6567
}
6668

6769
public virtual void MarkDead(Uri uri)
@@ -82,7 +84,8 @@ public virtual void MarkAlive(Uri uri)
8284
return;
8385
lock (state)
8486
{
85-
state.date = this._dateTimeProvider.AliveTime(uri, state._attempts);
87+
var aliveTime =this._dateTimeProvider.AliveTime(uri, state._attempts);
88+
state.date = aliveTime;
8689
state._attempts = 0;
8790
}
8891
}

0 commit comments

Comments
 (0)