Skip to content

Commit e00a571

Browse files
committed
Split "control word" into separate parts for CancellationTests.
Instead of overloading multiple pieces of data into one integer, split them out into separate inputs.
1 parent a9d7fa5 commit e00a571

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

tests/MySqlConnector.Tests/CancellationTests.cs

Lines changed: 19 additions & 21 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 {4000 + step};";
40+
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
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 {4000 + step};";
62+
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
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 {4000 + step};";
87+
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
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 {4000 + step};";
114+
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
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 {4000 + step};";
144+
command.CommandText = $"SELECT 0, 4000, {step}, 1;";
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,14 +166,13 @@ public void Test(int step, int method)
166166
connection.Open();
167167
using var command = connection.CreateCommand();
168168
command.CommandTimeout = 1;
169-
var expected = 100 + step;
170-
command.CommandText = $"SELECT {expected};";
169+
command.CommandText = $"SELECT 42, 100, {step}, 1;";
171170
var stopwatch = Stopwatch.StartNew();
172171
var result = s_executeMethods[method](command);
173172
if (method == 1)
174173
Assert.Equal(0, result); // ExecuteNonQuery
175174
else
176-
Assert.Equal(expected, result);
175+
Assert.Equal(42, result);
177176
Assert.InRange(stopwatch.ElapsedMilliseconds, 50, 250);
178177
}
179178
}
@@ -188,15 +187,14 @@ public async Task Test(int step, int method)
188187
connection.Open();
189188
using var command = connection.CreateCommand();
190189
command.CommandTimeout = 0;
191-
var expected = 100 + step;
192-
command.CommandText = $"SELECT {expected};";
190+
command.CommandText = $"SELECT 42, 100, {step}, 1;";
193191
using var source = new CancellationTokenSource(TimeSpan.FromSeconds(1));
194192
var stopwatch = Stopwatch.StartNew();
195193
var result = await s_executeAsyncMethods[method](command, source.Token);
196194
if (method == 1)
197195
Assert.Equal(0, result); // ExecuteNonQuery
198196
else
199-
Assert.Equal(expected, result);
197+
Assert.Equal(42, result);
200198
Assert.InRange(stopwatch.ElapsedMilliseconds, 50, 250);
201199
}
202200
}
@@ -212,7 +210,7 @@ public void Timeout(int method)
212210
connection.Open();
213211
using var command = connection.CreateCommand();
214212
command.CommandTimeout = 1;
215-
command.CommandText = $"SELECT 100;";
213+
command.CommandText = $"SELECT 0, 100, -1, 1;";
216214
var stopwatch = Stopwatch.StartNew();
217215
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
218216
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -229,10 +227,10 @@ public void NoTimeout(int method)
229227
connection.Open();
230228
using var command = connection.CreateCommand();
231229
command.CommandTimeout = 1;
232-
command.CommandText = $"SELECT 100;";
230+
command.CommandText = $"SELECT 42, 100, -1, 1;";
233231
var stopwatch = Stopwatch.StartNew();
234232
var result = s_executeMethods[method](command);
235-
Assert.Equal(100, result);
233+
Assert.Equal(42, result);
236234
Assert.InRange(stopwatch.ElapsedMilliseconds, 1100, 1500);
237235
}
238236
}
@@ -248,7 +246,7 @@ public async Task Timeout(int method)
248246
connection.Open();
249247
using var command = connection.CreateCommand();
250248
command.CommandTimeout = 1;
251-
command.CommandText = $"SELECT 100;";
249+
command.CommandText = $"SELECT 0, 100, -1, 1;";
252250
var stopwatch = Stopwatch.StartNew();
253251
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
254252
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -265,10 +263,10 @@ public async Task NoTimeout(int method)
265263
connection.Open();
266264
using var command = connection.CreateCommand();
267265
command.CommandTimeout = 1;
268-
command.CommandText = $"SELECT 100;";
266+
command.CommandText = $"SELECT 42, 100, -1, 1;";
269267
var stopwatch = Stopwatch.StartNew();
270268
var result = await s_executeAsyncMethods[method](command, default);
271-
Assert.Equal(100, result);
269+
Assert.Equal(42, result);
272270
Assert.InRange(stopwatch.ElapsedMilliseconds, 1100, 1500);
273271
}
274272
}
@@ -283,7 +281,7 @@ public void Test(int step, int method)
283281
connection.Open();
284282
using var command = connection.CreateCommand();
285283
command.CommandTimeout = 1;
286-
command.CommandText = $"SELECT {10000 + step};";
284+
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
287285
var stopwatch = Stopwatch.StartNew();
288286
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
289287
Assert.InRange(stopwatch.ElapsedMilliseconds, 2900, 3500);
@@ -305,7 +303,7 @@ public async Task Test(int step, int method)
305303
connection.Open();
306304
using var command = connection.CreateCommand();
307305
command.CommandTimeout = 1;
308-
command.CommandText = $"SELECT {10000 + step};";
306+
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
309307
var stopwatch = Stopwatch.StartNew();
310308
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
311309
Assert.InRange(stopwatch.ElapsedMilliseconds, 2900, 3500);
@@ -328,7 +326,7 @@ public void Execute(int step, int method)
328326
connection.Open();
329327
using var command = connection.CreateCommand();
330328
command.CommandTimeout = 1;
331-
command.CommandText = $"SELECT {10000 + step};";
329+
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
332330
var stopwatch = Stopwatch.StartNew();
333331
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
334332
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
@@ -348,7 +346,7 @@ public async Task ExecuteAsync(int step, int method)
348346
connection.Open();
349347
using var command = connection.CreateCommand();
350348
command.CommandTimeout = 1;
351-
command.CommandText = $"SELECT {10000 + step};";
349+
command.CommandText = $"SELECT 0, 10000, {step}, 0;";
352350
var stopwatch = Stopwatch.StartNew();
353351
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
354352
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);

