Skip to content

Commit 06cf6b5

Browse files
committed
Further test cleanup and fixup
1 parent 3e5355b commit 06cf6b5

File tree

11 files changed

+63
-29
lines changed

11 files changed

+63
-29
lines changed

tests/Tests.Core/Client/FixedResponseClient.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System;
6+
using System.Net;
67
using System.Text;
78
using Elasticsearch.Net;
89
using Nest;
@@ -16,10 +17,11 @@ public static IElasticClient Create(
1617
int statusCode = 200,
1718
Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
1819
string contentType = null,
19-
Exception exception = null
20+
Exception exception = null,
21+
bool productCheckSucceeds = true
2022
)
2123
{
22-
var settings = CreateConnectionSettings(response, statusCode, modifySettings, contentType, exception);
24+
var settings = CreateConnectionSettings(response, statusCode, modifySettings, contentType, exception, productCheckSucceeds);
2325
return new ElasticClient(settings);
2426
}
2527

@@ -28,7 +30,8 @@ public static ConnectionSettings CreateConnectionSettings(
2830
int statusCode = 200,
2931
Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
3032
string contentType = null,
31-
Exception exception = null
33+
Exception exception = null,
34+
bool productCheckSucceeds = true
3235
)
3336
{
3437
contentType ??= RequestData.DefaultJsonMimeType;
@@ -51,7 +54,12 @@ public static ConnectionSettings CreateConnectionSettings(
5154
}
5255
}
5356

54-
var connection = new InMemoryConnection(responseBytes, statusCode, exception, contentType);
57+
var productCheckResponse = productCheckSucceeds ? InMemoryConnection.ValidProductCheckResponse() : new InMemoryHttpResponse
58+
{
59+
StatusCode = 500
60+
};
61+
62+
var connection = new InMemoryConnection(responseBytes, statusCode, exception, contentType, productCheckResponse);
5563
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
5664
var defaultSettings = new ConnectionSettings(connectionPool, connection)
5765
.DefaultIndex("default-index");

