-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterRetryTests.cs
More file actions
263 lines (224 loc) · 10.8 KB
/
ClusterRetryTests.cs
File metadata and controls
263 lines (224 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.Net;
using System.Text;
using System.Text.Json;
using Couchbase.AnalyticsClient.DI;
using Couchbase.AnalyticsClient.Exceptions;
using Couchbase.AnalyticsClient.HTTP;
using Couchbase.AnalyticsClient.Internal.HTTP;
using Couchbase.AnalyticsClient.Options;
using Couchbase.AnalyticsClient.Query;
using Couchbase.Core.Json;
using Microsoft.Extensions.Logging;
using Moq;
using Moq.Protected;
using Xunit;
using Xunit.Abstractions;
namespace Couchbase.AnalyticsClient.UnitTests.Internal;
public class ClusterRetryTests
{
private readonly ITestOutputHelper _outputHelper;
private readonly Mock<ICouchbaseHttpClientFactory> _httpClientFactoryMock;
private readonly Mock<ILogger<Cluster>> _clusterLoggerMock;
private readonly Mock<IDeserializer> _deserializerMock;
private readonly Mock<HttpMessageHandler> _httpMessageHandlerMock;
private readonly string _connectionString;
private readonly Credential _credential;
public ClusterRetryTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
_httpClientFactoryMock = new Mock<ICouchbaseHttpClientFactory>();
_clusterLoggerMock = new Mock<ILogger<Cluster>>();
_deserializerMock = new Mock<IDeserializer>();
_httpMessageHandlerMock = new Mock<HttpMessageHandler>();
_connectionString = "http://127.0.0.1";
_credential = new Credential("Administrator", "password");
_httpClientFactoryMock.Setup(f => f.Create()).Returns(() => new HttpClient(_httpMessageHandlerMock.Object));
}
/// <summary>
/// Tests that the cluster retries when it encounters *only* retriable errors, on a 200 OK response.
/// Ultimately, the cluster should throw a QueryException with the last error in the list since it
/// still doesn't succeed after all retries.
/// </summary>
[Fact]
public async Task ExecuteQueryAsync_WithRetriableErrors_RetriesAndThrowsCorrectException()
{
const int maxRetries = 3;
var cluster = CreateClusterWithRetryConfiguration(maxRetries);
var retriableErrors = new List<QueryError>
{
new QueryError(24045, "Some unknown retriable error occurred", true),
new QueryError(24055, "Another retriable error!", true)
};
// Configure HTTP handler to return 200 OK with retriable errors in the body
var callCount = 0;
_httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Returns(() =>
{
callCount++;
_outputHelper.WriteLine($"HTTP request attempt: {callCount}");
var responseJson = BuildErrorResponseJson(retriableErrors);
var responseContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = responseContent });
});
var exception = await Assert.ThrowsAsync<QueryException>(
() => cluster.ExecuteQueryAsync("SELECT * FROM test", new QueryOptions { AsStreaming = false }));
// Verify correct number of attempts (maxRetries + 1 attempts total, since we don't count the initial call as a "retry")
Assert.Equal(maxRetries + 1, callCount);
Assert.Equal(24045, exception.Code);
Assert.Equal("Some unknown retriable error occurred", exception.ServerMessage);
// We're verifying the number of times an HTTP client was created is exactly 1,
// since we now reuse the same HttpClient instance across retries.
_httpClientFactoryMock.Verify(f => f.Create(), Times.Once);
}
/// <summary>
/// Tests that the cluster does not retry when it encounters at least 1 non-retriable error, on a 200 OK response.
/// Ultimately, the cluster should throw a QueryException with the first error in the list.
/// </summary>
[Fact]
public async Task ExecuteQueryAsync_WithMixedErrors_DoesNotRetryAndThrowsNonRetriableError()
{
const int maxRetries = 3;
var cluster = CreateClusterWithRetryConfiguration(maxRetries);
// one non-retriable, one retriable
var mixedErrors = new List<QueryError>
{
new QueryError(24001, "A non-retriable error occurred", false),
new QueryError(24045, "Some retriable error", true)
};
var callCount = 0;
_httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Returns(() =>
{
callCount++;
_outputHelper.WriteLine($"HTTP request attempt: {callCount}");
var responseJson = BuildErrorResponseJson(mixedErrors);
var responseContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = responseContent });
});
var exception = await Assert.ThrowsAsync<QueryException>(
() => cluster.ExecuteQueryAsync("SELECT * FROM test", new QueryOptions { AsStreaming = false }));
// Should not retry with mixed errors, only 1 attempt
Assert.Equal(1, callCount);
// Should throw for the first non-retriable error
Assert.Equal(24001, exception.Code);
Assert.Equal("A non-retriable error occurred", exception.ServerMessage);
_httpClientFactoryMock.Verify(f => f.Create(), Times.Once);
}
/// <summary>
/// Tests that the cluster retries when it encounters 1 retriable error, on a 200 OK response.
/// Ultimately, the cluster should return a result on the second attempt.
/// </summary>
[Fact]
public async Task ExecuteQueryAsync_WithSuccessfulRetry_ReturnsResultOnSecondAttempt()
{
const int maxRetries = 3;
var cluster = CreateClusterWithRetryConfiguration(maxRetries);
var callCount = 0;
_httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Returns(() =>
{
callCount++;
_outputHelper.WriteLine($"HTTP request attempt: {callCount}");
if (callCount == 1)
{
var retriableErrors = new List<QueryError>
{
new QueryError(24045, "Some unknown retriable error occurred", true)
};
var responseJson = BuildErrorResponseJson(retriableErrors);
var responseContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = responseContent });
}
else
{
var responseJson = BuildSuccessResponseJson();
var responseContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = responseContent });
}
});
var result = await cluster.ExecuteQueryAsync("SELECT * FROM test", new QueryOptions { AsStreaming = false });
Assert.NotNull(result);
Assert.Equal(2, callCount);
// We're verifying the number of times an HTTP client was created is exactly 1,
// since we now reuse the same HttpClient instance across retries.
_httpClientFactoryMock.Verify(f => f.Create(), Times.Once);
}
/// <summary>
/// Tests that the cluster does not retry when it encounters a retriable error, on a 200 OK response.
/// Ultimately, the cluster should throw a QueryException with the first error in the list.
/// </summary>
[Fact]
public async Task ExecuteQueryAsync_WithZeroRetries_DoesNotRetryOnRetriableError()
{
const int maxRetries = 0;
var cluster = CreateClusterWithRetryConfiguration(maxRetries);
var retriableErrors = new List<QueryError>
{
new QueryError(24045, "Some unknown retriable error occurred", true)
};
var callCount = 0;
_httpMessageHandlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.Returns(() =>
{
callCount++;
_outputHelper.WriteLine($"HTTP request attempt: {callCount}");
var responseJson = BuildErrorResponseJson(retriableErrors);
var responseContent = new StringContent(responseJson, Encoding.UTF8, "application/json");
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = responseContent });
});
var exception = await Assert.ThrowsAsync<QueryException>(
() => cluster.ExecuteQueryAsync("SELECT * FROM test", new QueryOptions { AsStreaming = false }));
Assert.Equal(1, callCount);
Assert.Equal(24045, exception.Code);
_httpClientFactoryMock.Verify(f => f.Create(), Times.Once);
}
private Cluster CreateClusterWithRetryConfiguration(int maxRetries)
{
var clusterOptions = new ClusterOptions { ConnectionString = _connectionString }
.WithMaxRetries((uint)maxRetries)
.WithTimeoutOptions(new TimeoutOptions().WithQueryTimeout(TimeSpan.FromSeconds(30)))
.AddService<ICouchbaseHttpClientFactory, ICouchbaseHttpClientFactory>(
_ => _httpClientFactoryMock.Object,
ClusterServiceLifetime.Cluster)
.AddService<ILogger<Cluster>, ILogger<Cluster>>(
_ => _clusterLoggerMock.Object,
ClusterServiceLifetime.Cluster)
.AddService<IDeserializer, IDeserializer>(
_ => new StjJsonDeserializer(),
ClusterServiceLifetime.Cluster);
return Cluster.Create(_credential, clusterOptions);
}
private static string BuildErrorResponseJson(IEnumerable<QueryError> errors)
{
var payload = new
{
status = "fatal",
errors = errors.Select(e => new { code = e.Code, msg = e.Message, retriable = e.Retriable })
};
return JsonSerializer.Serialize(payload);
}
private static string BuildSuccessResponseJson()
{
return "{\"status\":\"success\",\"results\":[]}";
}
}