Skip to content

Commit d8ced8d

Browse files
committed
Rename 'respectCancellation' value to 'flags'.
Make 'ignoreCancellation' an opt-in flag.
1 parent e00a571 commit d8ced8d

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

tests/MySqlConnector.Tests/CancellationTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Execute(int step, int method)
3737
connection.Open();
3838
using var command = connection.CreateCommand();
3939
command.CommandTimeout = 1;
40-
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
40+
command.CommandText = $"SELECT 0, 4000, {step}, 0;";
4141
var stopwatch = Stopwatch.StartNew();
4242
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
4343
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -59,7 +59,7 @@ public async Task ExecuteAsyncs(int step, int method)
5959
connection.Open();
6060
using var command = connection.CreateCommand();
6161
command.CommandTimeout = 1;
62-
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
62+
command.CommandText = $"SELECT 0, 4000, {step}, 0;";
6363
var stopwatch = Stopwatch.StartNew();
6464
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
6565
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -84,7 +84,7 @@ public void Execute(int step, int method)
8484
connection.Open();
8585
using var command = connection.CreateCommand();
8686
command.CommandTimeout = 10;
87-
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
87+
command.CommandText = $"SELECT 0, 4000, {step}, 0;";
8888
var task = Task.Run(async () =>
8989
{
9090
await Task.Delay(TimeSpan.FromSeconds(1));
@@ -111,7 +111,7 @@ public async Task ExecuteAsync(int step, int method)
111111
connection.Open();
112112
using var command = connection.CreateCommand();
113113
command.CommandTimeout = 10;
114-
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
114+
command.CommandText = $"SELECT 0, 4000, {step}, 0;";
115115
var task = Task.Run(async () =>
116116
{
117117
await Task.Delay(TimeSpan.FromSeconds(1));
@@ -141,7 +141,7 @@ public async Task Test(int step, int method)
141141
connection.Open();
142142
using var command = connection.CreateCommand();
143143
command.CommandTimeout = 0;
144-
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
144+
command.CommandText = $"SELECT 0, 4000, {step}, 0;";
145145
using var source = new CancellationTokenSource(TimeSpan.FromSeconds(1));
146146
var stopwatch = Stopwatch.StartNew();
147147
var ex = await Assert.ThrowsAsync<OperationCanceledException>(async () => await s_executeAsyncMethods[method](command, source.Token));
@@ -166,7 +166,7 @@ public void Test(int step, int method)
166166
connection.Open();
167167
using var command = connection.CreateCommand();
168168
command.CommandTimeout = 1;
169-
command.CommandText = $"SELECT 42, 100, {step}, 1;";
169+
command.CommandText = $"SELECT 42, 100, {step}, 0;";
170170
var stopwatch = Stopwatch.StartNew();
171171
var result = s_executeMethods[method](command);
172172
if (method == 1)
@@ -187,7 +187,7 @@ public async Task Test(int step, int method)
187187
connection.Open();
188188
using var command = connection.CreateCommand();
189189
command.CommandTimeout = 0;
190-
command.CommandText = $"SELECT 42, 100, {step}, 1;";
190+
command.CommandText = $"SELECT 42, 100, {step}, 0;";
191191
using var source = new CancellationTokenSource(TimeSpan.FromSeconds(1));
192192
var stopwatch = Stopwatch.StartNew();
193193
var result = await s_executeAsyncMethods[method](command, source.Token);
@@ -210,7 +210,7 @@ public void Timeout(int method)
210210
connection.Open();
211211
using var command = connection.CreateCommand();
212212
command.CommandTimeout = 1;
213-
command.CommandText = $"SELECT 0, 100, -1, 1;";
213+
command.CommandText = $"SELECT 0, 100, -1, 0;";
214214
var stopwatch = Stopwatch.StartNew();
215215
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
216216
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -227,7 +227,7 @@ public void NoTimeout(int method)
227227
connection.Open();
228228
using var command = connection.CreateCommand();
229229
command.CommandTimeout = 1;
230-
command.CommandText = $"SELECT 42, 100, -1, 1;";
230+
command.CommandText = $"SELECT 42, 100, -1, 0;";
231231
var stopwatch = Stopwatch.StartNew();
232232
var result = s_executeMethods[method](command);
233233
Assert.Equal(42, result);
@@ -246,7 +246,7 @@ public async Task Timeout(int method)
246246
connection.Open();
247247
using var command = connection.CreateCommand();
248248
command.CommandTimeout = 1;
249-
command.CommandText = $"SELECT 0, 100, -1, 1;";
249+
command.CommandText = $"SELECT 0, 100, -1, 0;";
250250
var stopwatch = Stopwatch.StartNew();
251251
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
252252
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -263,7 +263,7 @@ public async Task NoTimeout(int method)
263263
connection.Open();
264264
using var command = connection.CreateCommand();
265265
command.CommandTimeout = 1;
266-
command.CommandText = $"SELECT 42, 100, -1, 1;";
266+
command.CommandText = $"SELECT 42, 100, -1, 0;";
267267
var stopwatch = Stopwatch.StartNew();
268268
var result = await s_executeAsyncMethods[method](command, default);
269269
Assert.Equal(42, result);
@@ -281,7 +281,7 @@ public void Test(int step, int method)
281281
connection.Open();
282282
using var command = connection.CreateCommand();
283283
command.CommandTimeout = 1;
284-
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
284+
command.CommandText = $"SELECT 0, 10000, {step}, 1;";
285285
var stopwatch = Stopwatch.StartNew();
286286
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
287287
Assert.InRange(stopwatch.ElapsedMilliseconds, 2900, 3500);
@@ -303,7 +303,7 @@ public async Task Test(int step, int method)
303303
connection.Open();
304304
using var command = connection.CreateCommand();
305305
command.CommandTimeout = 1;
306-
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
306+
command.CommandText = $"SELECT 0, 10000, {step}, 1;";
307307
var stopwatch = Stopwatch.StartNew();
308308
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
309309
Assert.InRange(stopwatch.ElapsedMilliseconds, 2900, 3500);
@@ -326,7 +326,7 @@ public void Execute(int step, int method)
326326
connection.Open();
327327
using var command = connection.CreateCommand();
328328
command.CommandTimeout = 1;
329-
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
329+
command.CommandText = $"SELECT 0, 10000, {step}, 1;";
330330
var stopwatch = Stopwatch.StartNew();
331331
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
332332
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -346,7 +346,7 @@ public async Task ExecuteAsync(int step, int method)
346346
connection.Open();
347347
using var command = connection.CreateCommand();
348348
command.CommandTimeout = 1;
349-
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
349+
command.CommandText = $"SELECT 0, 10000, {step}, 1;";
350350
var stopwatch = Stopwatch.StartNew();
351351
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
352352
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);

tests/MySqlConnector.Tests/FakeMySqlServerConnection.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ public async Task RunAsync(TcpClient client, CancellationToken token)
9090
}
9191
else if ((match = Regex.Match(query, @"^SELECT ([0-9]+), ([0-9]+), ([0-9-]+), ([0-9]+)(;|$)")).Success)
9292
{
93-
// command is "SELECT {value}, {delay}, {pauseStep}, {respectCancellation}"
93+
// command is "SELECT {value}, {delay}, {pauseStep}, {flags}"
9494
var number = match.Groups[1].Value;
9595
var value = int.Parse(number);
9696
var delay = int.Parse(match.Groups[2].Value);
9797
var pauseStep = int.Parse(match.Groups[3].Value);
98-
var respectCancellation = int.Parse(match.Groups[4].Value) != 0;
98+
var flags = int.Parse(match.Groups[4].Value);
99+
var ignoreCancellation = (flags & 1) == 1;
99100

100101
var data = new byte[number.Length + 1];
101102
data[0] = (byte) number.Length;
@@ -124,10 +125,10 @@ public async Task RunAsync(TcpClient client, CancellationToken token)
124125
{
125126
if (pauseStep == step || pauseStep == -1)
126127
{
127-
if (respectCancellation)
128-
queryInterrupted = CancelQueryEvent.Wait(delay, token);
129-
else
128+
if (ignoreCancellation)
130129
await Task.Delay(delay, token);
130+
else
131+
queryInterrupted = CancelQueryEvent.Wait(delay, token);
131132
}
132133

133134
await SendAsync(stream, step, x => x.Write(packets[queryInterrupted ? 0 : step]));

0 commit comments

Comments
 (0)