tests/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnexpectedExceptions.doc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ [U] public async Task WillFailOverKnowConnectionExceptionButNotUnexpected()
104104
*
105105
* Here, pinging nodes on first use is enabled and the node on port 9200 throws an exception on ping; when this happens,
106106
* we still fallover to retry the ping on node on port 9201, where it succeeds.
107-
* Following this, the client call on 9201 throws a hard exception that we are not able to recover from
107+
* Following this, the client call on 9201 throws a hard exception that we are not able to recover from.
108108
*/
109109
[U] public async Task PingUnexceptedExceptionDoesFailOver()
110110
{

tests/Tests/ClientConcepts/ConnectionPooling/Failover/FallingOver.doc.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class FallingOver
1515
/**[[fail-over]]
1616
* === Fail over
1717
* When using a connection pool with more than one node, a request will be retried if
18-
* the call to a node throws an exception or returns a 502, 503 or 504 response
18+
* the call to a node throws an exception or returns a 502, 503 or 504 response.
1919
*/
2020
[U]
2121
public async Task ExceptionFallsOverToNextNode()
@@ -27,7 +27,7 @@ public async Task ExceptionFallsOverToNextNode()
2727
.StaticConnectionPool()
2828
.Settings(s => s.DisablePing())
2929
);
30-
30+
3131
audit = await audit.TraceCall(
3232
new ClientCall {
3333
{ AuditEvent.BadResponse, 9200 },
@@ -39,7 +39,7 @@ public async Task ExceptionFallsOverToNextNode()
3939
/**[[bad-gateway]]
4040
*==== 502 Bad Gateway
4141
*
42-
* Will be treated as an error that requires retrying
42+
* Will be treated as an error that requires retrying.
4343
*/
4444
[U]
4545
public async Task Http502FallsOver()
@@ -63,7 +63,7 @@ public async Task Http502FallsOver()
6363
/**[[service-unavailable]]
6464
*==== 503 Service Unavailable
6565
*
66-
* Will be treated as an error that requires retrying
66+
* Will be treated as an error that requires retrying.
6767
*/
6868
[U]
6969
public async Task Http503FallsOver()
@@ -87,7 +87,7 @@ public async Task Http503FallsOver()
8787
/**[[gateway-timeout]]
8888
*==== 504 Gateway Timeout
8989
*
90-
* Will be treated as an error that requires retrying
90+
* Will be treated as an error that requires retrying.
9191
*/
9292
[U]
9393
public async Task Http504FallsOver()
@@ -112,7 +112,7 @@ public async Task Http504FallsOver()
112112
* If a call returns a __valid__ HTTP status code other than 502 or 503, the request won't be retried.
113113
*
114114
* IMPORTANT: Different requests may have different status codes that are deemed __valid__. For example,
115-
* a *404 Not Found* response is a __valid__ status code for an index exists request
115+
* a *404 Not Found* response is a __valid__ status code for an index exists request.
116116
*/
117117
[U]
118118
public async Task HttpTeapotDoesNotFallOver()

tests/Tests/ClientConcepts/ConnectionPooling/MaxRetries/RespectsMaxRetry.doc.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
using System;
66
using System.Threading.Tasks;
77
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8-
using Elasticsearch.Net;
98
using Elasticsearch.Net.VirtualizedCluster;
109
using Elasticsearch.Net.VirtualizedCluster.Audit;
11-
using Tests.Framework;
1210
using static Elasticsearch.Net.AuditEvent;
1311

1412
namespace Tests.ClientConcepts.ConnectionPooling.MaxRetries

tests/Tests/ClientConcepts/ConnectionPooling/Pinging/FirstUsage.doc.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9-
using Elasticsearch.Net;
109
using Elasticsearch.Net.VirtualizedCluster;
1110
using Elasticsearch.Net.VirtualizedCluster.Audit;
1211
using FluentAssertions;
13-
using Tests.Framework;
1412
using static Elasticsearch.Net.VirtualizedCluster.Rules.TimesHelper;
1513
using static Elasticsearch.Net.AuditEvent;
1614

tests/Tests/ClientConcepts/ConnectionPooling/RequestOverrides/DisableSniffPingPerRequest.doc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ [U] public async Task DisableSniff()
3636
.Settings(s => s.SniffOnStartup()) // <1> sniff on startup
3737
);
3838

39-
/** Now We disable sniffing on the request so even though it's our first call,
39+
/** Now we disable sniffing on the request so even though it's our first call,
4040
* we do not want to sniff on startup.
4141
*
4242
* Instead, the sniff on startup is deferred to the second call into the cluster that

tests/Tests/ClientConcepts/ConnectionPooling/RequestOverrides/RequestTimeoutsOverrides.doc.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class RequestTimeoutsOverrides
1717
/**[[request-timeout]]
1818
* === Request timeouts
1919
*
20-
* While you can specify Request time out globally you can override this per request too
20+
* While you can specify Request time out globally, you can override this per request too.
2121
*/
2222

2323
[U]
2424
public async Task RespectsRequestTimeoutOverride()
2525
{
26-
27-
/** we set up a 10 node cluster with a global time out of 20 seconds.
26+
/** We set up a 10 node cluster with a global time out of 20 seconds.
2827
* Each call on a node takes 10 seconds. So we can only try this call on 2 nodes
2928
* before the max request time out kills the client call.
3029
*/
@@ -43,7 +42,7 @@ public async Task RespectsRequestTimeoutOverride()
4342
{ MaxTimeoutReached }
4443
},
4544
/**
46-
* On the second request we specify a request timeout override to 80 seconds
45+
* On the second request we specify a request timeout override to 80 seconds.
4746
* We should now see more nodes being tried.
4847
*/
4948
new ClientCall(r => r.RequestTimeout(TimeSpan.FromSeconds(80)))

tests/Tests/ClientConcepts/ConnectionPooling/RequestOverrides/RespectsMaxRetryOverrides.doc.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
using System;
66
using System.Threading.Tasks;
77
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8-
using Elasticsearch.Net;
98
using Elasticsearch.Net.VirtualizedCluster;
109
using Elasticsearch.Net.VirtualizedCluster.Audit;
11-
using Tests.Framework;
1210
using static Elasticsearch.Net.AuditEvent;
1311

1412
namespace Tests.ClientConcepts.ConnectionPooling.RequestOverrides
@@ -90,7 +88,6 @@ public async Task DoesNotRetryOnSingleNodeConnectionPool()
9088
{ BadResponse, 9200 }
9189
}
9290
);
93-
9491
}
9592
}
9693
}

tests/Tests/ClientConcepts/Troubleshooting/LoggingWithOnRequestCompleted.doc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task OnRequestCompletedIsCalledWhenExceptionIsThrown()
5959
500,
6060
connectionSettings => connectionSettings
6161
.ThrowExceptions() // <2> Always throw exceptions when a call results in an exception
62-
.OnRequestCompleted(r => counter++)
62+
.OnRequestCompleted(r => counter++), productCheckSucceeds: false
6363
);
6464

6565
Assert.Throws<ElasticsearchClientException>(() => client.RootNodeInfo()); // <3> Assert an exception is thrown and the counter is incremented

