@@ -52,6 +52,36 @@ public void Test(int step, int method)
52
52
}
53
53
}
54
54
55
+ public class CancelExecuteXWithCancel : CancellationTests
56
+ {
57
+ [ SkipCITheory ]
58
+ [ MemberData ( nameof ( GetSyncMethodSteps ) ) ]
59
+ public void Test ( int step , int method )
60
+ {
61
+ using var connection = new MySqlConnection ( m_csb . ConnectionString ) ;
62
+ connection . Open ( ) ;
63
+ using var command = connection . CreateCommand ( ) ;
64
+ command . CommandTimeout = 10 ;
65
+ command . CommandText = $ "SELECT { 4000 + step } ;";
66
+ var task = Task . Run ( async ( ) =>
67
+ {
68
+ await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
69
+ command . Cancel ( ) ;
70
+ } ) ;
71
+ var stopwatch = Stopwatch . StartNew ( ) ;
72
+ var ex = Assert . Throws < MySqlException > ( ( ) => s_executeMethods [ method ] ( command ) ) ;
73
+ Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
74
+ Assert . Equal ( MySqlErrorCode . QueryInterrupted , ex . ErrorCode ) ;
75
+ Assert . Null ( ex . InnerException ) ;
76
+ task . Wait ( ) ;
77
+
78
+ // connection should still be usable
79
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
80
+ command . CommandText = "SELECT 1;" ;
81
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
82
+ }
83
+ }
84
+
55
85
public class CancelExecuteXAsyncWithCommandTimeout : CancellationTests
56
86
{
57
87
[ SkipCITheory ]
@@ -69,6 +99,41 @@ public async Task Test(int step, int method)
69
99
Assert . Equal ( MySqlErrorCode . CommandTimeoutExpired , ex . ErrorCode ) ;
70
100
var inner = Assert . IsType < MySqlException > ( ex . InnerException ) ;
71
101
Assert . Equal ( MySqlErrorCode . QueryInterrupted , inner . ErrorCode ) ;
102
+
103
+ // connection should still be usable
104
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
105
+ command . CommandText = "SELECT 1;" ;
106
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
107
+ }
108
+ }
109
+
110
+ public class CancelExecuteXAsyncWithCancel : CancellationTests
111
+ {
112
+ [ SkipCITheory ]
113
+ [ MemberData ( nameof ( GetAsyncMethodSteps ) ) ]
114
+ public async Task Test ( int step , int method )
115
+ {
116
+ using var connection = new MySqlConnection ( m_csb . ConnectionString ) ;
117
+ connection . Open ( ) ;
118
+ using var command = connection . CreateCommand ( ) ;
119
+ command . CommandTimeout = 10 ;
120
+ command . CommandText = $ "SELECT { 4000 + step } ;";
121
+ var task = Task . Run ( async ( ) =>
122
+ {
123
+ await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
124
+ command . Cancel ( ) ;
125
+ } ) ;
126
+ var stopwatch = Stopwatch . StartNew ( ) ;
127
+ var ex = await Assert . ThrowsAsync < MySqlException > ( async ( ) => await s_executeAsyncMethods [ method ] ( command , default ) ) ;
128
+ Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
129
+ Assert . Equal ( MySqlErrorCode . QueryInterrupted , ex . ErrorCode ) ;
130
+ Assert . Null ( ex . InnerException ) ;
131
+ task . Wait ( ) ;
132
+
133
+ // connection should still be usable
134
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
135
+ command . CommandText = "SELECT 1;" ;
136
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
72
137
}
73
138
}
74
139
@@ -89,6 +154,11 @@ public async Task Test(int step, int method)
89
154
Assert . InRange ( stopwatch . ElapsedMilliseconds , 900 , 1500 ) ;
90
155
var mySqlException = Assert . IsType < MySqlException > ( ex . InnerException ) ;
91
156
Assert . Equal ( MySqlErrorCode . QueryInterrupted , mySqlException . ErrorCode ) ;
157
+
158
+ // connection should still be usable
159
+ Assert . Equal ( ConnectionState . Open , connection . State ) ;
160
+ command . CommandText = "SELECT 1;" ;
161
+ Assert . Equal ( 1 , command . ExecuteScalar ( ) ) ;
92
162
}
93
163
}
94
164
0 commit comments