Skip to content

Commit 44d4957

Browse files
committed
Make test timeouts less sensitive to slow CI environments.
Helps address #246.
1 parent 8719f55 commit 44d4957

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

tests/SideBySide/AppConfig.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public static MySqlConnectionStringBuilder CreateConnectionStringBuilder()
6565
return new MySqlConnectionStringBuilder(ConnectionString);
6666
}
6767

68+
// tests can run extremely slowly on Appveyor (and slightly slower under Travis)
69+
public static int TimeoutDelayFactor { get; } = Environment.GetEnvironmentVariable("APPVEYOR") == "True" ? 6 :
70+
Environment.GetEnvironmentVariable("TRAVIS") == "true" ? 3 : 1;
71+
6872
private static string GetCodeRootPath()
6973
{
7074
#if NET46

tests/SideBySide/CancelTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System;
32
using System.Data;
43
using System.Diagnostics;
54
using System.Linq;
@@ -112,7 +111,7 @@ public void CancelCommandBeforeRead()
112111
Assert.Equal((int) MySqlErrorCode.QueryInterrupted, ex.Number);
113112
}
114113
Assert.False(reader.NextResult());
115-
Assert.InRange(stopwatch.ElapsedMilliseconds, 0, 1000 * c_ciDelayFactor);
114+
TestUtilities.AssertDuration(stopwatch, 0, 1000);
116115
Assert.InRange(rows, 0, 10000000);
117116
}
118117
}
@@ -174,7 +173,7 @@ public void DapperQueryMultiple()
174173
stopwatch = Stopwatch.StartNew();
175174
}
176175
stopwatch.Stop();
177-
Assert.InRange(stopwatch.ElapsedMilliseconds, 0, 1000 * c_ciDelayFactor);
176+
TestUtilities.AssertDuration(stopwatch, 0, 1000);
178177
}
179178

180179
#if !BASELINE
@@ -294,7 +293,7 @@ public async Task CancelSlowQueryWithTokenAfterExecuteReader()
294293
var stopwatch = Stopwatch.StartNew();
295294
using (var reader = await cmd.ExecuteReaderAsync(cts.Token))
296295
{
297-
Assert.InRange(stopwatch.ElapsedMilliseconds, 450, 750 * c_ciDelayFactor);
296+
TestUtilities.AssertDuration(stopwatch, 450, 300);
298297

299298
var rows = 0;
300299
try
@@ -329,7 +328,7 @@ public async Task CancelSlowQueryWithTokenAfterNextResult()
329328
// the call to NextResult should block until the token is cancelled
330329
var stopwatch = Stopwatch.StartNew();
331330
Assert.True(await reader.NextResultAsync(cts.Token));
332-
Assert.InRange(stopwatch.ElapsedMilliseconds, 450, 750 * c_ciDelayFactor);
331+
TestUtilities.AssertDuration(stopwatch, 450, 300);
333332

334333
int rows = 0;
335334
try
@@ -393,10 +392,6 @@ private static CancellationToken GetCanceledToken()
393392
const string c_slowQuery = @"select * from integers a join integers b join integers c join integers d join integers e join integers f join integers g join integers h
394393
where sqrt(a.value) + sqrt(b.value) + sqrt(c.value) + sqrt(d.value) + sqrt(e.value) + sqrt(f.value) + sqrt(g.value) + sqrt(h.value) = 20;";
395394

396-
// tests can run extremely slowly on Appveyor (and slightly slower under Travis)
397-
static readonly int c_ciDelayFactor = Environment.GetEnvironmentVariable("APPVEYOR") == "True" ? 10 :
398-
Environment.GetEnvironmentVariable("TRAVIS") == "true" ? 3 : 1;
399-
400395
readonly DatabaseFixture m_database;
401396
}
402397
}

tests/SideBySide/ConnectSync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public void ConnectTimeout()
218218
var stopwatch = Stopwatch.StartNew();
219219
Assert.Throws<MySqlException>(() => connection.Open());
220220
stopwatch.Stop();
221-
Assert.InRange(stopwatch.ElapsedMilliseconds, 2800, 3500);
221+
TestUtilities.AssertDuration(stopwatch, 2900, 200);
222222
}
223223
}
224224

tests/SideBySide/ConnectionPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public async Task ExhaustConnectionPoolWithTimeout()
123123
var stopwatch = Stopwatch.StartNew();
124124
Assert.Throws<MySqlException>(() => extraConnection.Open());
125125
stopwatch.Stop();
126-
Assert.InRange(stopwatch.ElapsedMilliseconds, 4500, 5500);
126+
Assert.InRange(stopwatch.ElapsedMilliseconds, 4500, 6000);
127127
}
128128

129129
foreach (var connection in connections)

tests/SideBySide/TestUtilities.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Globalization;
34
using Xunit;
45

@@ -21,6 +22,16 @@ public static void AssertEqual(byte[] expected, byte[] actual)
2122
}
2223
}
2324

25+
/// <summary>
26+
/// Asserts that <paramref name="stopwatch"/> is in the range [minimumMilliseconds, minimumMilliseconds + lengthMilliseconds].
27+
/// </summary>
28+
/// <remarks>This method applies a scaling factor for delays encountered under Continuous Integration environments.</remarks>
29+
public static void AssertDuration(Stopwatch stopwatch, int minimumMilliseconds, int lengthMilliseconds)
30+
{
31+
var elapsed = stopwatch.ElapsedMilliseconds;
32+
Assert.InRange(elapsed, minimumMilliseconds, minimumMilliseconds + lengthMilliseconds * AppConfig.TimeoutDelayFactor);
33+
}
34+
2435
public static Version ParseServerVersion(string serverVersion)
2536
{
2637
// copied from MySql.Data.MySqlClient.ServerVersion

0 commit comments

Comments
 (0)