test: classify remaining explicit waits

This commit is contained in:
Erik 2026-08-18 12:16:53 +02:00
parent 6faeb4a103
commit 056af276d0
2 changed files with 86 additions and 2 deletions

View file

@ -189,6 +189,41 @@ function Test-DirectOutputSignal {
return $false
}
function Get-WaitSites {
param(
[Parameter(Mandatory)]$Tree,
[Parameter(Mandatory)]$Method
)
$sites = [Collections.Generic.List[object]]::new()
foreach ($invocation in @($Method.DescendantNodes() | Where-Object {
$_.GetType().Name -eq 'InvocationExpressionSyntax'
})) {
$expression = $invocation.Expression.ToString()
$kind = if ($expression -match '(^|\.)Thread\.Sleep$') {
'ThreadSleep'
} elseif ($expression -match '(^|\.)Task\.Delay$') {
'TaskDelay'
} else {
$null
}
if ($null -eq $kind) {
continue
}
$text = [regex]::Replace($invocation.ToString(), '\s+', ' ').Trim()
$sites.Add([ordered]@{
Line = Get-NodeLine $Tree $invocation
Kind = $kind
Invocation = $text
IsCancellableInfiniteDelay = $kind -eq 'TaskDelay' -and
$text -match 'Timeout\.InfiniteTimeSpan' -and
$invocation.ArgumentList.Arguments.Count -ge 2
})
}
return @($sites)
}
function Test-RecursiveSignal {
param(
[Parameter(Mandatory)][string]$MethodKey,
@ -352,6 +387,7 @@ foreach ($relativePath in $trackedFiles) {
}
$bodyText = $method.ToString()
$waitSites = @(Get-WaitSites $tree $method)
$environmentVariables = @([regex]::Matches(
$bodyText,
'GetEnvironmentVariable\s*\(\s*"([A-Za-z0-9_]+)"') |
@ -376,8 +412,17 @@ foreach ($relativePath in $trackedFiles) {
HasOutputSignal = $hasOutputSignal
OutputOnlyCandidate = $hasOutputSignal -and -not $hasFailureSignal
EnvironmentVariables = $environmentVariables
HasThreadSleep = $bodyText -match '\bThread\.Sleep\s*\('
HasTaskDelay = $bodyText -match '\bTask\.Delay\s*\('
WaitSites = $waitSites
HasThreadSleep = @($waitSites | Where-Object {
$_.Kind -eq 'ThreadSleep'
}).Count -gt 0
HasTaskDelay = @($waitSites | Where-Object {
$_.Kind -eq 'TaskDelay'
}).Count -gt 0
HasOnlyCancellableInfiniteDelay = $waitSites.Count -gt 0 -and
@($waitSites | Where-Object {
-not $_.IsCancellableInfiniteDelay
}).Count -eq 0
ReadsSourceText = $bodyText -match '(ReadAllText|ReadAllLines)\s*\(' -and
$bodyText -match '\.cs'
})
@ -453,6 +498,12 @@ $summary = [ordered]@{
}
ThreadSleepMethods = @($orderedRecords | Where-Object { $_.HasThreadSleep }).Count
TaskDelayMethods = @($orderedRecords | Where-Object { $_.HasTaskDelay }).Count
CancellableInfiniteDelayOnlyMethods = @($orderedRecords | Where-Object {
$_.HasOnlyCancellableInfiniteDelay
}).Count
FixedWallClockWaitMethods = @($orderedRecords | Where-Object {
$_.WaitSites.Count -gt 0 -and -not $_.HasOnlyCancellableInfiniteDelay
}).Count
DirectEnvironmentVariableMethods = @($orderedRecords | Where-Object {
$_.EnvironmentVariables.Count -gt 0
}).Count