Skip to content

Commit af93992

Browse files
authored
Make heartbeat test tolerant of odd wait times (#47688)
1 parent 7a0d0de commit af93992

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Servers/Kestrel/Core/test/HeartbeatTests.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ public async void HeartbeatLoopRunsWithSpecifiedInterval()
7070
Logger.LogInformation($"Starting heartbeat dispose.");
7171
}
7272

73-
// Interval timing isn't exact. For example, interval of 300ms results in split of 312.67ms.
74-
// Under load the server might take a long time to resume. Provide tolerance for late resume.
7573
Assert.Collection(splits,
7674
ts => AssertApproxEqual(intervalMs, ts.TotalMilliseconds),
7775
ts => AssertApproxEqual(intervalMs, ts.TotalMilliseconds),
7876
ts => AssertApproxEqual(intervalMs, ts.TotalMilliseconds),
7977
ts => AssertApproxEqual(intervalMs, ts.TotalMilliseconds));
8078

81-
static void AssertApproxEqual(double expectedValue, double value)
79+
static void AssertApproxEqual(double intervalMs, double actualMs)
8280
{
83-
if (value < expectedValue)
81+
// Interval timing isn't exact on a slow computer. For example, interval of 300ms results in split between 300ms and 450ms.
82+
// Under load the server might take a long time to resume. Provide tolerance for late resume.
83+
84+
// Round value to nearest 50. Avoids error when wait time is slightly less than expected value.
85+
var roundedActualMs = Math.Round(actualMs / 50.0) * 50.0;
86+
87+
if (roundedActualMs < intervalMs)
8488
{
85-
Assert.Fail($"{value} is smaller than wait time of {expectedValue}.");
89+
Assert.Fail($"{roundedActualMs} is smaller than wait time of {intervalMs}.");
8690
}
8791
// Be tolerant of a much larger value. Heartbeat might not immediately resume if the server is under load.
88-
if (value > expectedValue * 4)
92+
if (roundedActualMs > intervalMs * 4)
8993
{
90-
Assert.Fail($"{value} is much larger than wait time of {expectedValue}.");
94+
Assert.Fail($"{roundedActualMs} is much larger than wait time of {intervalMs}.");
9195
}
9296
}
9397
}

0 commit comments

Comments
 (0)