Skip to content

Commit 8f048b9

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 cbb8321 commit 8f048b9

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;
@@ -215,22 +217,25 @@ [U] public void Static()
215217
// hide
216218
[U] public void RandomizedInitialNodes()
217219
{
218-
StaticConnectionPool CreatStaticConnectionPool()
220+
IEnumerable<StaticConnectionPool> CreatStaticConnectionPools()
219221
{
222+
Thread.Sleep(1);
223+
220224
var uris = new[]
221225
{
222226
new Uri("https://10.0.0.1:9200/"),
223227
new Uri("https://10.0.0.2:9200/"),
224228
new Uri("https://10.0.0.3:9200/")
225229
};
226230

227-
var staticConnectionPool = new StaticConnectionPool(uris);
228-
return staticConnectionPool;
231+
yield return new StaticConnectionPool(uris);
229232
}
230233

231234
// assertion works on the probability of seeing a Uri other than https://10.0.0.1:9200/
232235
// as the first value over 50 runs, when randomized.
233-
Enumerable.Repeat(CreatStaticConnectionPool().CreateView().First().Uri.ToString(), 50)
236+
CreatStaticConnectionPools()
237+
.Take(50)
238+
.Select(p => p.CreateView().First().Uri.ToString())
234239
.All(uri => uri == "https://10.0.0.1:9200/")
235240
.Should()
236241
.BeFalse();

0 commit comments

Comments
 (0)