Skip to content

Commit 837891f

Browse files
committed
Test MySqlCommand.Cancel against the mock server.
1 parent 0589e29 commit 837891f

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/MySqlConnector.Tests/CancellationTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ public void Test(int step, int method)
5252
}
5353
}
5454

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+
5585
public class CancelExecuteXAsyncWithCommandTimeout : CancellationTests
5686
{
5787
[SkipCITheory]
@@ -69,6 +99,41 @@ public async Task Test(int step, int method)
6999
Assert.Equal(MySqlErrorCode.CommandTimeoutExpired, ex.ErrorCode);
70100
var inner = Assert.IsType<MySqlException>(ex.InnerException);
71101
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());
72137
}
73138
}
74139

@@ -89,6 +154,11 @@ public async Task Test(int step, int method)
89154
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
90155
var mySqlException = Assert.IsType<MySqlException>(ex.InnerException);
91156
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());
92162
}
93163
}
94164

0 commit comments

Comments
 (0)