Skip to content

Commit a5b262d

Browse files
committed
added test to try reproduce reports of overly active sniffing by @modec on twitter
1 parent e38c9a2 commit a5b262d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Tests/Elasticsearch.Net.Tests.Unit/ConnectionPools/Sniffing/SniffingConnectionPoolTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,53 @@ public void SniffOnStartupCallsSniffOnlyOnce()
4141
}
4242
}
4343

44+
[Test]
45+
public void SniffCalledOnceAndEachEnpointPingedOnce()
46+
{
47+
using (var fake = new AutoFake(callsDoNothing: true))
48+
{
49+
//It's recommended to only have on instance of your connection pool
50+
//Be sure to register it as Singleton in your IOC
51+
var uris = new[] { new Uri("http://localhost:9200"), new Uri("http://localhost:9201") };
52+
var connectionPool = new SniffingConnectionPool(uris);
53+
var config = new ConnectionConfiguration(connectionPool)
54+
.SniffOnStartup();
55+
fake.Provide<IConnectionConfigurationValues>(config);
56+
57+
var pingCall = FakeCalls.PingAtConnectionLevel(fake);
58+
pingCall.Returns(FakeResponse.Ok(config));
59+
var sniffCall = FakeCalls.Sniff(fake, config, uris);
60+
var transport = FakeCalls.ProvideDefaultTransport(fake);
61+
var getCall = FakeCalls.GetSyncCall(fake);
62+
getCall.Returns(FakeResponse.Ok(config));
63+
64+
var client1 = new ElasticsearchClient(config, transport: transport);
65+
client1.Info();
66+
client1.Info();
67+
client1.Info();
68+
client1.Info();
69+
var client2 = new ElasticsearchClient(config, transport: transport);
70+
client2.Info();
71+
client2.Info();
72+
client2.Info();
73+
client2.Info();
74+
var client3 = new ElasticsearchClient(config, transport: transport);
75+
client3.Info();
76+
client3.Info();
77+
client3.Info();
78+
client3.Info();
79+
var client4 = new ElasticsearchClient(config, transport: transport);
80+
client4.Info();
81+
client4.Info();
82+
client4.Info();
83+
client4.Info();
84+
85+
sniffCall.MustHaveHappened(Repeated.Exactly.Once);
86+
//sniff validates first node, one new node should be pinged before usage.
87+
pingCall.MustHaveHappened(Repeated.Exactly.Once);
88+
}
89+
}
90+
4491
[Test]
4592
public void SniffIsCalledAfterItHasGoneOutOfDate()
4693
{
@@ -481,6 +528,7 @@ public void ShouldRetryOnSniff500()
481528
sniffException.Should().NotBeNull();
482529
}
483530
}
531+
484532

485533
}
486534
}

0 commit comments

Comments
 (0)