Skip to content
Merged
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
100 changes: 100 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4739,6 +4739,106 @@ try { $bcOk = (Invoke-PkProcessWithTimeout -FilePath $cmdExe -Arguments '/d /c e
Test-Assert ($bcOk -match '^ok') ("a program runs with -OutputEncoding '65001' ('" + ([string]$bcOk).Trim() + "')")
Write-Host ""

Write-Host "=== BD: -StreamTimeoutMs on the batch and the dispatcher ===" -ForegroundColor Cyan
# The wait for held output was fixed at 15 s in both, and since the wait
# moved into the dispatcher's default poll, a shorter -StreamTimeoutMs passed
# to Complete-PkProcess in -JobFinalize no longer shortened it. Each held
# command leaves a ping (-w 4321, BB's helpers) holding its output for 8 s.
Reset-Harness
function Invoke-HarnessHeldDispatch {
param([int]$StreamTimeoutMs, $FinalizeTimeoutMs)
$feed = @{ Given = $false }
$got = @{}
$jobs = @(New-HarnessHeldCommand -Seconds 8 -Key 'held')
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$null = Invoke-PkProcessDispatch -MaxConcurrent 1 -StreamTimeoutMs $StreamTimeoutMs `
-GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return @{ Name = 'held' } }.GetNewClosure() `
-JobBuilder { param($g) return $jobs }.GetNewClosure() `
-JobFinalize {
param($j, $q)
if ($null -eq $FinalizeTimeoutMs) { $got.R = Complete-PkProcess -Job $j } else { $got.R = Complete-PkProcess -Job $j -StreamTimeoutMs $FinalizeTimeoutMs }
}.GetNewClosure()
$sw.Stop()
return @{ Result = $got.R; Seconds = [math]::Round($sw.Elapsed.TotalSeconds, 1) }
}
try {
$bdOut = $null; $bdError = $null
$sw = [System.Diagnostics.Stopwatch]::StartNew()
try { $bdOut = Invoke-PkProcessBatch -StreamTimeoutMs 1000 -Commands @(New-HarnessHeldCommand -Seconds 8 -Key 'held') } catch { $bdError = [string]$_ }
$sw.Stop()
$bdR = $null; if ($null -ne $bdOut) { $bdR = @($bdOut.Results)[0] }
Test-Assert ($null -ne $bdR -and $bdR.StreamTimedOut -and $bdR.Stdout -match 'held' -and $sw.Elapsed.TotalSeconds -lt 4) ("batch -StreamTimeoutMs 1000: held output is given up on after about a second, with what arrived (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s" + $(if ($bdError) { ", error: " + $bdError }) + ")")
Stop-HarnessTaggedPings

$bdOut = $null; $bdError = $null
$sw = [System.Diagnostics.Stopwatch]::StartNew()
try { $bdOut = Invoke-PkProcessBatch -StreamTimeoutMs 0 -Commands @(New-HarnessHeldCommand -Seconds 8 -Key 'held') } catch { $bdError = [string]$_ }
$sw.Stop()
$bdR = $null; if ($null -ne $bdOut) { $bdR = @($bdOut.Results)[0] }
Test-Assert ($null -ne $bdR -and $bdR.StreamTimedOut -and $sw.Elapsed.TotalSeconds -lt 3) ("batch -StreamTimeoutMs 0: doesn't wait for held output (" + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s" + $(if ($bdError) { ", error: " + $bdError }) + ")")
Stop-HarnessTaggedPings

# The dispatcher: Complete-PkProcess with no -StreamTimeoutMs of its own
# uses the dispatcher's, so it doesn't wait again; one with its own waits
# only the rest.
$bdRun = $null; $bdError = $null
try { $bdRun = Invoke-HarnessHeldDispatch -StreamTimeoutMs 1000 -FinalizeTimeoutMs $null } catch { $bdError = [string]$_ }
Test-Assert ($null -ne $bdRun -and $bdRun.Result.StreamTimedOut -and $bdRun.Result.Stdout -match 'held' -and $bdRun.Seconds -lt 4) ("dispatcher -StreamTimeoutMs 1000: a plain Complete-PkProcess in -JobFinalize doesn't wait a second time (" + $(if ($bdRun) { [string]$bdRun.Seconds + " s" } else { $bdError }) + ")")
Stop-HarnessTaggedPings
$bdRun = $null; $bdError = $null
try { $bdRun = Invoke-HarnessHeldDispatch -StreamTimeoutMs 1000 -FinalizeTimeoutMs 3000 } catch { $bdError = [string]$_ }
Test-Assert ($null -ne $bdRun -and $bdRun.Result.StreamTimedOut -and $bdRun.Seconds -ge 2.5 -and $bdRun.Seconds -lt 6) ("dispatcher -StreamTimeoutMs 1000, Complete-PkProcess -StreamTimeoutMs 3000: waits the rest, about 3 s in all (" + $(if ($bdRun) { [string]$bdRun.Seconds + " s" } else { $bdError }) + ")")
Stop-HarnessTaggedPings

# A job -JobFinalize re-queues waits the dispatcher's limit again, from
# its own new exit.
$bdFeed = @{ Given = $false }
$bdGot = [System.Collections.Generic.List[object]]::new()
$bdJobs = @(New-HarnessHeldCommand -Seconds 8 -Key 'again')
$bdError = $null
$sw = [System.Diagnostics.Stopwatch]::StartNew()
try {
$null = Invoke-PkProcessDispatch -MaxConcurrent 1 -StreamTimeoutMs 1000 `
-GroupProvider { if ($bdFeed.Given) { return $null }; $bdFeed.Given = $true; return @{ Name = 'again' } }.GetNewClosure() `
-JobBuilder { param($g) return $bdJobs }.GetNewClosure() `
-JobFinalize { param($j, $q) $bdGot.Add((Complete-PkProcess -Job $j)); if ($bdGot.Count -eq 1) { $q.Enqueue($j) } }.GetNewClosure()
} catch { $bdError = [string]$_ }
$sw.Stop()
$bdBoth = @($bdGot | Where-Object { $_.StreamTimedOut -and $_.Stdout -match 'held' }).Count
Test-Assert ($bdGot.Count -eq 2 -and $bdBoth -eq 2 -and $sw.Elapsed.TotalSeconds -lt 5) ("dispatcher -StreamTimeoutMs 1000: a re-queued job is given up on after about a second again (" + $bdBoth + " of " + $bdGot.Count + " runs timed out with their output, " + [math]::Round($sw.Elapsed.TotalSeconds, 1) + " s" + $(if ($bdError) { ", error: " + $bdError }) + ")")
Stop-HarnessTaggedPings

# Inside a worker the kit isn't a module: the default poll still carries
# the limit it was given.
$bdRes = [System.Collections.Generic.List[object]]::new()
$bdOut = Invoke-PkRunspaceJobs -MaxParallel 1 -JobParams @(@{ Cmd = $cmdExe }) -ResultHandler { param($r) $bdRes.Add($r) }.GetNewClosure() -WorkerScript {
param($Cmd)
$wsw = [System.Diagnostics.Stopwatch]::StartNew()
$wb = Invoke-PkProcessBatch -StreamTimeoutMs 1000 -Commands @(@{ FilePath = $Cmd; Arguments = '/d /c echo held& start /b ping -n 9 -w 4321 127.0.0.1 >nul' })
$wr = @($wb.Results)[0]
[pscustomobject]@{ TimedOut = $wr.StreamTimedOut; Held = ($wr.Stdout -match 'held'); Seconds = [math]::Round($wsw.Elapsed.TotalSeconds, 1) }
}
$bdW = $null; if ($bdRes.Count -eq 1) { $bdW = $bdRes[0] }
Test-Assert ($bdOut.Errors.Count -eq 0 -and $null -ne $bdW -and $bdW.TimedOut -and $bdW.Held -and $bdW.Seconds -lt 4) ("a batch inside a worker honors -StreamTimeoutMs 1000 (" + $(if ($bdW) { [string]$bdW.Seconds + " s" } else { [string]$bdOut.Errors.Count + " errors: " + (@($bdOut.Errors | ForEach-Object { [string]$_.Error }) -join '; ') }) + ")")
Stop-HarnessTaggedPings

# A negative value is refused before anything starts.
$bdStarted = @{ Count = 0 }
$bdErrors = @()
foreach ($bdWhich in 'batch', 'dispatcher') {
$bdError = $null
try {
if ($bdWhich -eq 'batch') { $null = Invoke-PkProcessBatch -StreamTimeoutMs -1 -Commands @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }
else { $null = Invoke-PkProcessDispatch -MaxConcurrent 1 -StreamTimeoutMs -1 -GroupProvider { $bdStarted.Count++; return $null }.GetNewClosure() -JobBuilder { param($g) } -JobFinalize { param($j, $q) } }
} catch { $bdError = [string]$_ }
$bdErrors += $bdError
}
Test-Assert ($bdErrors[0] -like 'Invoke-PkProcessBatch: -StreamTimeoutMs must be 0 or more (got -1)*' -and $bdErrors[1] -like 'Invoke-PkProcessDispatch: -StreamTimeoutMs must be 0 or more (got -1)*' -and $bdStarted.Count -eq 0) ("-StreamTimeoutMs -1 is refused by both before anything starts (" + ($bdErrors -join ' | ') + ")")
} finally {
Stop-HarnessTaggedPings
}
Write-Host ""

Set-PkMessageHandler -Handler $null
Reset-PkAbortState

Expand Down
Loading
Loading