Skip to content

Commit 07c75f7

Browse files
Mpdreamzrusscam
authored andcommitted
Fix test for random initialized node (#4134)
The test was using a local method that yielded a single pool while later .Take(50) was requested on this sequence. This always yields an enumeration with 1 element which increases the chances for the assertion to turn false. Rather than relying on Thread.Sleep to create different seeded randoms the StaticConnectionPool now defines a protected constructor that allows one to pass in a seed value. (cherry picked from commit 9734f04)
1 parent dd7af74 commit 07c75f7

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/Elasticsearch.Net/ConnectionPool/StaticConnectionPool.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,35 @@ public StaticConnectionPool(IEnumerable<Uri> uris, bool randomize = true, IDateT
1414
: this(uris.Select(uri => new Node(uri)), randomize, dateTimeProvider) { }
1515

1616
public StaticConnectionPool(IEnumerable<Node> nodes, bool randomize = true, IDateTimeProvider dateTimeProvider = null)
17+
: this(nodes, randomize, randomizeSeed: null, dateTimeProvider) { }
18+
19+
protected StaticConnectionPool(IEnumerable<Node> nodes, bool randomize, int? randomizeSeed = null, IDateTimeProvider dateTimeProvider = null)
1720
{
18-
nodes.ThrowIfEmpty(nameof(nodes));
1921
Randomize = randomize;
22+
Random = !randomize || !randomizeSeed.HasValue
23+
? new Random()
24+
: new Random(randomizeSeed.Value);
25+
2026
Initialize(nodes, dateTimeProvider);
2127
}
2228

2329
//this constructor is protected because nodeScorer only makes sense on subclasses that support reseeding
2430
//otherwise just manually sort `nodes` before instantiating.
2531
protected StaticConnectionPool(IEnumerable<Node> nodes, Func<Node, float> nodeScorer, IDateTimeProvider dateTimeProvider = null)
2632
{
27-
nodes.ThrowIfEmpty(nameof(nodes));
2833
_nodeScorer = nodeScorer;
2934
Initialize(nodes, dateTimeProvider);
3035
}
3136

3237
private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvider)
3338
{
39+
40+
var nodesProvided = nodes?.ToList() ?? throw new ArgumentNullException(nameof(nodes));
41+
nodesProvided.ThrowIfEmpty(nameof(nodes));
3442
DateTimeProvider = dateTimeProvider ?? Net.DateTimeProvider.Default;
3543

3644
string scheme = null;
37-
foreach (var node in nodes)
45+
foreach (var node in nodesProvided)
3846
{
3947
if (scheme == null)
4048
{
@@ -45,7 +53,7 @@ private void Initialize(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvi
4553
throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes");
4654
}
4755

48-
InternalNodes = SortNodes(nodes)
56+
InternalNodes = SortNodes(nodesProvided)
4957
.DistinctBy(n => n.Uri)
5058
.ToList();
5159
LastUpdate = DateTimeProvider.Now();
@@ -86,7 +94,7 @@ protected List<Node> AliveNodes
8694
protected IDateTimeProvider DateTimeProvider { get; private set; }
8795

8896
protected List<Node> InternalNodes { get; set; }
89-
protected Random Random { get; } = new Random();
97+
protected Random Random { get; }
9098
protected bool Randomize { get; }
9199

92100
/// <summary>

src/Tests/Tests/ClientConcepts/ConnectionPooling/BuildingBlocks/ConnectionPooling.doc.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Elasticsearch.Net;
88
using FluentAssertions;
99
using Nest;
10+
using Tests.Configuration;
1011
using Tests.Framework;
1112
using Tests.XPack.Security.Privileges;
1213

@@ -214,20 +215,30 @@ [U] public void Static()
214215
}
215216
}
216217

218+
//hide
219+
private class SeededRandomConectionPool : StaticConnectionPool
220+
{
221+
public SeededRandomConectionPool(IEnumerable<Node> nodes, int seed)
222+
: base(nodes, randomize: true, randomizeSeed: seed, dateTimeProvider: null)
223+
{}
224+
}
225+
217226
// hide
218227
[U] public void RandomizedInitialNodes()
219228
{
220-
IEnumerable<StaticConnectionPool> CreatStaticConnectionPools()
229+
IEnumerable<StaticConnectionPool> CreateSeededPools(int nodeCount, int pools)
221230
{
222-
Thread.Sleep(1);
223-
var uris = Enumerable.Range(1, 50).Select(i => new Uri($"https://10.0.0.{i}:9200/"));
224-
yield return new StaticConnectionPool(uris);
231+
var seed = TestConfiguration.Instance.Seed;
232+
var nodes = Enumerable.Range(1, nodeCount)
233+
.Select(i => new Node(new Uri($"https://10.0.0.{i}:9200/")))
234+
.ToList();
235+
for(var i = 0; i < nodeCount; i++)
236+
yield return new SeededRandomConectionPool(nodes, seed + i);
225237
}
226238

227-
// assertion works on the probability of seeing a Uri other than https://10.0.0.1:9200/
228-
// as the first value over 50 runs, when randomized.
229-
CreatStaticConnectionPools()
230-
.Take(50)
239+
var connectionPools = CreateSeededPools(100, 100).ToList();
240+
connectionPools.Should().HaveCount(100);
241+
connectionPools
231242
.Select(p => p.CreateView().First().Uri.ToString())
232243
.All(uri => uri == "https://10.0.0.1:9200/")
233244
.Should()

0 commit comments

Comments
 (0)