tests/MySqlConnector.Tests/FakeMySqlServerConnection.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,21 @@ public async Task RunAsync(TcpClient client, CancellationToken token)
8888
await SendAsync(stream, 4, x => x.Write(data));
8989
await SendAsync(stream, 5, x => x.Write(new byte[] { 0xFE, 0, 0, 2, 0 })); // EOF
9090
}
91-
else if ((match = Regex.Match(query, @"^SELECT ([0-9]{3,})(;|$)")).Success)
91+
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}"
9394
var number = match.Groups[1].Value;
95+
var value = int.Parse(number);
96+
var delay = int.Parse(match.Groups[2].Value);
97+
var pauseStep = int.Parse(match.Groups[3].Value);
98+
var respectCancellation = int.Parse(match.Groups[4].Value) != 0;
99+
94100
var data = new byte[number.Length + 1];
95101
data[0] = (byte) number.Length;
96102
Encoding.UTF8.GetBytes(number, 0, number.Length, data, 1);
97-
var value = int.Parse(number);
103+
98104
var negativeOne = new byte[] { 2, 0x2D, 0x31 };
99-
100-
var packets = new byte[][]
105+
var packets = new[]
101106
{
102107
new byte[] { 0xFF, 0x25, 0x05, 0x23, 0x37, 0x30, 0x31, 0x30, 0x30 }.Concat(Encoding.ASCII.GetBytes("Query execution was interrupted")).ToArray(), // error
103108
new byte[] { 1 }, // one column
@@ -113,18 +118,16 @@ public async Task RunAsync(TcpClient client, CancellationToken token)
113118
negativeOne,
114119
new byte[] { 0xFE, 0, 0, 2, 0 }, // EOF
115120
};
116-
var pauseStep = value % 100;
117-
var respectCancellation = value < 10000;
118121

119122
var queryInterrupted = false;
120-
for (int step = 1; step < packets.Length && !queryInterrupted; step++)
123+
for (var step = 1; step < packets.Length && !queryInterrupted; step++)
121124
{
122-
if (pauseStep == step || value == 100)
125+
if (pauseStep == step || pauseStep == -1)
123126
{
124127
if (respectCancellation)
125-
queryInterrupted = CancelQueryEvent.Wait(value, token);
128+
queryInterrupted = CancelQueryEvent.Wait(delay, token);
126129
else
127-
await Task.Delay(value, token);
130+
await Task.Delay(delay, token);
128131
}
129132

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

0 commit comments

Comments
 (0)