@@ -37,7 +37,7 @@ public void Execute(int step, int method)
37
37
connection . Open ( ) ;
38
38
using var command = connection . CreateCommand ( ) ;
39
39
command . CommandTimeout = 1 ;
40
- command . CommandText = $ "SELECT { 4000 + step } ;";
40
+ command . CommandText = $ "SELECT 0, 4000, { step } , 0 ;";
41
41
var stopwatch = Stopwatch . StartNew ( ) ;
42
42
var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
43
43
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
@@ -53,13 +53,60 @@ public void Execute(int step, int method)
53
53
54
54
[ SkipCITheory ]
55
55
[ MemberData ( nameof ( GetAsyncMethodSteps ) ) ]
56
- public async Task ExecuteAsyncs ( int step , int method )
56
+ public async Task ExecuteAsync ( int step , int method )
57
+ {
58
+ using var connection = new MySqlConnection ( m_csb . ConnectionString ) ;
59
+ connection . Open ( ) ;
60
+ using var command = connection . CreateCommand ( ) ;
61
+ command . CommandTimeout = 1 ;
62
+ command . CommandText = $ "SELECT 0, 4000, { step } , 0;";
63
+ var stopwatch = Stopwatch . StartNew ( ) ;
64
+ var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
65
+ Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
66
+ Assert . Equal ( MySqlErrorCode . CommandTimeoutExpired , ex . ErrorCode ) ;
67
+ var inner = Assert . IsType < MySqlException > ( ex . InnerException ) ;
68
+ Assert . Equal ( MySqlErrorCode . QueryInterrupted , inner . ErrorCode ) ;
69
+
70
+ // connection should still be usable
71
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
72
+ command . CommandText = "SELECT 1;" ;
73
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
74
+ }
75
+ }
76
+
77
+ public class CancelBufferedWithCommandTimeout : CancellationTests
78
+ {
79
+ [ SkipCITheory ]
80
+ [ MemberData ( nameof ( GetSyncMethodSteps ) ) ]
81
+ public void Execute ( int step , int method )
82
+ {
83
+ using var connection = new MySqlConnection ( m_csb . ConnectionString ) ;
84
+ connection . Open ( ) ;
85
+ using var command = connection . CreateCommand ( ) ;
86
+ command . CommandTimeout = 1 ;
87
+ command . CommandText = $ "SELECT 0, 4000, { step } , 2;";
88
+ var stopwatch = Stopwatch . StartNew ( ) ;
89
+ var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
90
+ Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
91
+ Assert . Equal ( MySqlErrorCode . CommandTimeoutExpired , ex . ErrorCode ) ;
92
+ var inner = Assert . IsType < MySqlException > ( ex . InnerException ) ;
93
+ Assert . Equal ( MySqlErrorCode . QueryInterrupted , inner . ErrorCode ) ;
94
+
95
+ // connection should still be usable
96
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
97
+ command . CommandText = "SELECT 1;" ;
98
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
99
+ }
100
+
101
+ [ SkipCITheory ]
102
+ [ MemberData ( nameof ( GetAsyncMethodSteps ) ) ]
103
+ public async Task ExecuteAsync ( int step , int method )
57
104
{
58
105
using var connection = new MySqlConnection ( m_csb . ConnectionString ) ;
59
106
connection . Open ( ) ;
60
107
using var command = connection . CreateCommand ( ) ;
61
108
command . CommandTimeout = 1 ;
62
- command . CommandText = $ "SELECT { 4000 + step } ;";
109
+ command . CommandText = $ "SELECT 0, 4000, { step } , 2 ;";
63
110
var stopwatch = Stopwatch . StartNew ( ) ;
64
111
var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
65
112
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
@@ -84,7 +131,7 @@ public void Execute(int step, int method)
84
131
connection . Open ( ) ;
85
132
using var command = connection . CreateCommand ( ) ;
86
133
command . CommandTimeout = 10 ;
87
- command . CommandText = $ "SELECT { 4000 + step } ;";
134
+ command . CommandText = $ "SELECT 0, 4000, { step } , 0 ;";
88
135
var task = Task . Run ( async ( ) =>
89
136
{
90
137
await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
@@ -111,7 +158,7 @@ public async Task ExecuteAsync(int step, int method)
111
158
connection . Open ( ) ;
112
159
using var command = connection . CreateCommand ( ) ;
113
160
command . CommandTimeout = 10 ;
114
- command . CommandText = $ "SELECT { 4000 + step } ;";
161
+ command . CommandText = $ "SELECT 0, 4000, { step } , 0 ;";
115
162
var task = Task . Run ( async ( ) =>
116
163
{
117
164
await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
@@ -141,7 +188,7 @@ public async Task Test(int step, int method)
141
188
connection . Open ( ) ;
142
189
using var command = connection . CreateCommand ( ) ;
143
190
command . CommandTimeout = 0 ;
144
- command . CommandText = $ "SELECT { 4000 + step } ;";
191
+ command . CommandText = $ "SELECT 0, 4000, { step } , 0 ;";
145
192
using var source = new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) ;
146
193
var stopwatch = Stopwatch . StartNew ( ) ;
147
194
var ex = await Assert . ThrowsAsync < OperationCanceledException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , source . Token ) ) ;
@@ -166,14 +213,13 @@ public void Test(int step, int method)
166
213
connection . Open ( ) ;
167
214
using var command = connection . CreateCommand ( ) ;
168
215
command . CommandTimeout = 1 ;
169
- var expected = 100 + step ;
170
- command . CommandText = $ "SELECT { expected } ;";
216
+ command . CommandText = $ "SELECT 42, 100, { step } , 0;";
171
217
var stopwatch = Stopwatch . StartNew ( ) ;
172
218
var result = s_executeMethods [ method ] ( command ) ;
173
219
if ( method == 1 )
174
220
Assert . Equal ( 0 , result ) ; // ExecuteNonQuery
175
221
else
176
- Assert . Equal ( expected , result ) ;
222
+ Assert . Equal ( 42 , result ) ;
177
223
Assert . InRange ( stopwatch . ElapsedMilliseconds , 50 , 250 ) ;
178
224
}
179
225
}
@@ -188,15 +234,14 @@ public async Task Test(int step, int method)
188
234
connection . Open ( ) ;
189
235
using var command = connection . CreateCommand ( ) ;
190
236
command . CommandTimeout = 0 ;
191
- var expected = 100 + step ;
192
- command . CommandText = $ "SELECT { expected } ;";
237
+ command . CommandText = $ "SELECT 42, 100, { step } , 0;";
193
238
using var source = new CancellationTokenSource ( TimeSpan . FromSeconds ( 1 ) ) ;
194
239
var stopwatch = Stopwatch . StartNew ( ) ;
195
240
var result = await s_executeAsyncMethods [ method ] ( command , source . Token ) ;
196
241
if ( method == 1 )
197
242
Assert . Equal ( 0 , result ) ; // ExecuteNonQuery
198
243
else
199
- Assert . Equal ( expected , result ) ;
244
+ Assert . Equal ( 42 , result ) ;
200
245
Assert . InRange ( stopwatch . ElapsedMilliseconds , 50 , 250 ) ;
201
246
}
202
247
}
@@ -212,7 +257,7 @@ public void Timeout(int method)
212
257
connection . Open ( ) ;
213
258
using var command = connection . CreateCommand ( ) ;
214
259
command . CommandTimeout = 1 ;
215
- command . CommandText = $ "SELECT 100;";
260
+ command . CommandText = $ "SELECT 0, 100, -1, 0 ;";
216
261
var stopwatch = Stopwatch . StartNew ( ) ;
217
262
var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
218
263
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
@@ -229,10 +274,10 @@ public void NoTimeout(int method)
229
274
connection . Open ( ) ;
230
275
using var command = connection . CreateCommand ( ) ;
231
276
command . CommandTimeout = 1 ;
232
- command . CommandText = $ "SELECT 100;";
277
+ command . CommandText = $ "SELECT 42, 100, -1, 0 ;";
233
278
var stopwatch = Stopwatch . StartNew ( ) ;
234
279
var result = s_executeMethods [ method ] ( command ) ;
235
- Assert . Equal ( 100 , result ) ;
280
+ Assert . Equal ( 42 , result ) ;
236
281
Assert . InRange ( stopwatch . ElapsedMilliseconds , 1100 , 1500 ) ;
237
282
}
238
283
}
@@ -248,7 +293,7 @@ public async Task Timeout(int method)
248
293
connection . Open ( ) ;
249
294
using var command = connection . CreateCommand ( ) ;
250
295
command . CommandTimeout = 1 ;
251
- command . CommandText = $ "SELECT 100;";
296
+ command . CommandText = $ "SELECT 0, 100, -1, 0 ;";
252
297
var stopwatch = Stopwatch . StartNew ( ) ;
253
298
var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
254
299
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
@@ -265,10 +310,10 @@ public async Task NoTimeout(int method)
265
310
connection . Open ( ) ;
266
311
using var command = connection . CreateCommand ( ) ;
267
312
command . CommandTimeout = 1 ;
268
- command . CommandText = $ "SELECT 100;";
313
+ command . CommandText = $ "SELECT 42, 100, -1, 0 ;";
269
314
var stopwatch = Stopwatch . StartNew ( ) ;
270
315
var result = await s_executeAsyncMethods [ method ] ( command , default ) ;
271
- Assert . Equal ( 100 , result ) ;
316
+ Assert . Equal ( 42 , result ) ;
272
317
Assert . InRange ( stopwatch . ElapsedMilliseconds , 1100 , 1500 ) ;
273
318
}
274
319
}
@@ -283,7 +328,7 @@ public void Test(int step, int method)
283
328
connection . Open ( ) ;
284
329
using var command = connection . CreateCommand ( ) ;
285
330
command . CommandTimeout = 1 ;
286
- command . CommandText = $ "SELECT { 10000 + step } ;";
331
+ command . CommandText = $ "SELECT 0, 10000, { step } , 1 ;";
287
332
var stopwatch = Stopwatch . StartNew ( ) ;
288
333
var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
289
334
Assert . InRange ( stopwatch . ElapsedMilliseconds , 2900 , 3500 ) ;
@@ -305,7 +350,7 @@ public async Task Test(int step, int method)
305
350
connection . Open ( ) ;
306
351
using var command = connection . CreateCommand ( ) ;
307
352
command . CommandTimeout = 1 ;
308
- command . CommandText = $ "SELECT { 10000 + step } ;";
353
+ command . CommandText = $ "SELECT 0, 10000, { step } , 1 ;";
309
354
var stopwatch = Stopwatch . StartNew ( ) ;
310
355
var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
311
356
Assert . InRange ( stopwatch . ElapsedMilliseconds , 2900 , 3500 ) ;
@@ -328,7 +373,7 @@ public void Execute(int step, int method)
328
373
connection . Open ( ) ;
329
374
using var command = connection . CreateCommand ( ) ;
330
375
command . CommandTimeout = 1 ;
331
- command . CommandText = $ "SELECT { 10000 + step } ;";
376
+ command . CommandText = $ "SELECT 0, 10000, { step } , 1 ;";
332
377
var stopwatch = Stopwatch . StartNew ( ) ;
333
378
var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
334
379
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
@@ -348,7 +393,7 @@ public async Task ExecuteAsync(int step, int method)
348
393
connection . Open ( ) ;
349
394
using var command = connection . CreateCommand ( ) ;
350
395
command . CommandTimeout = 1 ;
351
- command . CommandText = $ "SELECT { 10000 + step } ;";
396
+ command . CommandText = $ "SELECT 0, 10000, { step } , 1 ;";
352
397
var stopwatch = Stopwatch . StartNew ( ) ;
353
398
var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
354
399
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
0 commit comments