Skip to content

Commit 9734f04

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.
1 parent feb5083 commit 9734f04

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
@@ -15,27 +15,35 @@ public StaticConnectionPool(IEnumerable<Uri> uris, bool randomize = true, IDateT
1515
: this(uris.Select(uri => new Node(uri)), randomize, dateTimeProvider) { }
1616

1717
public StaticConnectionPool(IEnumerable<Node> nodes, bool randomize = true, IDateTimeProvider dateTimeProvider = null)
18+
: this(nodes, randomize, randomizeSeed: null, dateTimeProvider) { }
19+
20+
protected StaticConnectionPool(IEnumerable<Node> nodes, bool randomize, int? randomizeSeed = null, IDateTimeProvider dateTimeProvider = null)
1821
{
19-
nodes.ThrowIfEmpty(nameof(nodes));
2022
Randomize = randomize;
23+
Random = !randomize || !randomizeSeed.HasValue
24+
? new Random()
25+
: new Random(randomizeSeed.Value);
26+
2127
Initialize(nodes, dateTimeProvider);
2228
}
2329

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

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

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

49-
InternalNodes = SortNodes(nodes)
57+
InternalNodes = SortNodes(nodesProvided)
5058
.DistinctBy(n => n.Uri)
5159
.ToList();
5260
LastUpdate = DateTimeProvider.Now();
@@ -87,7 +95,7 @@ protected List<Node> AliveNodes
8795
protected IDateTimeProvider DateTimeProvider { get; private set; }
8896

8997
protected List<Node> InternalNodes { get; set; }
90-
protected Random Random { get; } = new Random();
98+
protected Random Random { get; }
9199
protected bool Randomize { get; }
92100

93101
/// <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

@@ -216,20 +217,30 @@ [U] public void Static()
216217
}
217218
}
218219

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

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

0 commit comments

Comments
 (0)