tests/Tests/CodeStandards/Requests.doc.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void BaseUriWithTrailingSlashIsRespected()
4242
public void BaseUriWithRelativePathIsRespected()
4343
{
4444
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200/elasticsearch"));
45-
var settings = new ConnectionSettings(pool, new InMemoryConnection());
45+
var settings = new ConnectionSettings(pool, new InMemoryConnection("elasticsearch"));
4646
var client = new ElasticClient(settings);
4747
var searchResponse = client.Search<Project>(s => s.AllIndices());
4848

@@ -53,7 +53,7 @@ public void BaseUriWithRelativePathIsRespected()
5353
public void BaseUriWithRelativePathAndTrailingSlashIsRespected()
5454
{
5555
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200/elasticsearch/"));
56-
var settings = new ConnectionSettings(pool, new InMemoryConnection());
56+
var settings = new ConnectionSettings(pool, new InMemoryConnection("elasticsearch/"));
5757
var client = new ElasticClient(settings);
5858
var searchResponse = client.Search<Project>(s => s.AllIndices());
5959

tests/Tests/MetaHeader/MetaHeaderHelperTests.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,13 @@ protected class SmallObject
183183
{
184184
public string Name { get; set; }
185185
}
186-
186+
187187
protected class TestableInMemoryConnection : IConnection
188188
{
189189
internal static readonly byte[] EmptyBody = Encoding.UTF8.GetBytes("");
190190

191+
private readonly InMemoryHttpResponse _productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
192+
191193
private readonly Action<RequestData> _perRequestAssertion;
192194
private readonly List<(int, string)> _responses;
193195
private int _requestCounter = -1;
@@ -202,8 +204,25 @@ public TestableInMemoryConnection(Action<RequestData> assertion, List<(int, stri
202204

203205
async Task<TResponse> IConnection.RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken)
204206
{
205-
Interlocked.Increment(ref _requestCounter);
207+
if ("/".Equals(requestData.Uri.AbsolutePath, StringComparison.Ordinal) && requestData.Method == HttpMethod.GET)
208+
{
209+
// We don't add product checks to the request count
210+
211+
_productCheckResponse.Headers.TryGetValue("X-elastic-product", out var productNames);
212+
213+
requestData.MadeItToResponse = true;
214+
215+
await using var ms = requestData.MemoryStreamFactory.Create(_productCheckResponse.ResponseBytes);
216+
217+
await Task.Yield(); // avoids test deadlocks
218+
219+
return ResponseBuilder.ToResponse<TResponse>(
220+
requestData, null, _productCheckResponse.StatusCode, null, ms,
221+
RequestData.DefaultJsonMimeType, productNames?.FirstOrDefault());
222+
}
206223

224+
Interlocked.Increment(ref _requestCounter);
225+
207226
_perRequestAssertion(requestData);
208227

209228
await Task.Yield(); // avoids test deadlocks
@@ -219,12 +238,27 @@ async Task<TResponse> IConnection.RequestAsync<TResponse>(RequestData requestDat
219238
var stream = !string.IsNullOrEmpty(response) ? requestData.MemoryStreamFactory.Create(Encoding.UTF8.GetBytes(response)) : requestData.MemoryStreamFactory.Create(EmptyBody);
220239

221240
return await ResponseBuilder
222-
.ToResponseAsync<TResponse>(requestData, null, statusCode, null, stream, RequestData.DefaultJsonMimeType, cancellationToken)
241+
.ToResponseAsync<TResponse>(requestData, null, statusCode, null, stream, RequestData.DefaultJsonMimeType, "Elasticsearch", cancellationToken)
223242
.ConfigureAwait(false);
224243
}
225244

226245
TResponse IConnection.Request<TResponse>(RequestData requestData)
227246
{
247+
if ("/".Equals(requestData.Uri.AbsolutePath, StringComparison.Ordinal) && requestData.Method == HttpMethod.GET)
248+
{
249+
// We don't add product checks to the request count
250+
251+
_productCheckResponse.Headers.TryGetValue("X-elastic-product", out var productNames);
252+
253+
requestData.MadeItToResponse = true;
254+
255+
using var ms = requestData.MemoryStreamFactory.Create(_productCheckResponse.ResponseBytes);
256+
257+
return ResponseBuilder.ToResponse<TResponse>(
258+
requestData, null, _productCheckResponse.StatusCode, null, ms,
259+
RequestData.DefaultJsonMimeType, productNames?.FirstOrDefault());
260+
}
261+
228262
Interlocked.Increment(ref _requestCounter);
229263

230264
_perRequestAssertion(requestData);

0 commit comments

Comments
 (0)