Skip to content

Commit

Permalink
Reduce test concurrency.
Browse files Browse the repository at this point in the history
There appears to be some kind of contention at the beginning of a test run that causes tests to fail with timeouts. Reducing the number of test classes runs fewer tests simultaneously, avoiding the problem.
  • Loading branch information
bgrainger committed Feb 28, 2021
1 parent cc54bc7 commit 9b400ed
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions tests/MySqlConnector.Tests/CancellationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public CancellationTests()

// NOTE: Multiple nested classes in order to force tests to run in parallel against each other

public class CancelExecuteXWithCommandTimeout : CancellationTests
public class CancelWithCommandTimeout : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetSyncMethodSteps))]
public void Test(int step, int method)
public void Execute(int step, int method)
{
using var connection = new MySqlConnection(m_csb.ConnectionString);
connection.Open();
Expand All @@ -50,30 +50,22 @@ public void Test(int step, int method)
command.CommandText = "SELECT 1;";
Assert.Equal(1, command.ExecuteScalar());
}
}

public class CancelExecuteXWithCancel : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetSyncMethodSteps))]
public void Test(int step, int method)
[MemberData(nameof(GetAsyncMethodSteps))]
public async Task ExecuteAsyncs(int step, int method)
{
using var connection = new MySqlConnection(m_csb.ConnectionString);
connection.Open();
using var command = connection.CreateCommand();
command.CommandTimeout = 10;
command.CommandTimeout = 1;
command.CommandText = $"SELECT {4000 + step};";
var task = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(1));
command.Cancel();
});
var stopwatch = Stopwatch.StartNew();
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
Assert.Equal(MySqlErrorCode.QueryInterrupted, ex.ErrorCode);
Assert.Null(ex.InnerException);
task.Wait();
Assert.Equal(MySqlErrorCode.CommandTimeoutExpired, ex.ErrorCode);
var inner = Assert.IsType<MySqlException>(ex.InnerException);
Assert.Equal(MySqlErrorCode.QueryInterrupted, inner.ErrorCode);

// connection should still be usable
Assert.Equal(ConnectionState.Open, connection.State);
Expand All @@ -82,36 +74,38 @@ public void Test(int step, int method)
}
}

public class CancelExecuteXAsyncWithCommandTimeout : CancellationTests
public class CancelWithCancel : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetAsyncMethodSteps))]
public async Task Test(int step, int method)
[MemberData(nameof(GetSyncMethodSteps))]
public void Execute(int step, int method)
{
using var connection = new MySqlConnection(m_csb.ConnectionString);
connection.Open();
using var command = connection.CreateCommand();
command.CommandTimeout = 1;
command.CommandTimeout = 10;
command.CommandText = $"SELECT {4000 + step};";
var task = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(1));
command.Cancel();
});
var stopwatch = Stopwatch.StartNew();
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await s_executeAsyncMethods[method](command, default));
var ex = Assert.Throws<MySqlException>(() => s_executeMethods[method](command));
Assert.InRange(stopwatch.ElapsedMilliseconds, 900, 1500);
Assert.Equal(MySqlErrorCode.CommandTimeoutExpired, ex.ErrorCode);
var inner = Assert.IsType<MySqlException>(ex.InnerException);
Assert.Equal(MySqlErrorCode.QueryInterrupted, inner.ErrorCode);
Assert.Equal(MySqlErrorCode.QueryInterrupted, ex.ErrorCode);
Assert.Null(ex.InnerException);
task.Wait();

// connection should still be usable
Assert.Equal(ConnectionState.Open, connection.State);
command.CommandText = "SELECT 1;";
Assert.Equal(1, command.ExecuteScalar());
}
}

public class CancelExecuteXAsyncWithCancel : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetAsyncMethodSteps))]
public async Task Test(int step, int method)
public async Task ExecuteAsync(int step, int method)
{
using var connection = new MySqlConnection(m_csb.ConnectionString);
connection.Open();
Expand Down Expand Up @@ -323,11 +317,11 @@ public async Task Test(int step, int method)
}
}

public class ExecuteXWithCancellationTimeoutIsNegativeOne : CancellationTests
public class WithCancellationTimeoutIsNegativeOne : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetSyncMethodSteps))]
public void Test(int step, int method)
public void Execute(int step, int method)
{
var csb = new MySqlConnectionStringBuilder(m_csb.ConnectionString) { CancellationTimeout = -1 };
using var connection = new MySqlConnection(csb.ConnectionString);
Expand All @@ -344,13 +338,10 @@ public void Test(int step, int method)
// connection is unusable
Assert.Equal(ConnectionState.Closed, connection.State);
}
}

public class ExecuteXAsyncWithCancellationTimeoutIsNegativeOne : CancellationTests
{
[SkipCITheory]
[MemberData(nameof(GetAsyncMethodSteps))]
public async Task Test(int step, int method)
public async Task ExecuteAsync(int step, int method)
{
var csb = new MySqlConnectionStringBuilder(m_csb.ConnectionString) { CancellationTimeout = -1 };
using var connection = new MySqlConnection(csb.ConnectionString);
Expand Down

0 comments on commit 9b400ed

Please sign in to comment.