Fix flaky CanUpgradeRequestWithConnectionKeepAliveUpgradeHeader test (#7323)

* Randomize MockSystemClock
This commit is contained in:
Stephen Halter 2019-02-07 11:56:10 -08:00 committed by GitHub
parent 0211e894fe
commit 9c41d5b48f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -9,7 +9,16 @@ namespace Microsoft.AspNetCore.Testing
{
public class MockSystemClock : ISystemClock
{
private long _utcNowTicks = DateTimeOffset.UtcNow.Ticks;
private static Random _random = new Random();
private long _utcNowTicks;
public MockSystemClock()
{
// Use a random DateTimeOffset to ensure tests that incorrectly use the current DateTimeOffset fail always instead of only rarely.
// Pick a date between the min DateTimeOffset and a day before the max DateTimeOffset so there's room to advance the clock.
_utcNowTicks = NextLong(DateTimeOffset.MinValue.Ticks, DateTimeOffset.MaxValue.Ticks - TimeSpan.FromDays(1).Ticks);
}
public DateTimeOffset UtcNow
{
@ -25,5 +34,10 @@ namespace Microsoft.AspNetCore.Testing
}
public int UtcNowCalled { get; private set; }
private long NextLong(long minValue, long maxValue)
{
return (long)(_random.NextDouble() * (maxValue - minValue) + minValue);
}
}
}

View File

@ -130,7 +130,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
[Fact]
public async Task CanUpgradeRequestWithConnectionKeepAliveUpgradeHeader()
{
var testContext = new TestServiceContext();
var dataRead = false;
using (var server = new TestServer(async context =>
@ -154,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
await connection.ReceiveEnd(
"HTTP/1.1 101 Switching Protocols",
"Connection: Upgrade",
$"Date: {testContext.DateHeaderValue}",
$"Date: {server.Context.DateHeaderValue}",
"",
"");
}