-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathClientTimeoutTests.cs
401 lines (368 loc) · 17.1 KB
/
ClientTimeoutTests.cs
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// Copyright (C) DataStax Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cassandra.IntegrationTests.SimulacronAPI;
using Cassandra.IntegrationTests.TestBase;
using Cassandra.IntegrationTests.TestClusterManagement.Simulacron;
using Cassandra.Tests;
using NUnit.Framework;
namespace Cassandra.IntegrationTests.Core
{
[TestFixture, Category(TestCategory.Short)]
public class ClientTimeoutTests : TestGlobals
{
private SimulacronCluster _testCluster;
public ClientTimeoutTests()
{
Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Info;
Diagnostics.CassandraStackTraceIncluded = true;
}
[TearDown]
public void TearDown()
{
_testCluster?.Dispose();
_testCluster = null;
}
[Test]
public void Should_Move_To_Next_Host_For_Simple_Queries()
{
_testCluster = SimulacronCluster.CreateNew(2);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(500);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
var nodes = _testCluster.GetNodes().ToList();
var node = nodes[0];
node.PrimeFluent(b => b
.WhenQuery("SELECT key FROM system.local")
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(2000));
TestHelper.Invoke(() =>
{
var rs = session.Execute("SELECT key FROM system.local");
Assert.AreEqual(nodes[1].ContactPoint, rs.Info.QueriedHost.ToString());
}, 10);
}
}
[Test]
public void Should_Move_To_Next_Host_For_Bound_Statements()
{
_testCluster = SimulacronCluster.CreateNew(2);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(500);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
var ps = session.Prepare("SELECT key FROM system.local");
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
var nodes = _testCluster.GetNodes().ToList();
var node = nodes[0];
node.PrimeFluent(
b => b.WhenQuery("SELECT key FROM system.local")
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(2000));
TestHelper.Invoke(() =>
{
var rs = session.Execute(ps.Bind());
Assert.AreEqual(nodes[1].ContactPoint, rs.Info.QueriedHost.ToString());
}, 10);
}
}
[Test]
public void Should_Move_To_Next_Host_For_Prepare_Requests()
{
_testCluster = SimulacronCluster.CreateNew(2);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(3000);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
var node = _testCluster.GetNodes().Skip(1).First();
node.Stop().GetAwaiter().GetResult();
TestHelper.Invoke(() =>
{
session.Prepare("SELECT key FROM system.local");
}, 10);
node.Start().GetAwaiter().GetResult();
}
}
[Test]
public void Should_Throw_OperationTimedOutException_When_Retry_Is_False()
{
_testCluster = SimulacronCluster.CreateNew(2);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(500);
var queryOptions = new QueryOptions().SetRetryOnTimeout(false);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions)
.WithQueryOptions(queryOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
var nodes = _testCluster.GetNodes().ToList();
var node = nodes[1];
node.PrimeFluent(
b => b.WhenQuery("SELECT key FROM system.local")
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(2000));
var coordinators = new HashSet<string>();
var exceptions = new List<OperationTimedOutException>();
TestHelper.Invoke(() =>
{
try
{
var rs = session.Execute("SELECT key FROM system.local");
coordinators.Add(rs.Info.QueriedHost.ToString());
}
catch (OperationTimedOutException ex)
{
exceptions.Add(ex);
}
}, 10);
Assert.AreEqual(1, coordinators.Count);
Assert.AreEqual(5, exceptions.Count);
Assert.AreEqual(nodes[0].ContactPoint, coordinators.First());
}
}
[Test]
public void Should_Wait_When_ReadTimeout_Is_Zero()
{
var socketOptions = new SocketOptions().SetReadTimeoutMillis(0);
using (var simulacronCluster = SimulacronCluster.CreateNew(3))
{
const string cql = "SELECT key FROM system.local";
simulacronCluster.PrimeFluent(
b => b.WhenQuery(cql)
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(30000));
using (var cluster = ClusterBuilder().AddContactPoint(simulacronCluster.InitialContactPoint).WithSocketOptions(socketOptions).Build())
{
var session = cluster.Connect();
var query = new SimpleStatement(cql);
var task = session.ExecuteAsync(query);
Thread.Sleep(15000);
Assert.AreEqual(TaskStatus.WaitingForActivation, task.Status);
Thread.Sleep(15000);
TestHelper.RetryAssert(
() => { Assert.AreEqual(TaskStatus.RanToCompletion, task.Status, task.Exception?.ToString() ?? "no exception"); },
100,
50);
}
}
}
/// Tests the priority of statement specific timeout over general timeout
///
/// @jira_ticket CSHARP-415
/// @expected_result A OperationTimedOutException if timeout expires.
///
/// @test_category connection:timeout
[Test]
public void Should_Use_Statement_ReadTimeout()
{
const int generalReadTimeout = 1000;
const int statementReadTimeout = 8000;
_testCluster = SimulacronCluster.CreateNew(1);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(generalReadTimeout);
var queryOptions = new QueryOptions().SetRetryOnTimeout(false);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions)
.WithPoolingOptions(PoolingOptions.Create().SetHeartBeatInterval(0))
.WithQueryTimeout(Timeout.Infinite)
.WithQueryOptions(queryOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
var nodes = _testCluster.GetNodes().ToList();
var node = nodes[0];
node.PrimeFluent(
b => b.WhenQuery("SELECT key FROM system.local")
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(30000));
var stopWatch = new Stopwatch();
stopWatch.Start();
Assert.Throws<OperationTimedOutException>(() => session.Execute("SELECT key FROM system.local"));
stopWatch.Stop();
//precision of the timer is not guaranteed
Assert.Greater(stopWatch.ElapsedMilliseconds, generalReadTimeout - 2000);
Assert.Less(stopWatch.ElapsedMilliseconds, generalReadTimeout + 2000);
//Try with an specified timeout at Statement level
var stmt = new SimpleStatement("SELECT key FROM system.local")
.SetReadTimeoutMillis(statementReadTimeout);
stopWatch.Restart();
Assert.Throws<OperationTimedOutException>(() => session.Execute(stmt));
stopWatch.Stop();
//precision of the timer is not guaranteed
Assert.Greater(stopWatch.ElapsedMilliseconds, statementReadTimeout - 4000);
Assert.Less(stopWatch.ElapsedMilliseconds, statementReadTimeout + 4000);
}
}
/// Tests a NoHostAvailableException is raised when all hosts down with read timeout
///
/// Should_Throw_NoHostAvailableException_When_All_Hosts_Down tests that the driver quickly throws a NoHostAvailableException
/// when all nodes in the cluster is down, given a set ReadTimeoutMillis of 3 seconds.
///
/// @since 2.7.0
/// @jira_ticket CSHARP-332
/// @expected_result A NoHostAvailableException should be raised after 3 seconds.
///
/// @test_category connection:timeout
[Test]
public void Should_Throw_NoHostAvailableException_When_All_Hosts_Down()
{
_testCluster = SimulacronCluster.CreateNew(2);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(500);
var builder = ClusterBuilder().AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions);
using (var cluster = builder.Build())
{
var session = cluster.Connect();
//warmup
TestHelper.Invoke(() => session.Execute("SELECT key FROM system.local"), 10);
_testCluster.PrimeFluent(
b => b.WhenQuery("SELECT key FROM system.local")
.ThenRowsSuccess(new [] { ("key", DataType.Ascii) }, rows => rows.WithRow("123"))
.WithDelayInMs(10000));
var ex = Assert.Throws<NoHostAvailableException>(() => session.Execute("SELECT key FROM system.local"));
Assert.AreEqual(2, ex.Errors.Count);
foreach (var innerException in ex.Errors.Values)
{
Assert.IsInstanceOf<OperationTimedOutException>(innerException);
}
}
}
[Test]
public void Should_Throw_NoHostAvailable_When_Startup_Times_out()
{
_testCluster = SimulacronCluster.CreateNew(1);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(1000).SetConnectTimeoutMillis(1000);
var builder = ClusterBuilder()
.AddContactPoint(_testCluster.InitialContactPoint)
.WithSocketOptions(socketOptions);
using (var cluster = builder.Build())
{
_testCluster.GetNodes().First().DisableConnectionListener(0, "reject_startup").GetAwaiter().GetResult();
var ex = Assert.Throws<NoHostAvailableException>(() => cluster.Connect());
Assert.AreEqual(1, ex.Errors.Count);
foreach (var innerException in ex.Errors.Values)
{
Assert.IsInstanceOf<OperationTimedOutException>(innerException);
}
}
}
[Test]
public void Should_Not_Leak_Connections_Test()
{
_testCluster = SimulacronCluster.CreateNew(1);
var socketOptions = new SocketOptions().SetReadTimeoutMillis(1).SetConnectTimeoutMillis(1);
var node = _testCluster.GetNodes().First();
node.DisableConnectionListener(0, "reject_startup").GetAwaiter().GetResult();
var clusters = Enumerable.Range(0, 100).Select(
b => ClusterBuilder()
.AddContactPoint(_testCluster.InitialContactPoint)
.WithReconnectionPolicy(new ConstantReconnectionPolicy(120 * 1000))
.WithSocketOptions(socketOptions)
.Build()).ToList();
try
{
var tasks = clusters.Select(c => Task.Run(async () =>
{
try
{
var session = await c.ConnectAsync().ConfigureAwait(false);
try
{
await session.ConnectAsync().ConfigureAwait(false);
}
finally
{
await session.ShutdownAsync().ConfigureAwait(false);
}
}
catch (NoHostAvailableException ex)
{
Assert.AreEqual(1, ex.Errors.Count);
return;
}
Assert.Fail();
})).ToArray();
Task.WaitAll(tasks);
foreach (var t in tasks)
{
t.Dispose();
}
tasks = null;
Thread.Sleep(3000);
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
decimal initialMemory = GC.GetTotalMemory(true);
const int length = 200;
tasks = clusters.Select(c => Task.Run(async () =>
{
for (var i = 0; i < length; i++)
{
try
{
var session = await c.ConnectAsync().ConfigureAwait(false);
try
{
await session.ConnectAsync().ConfigureAwait(false);
}
finally
{
await session.ShutdownAsync().ConfigureAwait(false);
}
}
catch (NoHostAvailableException ex)
{
Assert.AreEqual(1, ex.Errors.Count);
continue;
}
Assert.Fail();
}
})).ToArray();
Task.WaitAll(tasks);
foreach (var t in tasks)
{
t.Dispose();
}
tasks = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
Assert.Less(GC.GetTotalMemory(true) / initialMemory, 1.5M,
"Should not exceed a 50% (1.5) more than was previously allocated");
}
finally
{
Task.WhenAll(clusters.Select(c => Task.Run(() => c.ShutdownAsync()))).GetAwaiter().GetResult();
}
}
}
}