Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions tst/functions/assert/Time/Should-BeFasterThan.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ InPesterModuleScope {
}

Describe "Should-BeFasterThan" {
# These [scriptblock] tests exercise the measure-and-compare path, not timing precision, so they
# use the floor/ceiling of any measurement rather than a threshold near the real run time. 10s is
# a ceiling no real script block reaches, so nothing measured here can cross it.
It "Does not throw when actual is faster than expected" -ForEach @(
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "100ms" }
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "10s" }
) {
$Actual | Should-BeFasterThan -Expected $Expected
}
Expand All @@ -36,8 +39,14 @@ Describe "Should-BeFasterThan" {
$Actual | Should-BeFasterThan -Expected $Expected
}

# 0ms is the floor of any measurement: a script block always takes >= 0, so this fails
# deterministically. The old 1ms bound sat just under the real ~10-15ms sleep and relied on a
# single Start-Sleep never being measured below it. On one CI run it was (the whole test ran in
# 8ms), so the assertion passed instead of failing. Re-testing the same Windows agents with tens
# of thousands of samples could not reproduce a sub-1ms measurement and confirmed Stopwatch/QPC
# is accurate there -- a rare transient outlier. Asserting against the 0ms floor removes the race.
It "Throws when scriptblock is slower than expected" -ForEach @(
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "1ms" }
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "0ms" }
) {
{ $Actual | Should-BeFasterThan -Expected $Expected } | Verify-AssertionFailed
}
Expand Down
10 changes: 8 additions & 2 deletions tst/functions/assert/Time/Should-BeSlowerThan.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Set-StrictMode -Version Latest

Describe "Should-BeSlowerThan" {
# 0ms is the floor of any measurement: a script block always takes >= 0, so it is always slower
# than 0ms and this passes deterministically, without racing the real run time against a nearby bound.
It "Does not throw when actual is slower than expected" -ForEach @(
@{ Actual = { Start-Sleep -Milliseconds 100 }; Expected = "1ms" }
@{ Actual = { Start-Sleep -Milliseconds 100 }; Expected = "0ms" }
) {
$Actual | Should-BeSlowerThan -Expected $Expected
}
Expand All @@ -13,8 +15,12 @@ Describe "Should-BeSlowerThan" {
$Actual | Should-BeSlowerThan -Expected $Expected
}

# 10s is a ceiling no real script block reaches, so this always registers as "not slower" and
# fails deterministically -- the mirror of the Should-BeFasterThan flake, where a single timed
# Start-Sleep landed on the wrong side of a bound set close to its real run time. Keep the bound
# far from any duration a [scriptblock] can produce so a rare timing outlier cannot cross it.
It "Throws when scriptblock is faster than expected" -ForEach @(
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "1000ms" }
@{ Actual = { Start-Sleep -Milliseconds 10 }; Expected = "10s" }
) {
{ $Actual | Should-BeSlowerThan -Expected $Expected } | Verify-AssertionFailed
}
Expand Down
Loading