Skip to content

Commit 1851b1b

Browse files
committed
added tests for hard exceptions in IConnection async methods
1 parent 12f90f7 commit 1851b1b

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/Elasticsearch.Net/Connection/Transport.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,29 @@ private Task<ElasticsearchResponse<Stream>> CallIntoConnectionAsync<T>(Transport
515515
var uri = requestState.CreatePathOnCurrentNode();
516516
var postData = requestState.PostData;
517517
var requestConfiguration = requestState.RequestConfiguration;
518-
switch (requestState.Method.ToLowerInvariant())
518+
var method = requestState.Method.ToLowerInvariant();
519+
try
519520
{
520-
case "head": return this.Connection.Head(uri, requestConfiguration);
521-
case "get": return this.Connection.Get(uri, requestConfiguration);
522-
case "post": return this.Connection.Post(uri, postData, requestConfiguration);
523-
case "put": return this.Connection.Put(uri, postData, requestConfiguration);
524-
case "delete":
525-
return postData == null || postData.Length == 0
526-
? this.Connection.Delete(uri, requestConfiguration)
527-
: this.Connection.Delete(uri, postData, requestConfiguration);
521+
switch (method)
522+
{
523+
case "head": return this.Connection.Head(uri, requestConfiguration);
524+
case "get": return this.Connection.Get(uri, requestConfiguration);
525+
case "post": return this.Connection.Post(uri, postData, requestConfiguration);
526+
case "put": return this.Connection.Put(uri, postData, requestConfiguration);
527+
case "delete":
528+
return postData == null || postData.Length == 0
529+
? this.Connection.Delete(uri, requestConfiguration)
530+
: this.Connection.Delete(uri, postData, requestConfiguration);
531+
default:
532+
throw new Exception("Unknown HTTP method " + requestState.Method);
533+
}
534+
}
535+
catch (Exception e)
536+
{
537+
var tcs = new TaskCompletionSource<ElasticsearchResponse<Stream>>();
538+
tcs.SetException(e);
539+
return tcs.Task;
528540
}
529-
throw new Exception("Unknown HTTP method " + requestState.Method);
530541
}
531542

532543
private Task<MemoryStream> Iterate(IEnumerable<Task> asyncIterator, MemoryStream memoryStream)

src/Tests/Elasticsearch.Net.Tests.Unit/Connection/RetryTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ public void ThrowsMaxRetryException_AndRetriesTheSpecifiedTimes()
4141

4242
}
4343
}
44+
45+
[Test]
46+
public void ThrowsMaxRetryException_AndRetriesTheSpecifiedTimes_HardIConnectionException_Async()
47+
{
48+
using (var fake = new AutoFake(callsDoNothing: true))
49+
{
50+
fake.Provide<IConnectionConfigurationValues>(_connectionConfig);
51+
FakeCalls.ProvideDefaultTransport(fake);
52+
var getCall = FakeCalls.GetCall(fake);
53+
54+
//return a started task that throws
55+
getCall.Throws<Exception>();
56+
57+
var client = fake.Resolve<ElasticsearchClient>();
58+
59+
client.Settings.MaxRetries.Should().Be(_retries);
60+
61+
Assert.Throws<MaxRetryException>(async () => await client.InfoAsync());
62+
getCall.MustHaveHappened(Repeated.Exactly.Times(_retries + 1));
63+
}
64+
}
4465

4566
[Test]
4667
public void ThrowsMaxRetryException_AndRetriesTheSpecifiedTimes_Async()

0 commit comments

Comments
 (0)