Skip to content

Commit 8e62e2d

Browse files
committed
Better randomized initial nodes test
This commit adds a better randomized initial nodes test by introducing a Thread.Sleep of 1 millisecond between successive instantiations of StaticConnectionPool. A new instance of Random is created for each instance of StaticConnectionPool, and the Random is initialized with Environment.TickCount, the number of milliseconds since the system started. To therefore ensure that seeds for successive instances of Random are different, a wait is introduced.
1 parent 02dfd4b commit 8e62e2d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text;
5+
using System.Threading;
46
using Elastic.Xunit.XunitPlumbing;
57
using Elasticsearch.Net;
68
using FluentAssertions;
@@ -217,22 +219,25 @@ [U] public void Static()
217219
// hide
218220
[U] public void RandomizedInitialNodes()
219221
{
220-
StaticConnectionPool CreatStaticConnectionPool()
222+
IEnumerable<StaticConnectionPool> CreatStaticConnectionPools()
221223
{
224+
Thread.Sleep(1);
225+
222226
var uris = new[]
223227
{
224228
new Uri("https://10.0.0.1:9200/"),
225229
new Uri("https://10.0.0.2:9200/"),
226230
new Uri("https://10.0.0.3:9200/")
227231
};
228232

229-
var staticConnectionPool = new StaticConnectionPool(uris);
230-
return staticConnectionPool;
233+
yield return new StaticConnectionPool(uris);
231234
}
232235

233236
// assertion works on the probability of seeing a Uri other than https://10.0.0.1:9200/
234237
// as the first value over 50 runs, when randomized.
235-
Enumerable.Repeat(CreatStaticConnectionPool().CreateView().First().Uri.ToString(), 50)
238+
CreatStaticConnectionPools()
239+
.Take(50)
240+
.Select(p => p.CreateView().First().Uri.ToString())
236241
.All(uri => uri == "https://10.0.0.1:9200/")
237242
.Should()
238243
.BeFalse();

0 commit comments

Comments
 (0)