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
36 changes: 36 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5324,6 +5324,42 @@ $bcJob = [hashtable]::new(); $bcJob['name'] = 'a'
$bcOut = $null; $bcError = $null
try { $bcOut = Invoke-PkRunspaceBatch -MaxConcurrent 1 -JobParams @(,$bcJob) -WorkerScript { param([Parameter(Mandatory)][string]$Name) $Name } } catch { $bcError = [string]$_ }
Test-Assert ($null -eq $bcError -and $null -ne $bcOut -and $bcOut.Errors.Count -eq 0) ("a mandatory parameter supplied in another case, in a case-sensitive hashtable, isn't counted as missing (" + $bcError + ")")
# A case-sensitive job's own 'canceltoken' is its CancelToken, as binding
# would have it: the kit doesn't put its own in its place. The caller's token
# here is cancelled, so the worker can tell which one it got.
$bcMine = [System.Threading.CancellationTokenSource]::new(); $bcMine.Cancel()
$bcTokenWorker = { param($Id, $CancelToken) $CancelToken.IsCancellationRequested }
$bcJob = [hashtable]::new(); $bcJob['canceltoken'] = $bcMine.Token; $bcJob['Id'] = 1
$bcSeen = [System.Collections.Generic.List[object]]::new(); $bcError = $null
try { $null = Invoke-PkRunspaceBatch -MaxConcurrent 1 -JobParams @(,$bcJob) -WorkerScript $bcTokenWorker -ResultHandler { param($r) $bcSeen.Add($r) }.GetNewClosure() } catch { $bcError = [string]$_ }
Test-Assert ($null -eq $bcError -and $bcSeen.Count -eq 1 -and $bcSeen[0] -eq $true) ("-JobParams: a case-sensitive job's 'canceltoken' reaches the worker, not the kit's token (got the caller's: " + ($bcSeen -join ',') + "; " + $bcError + ")")
$bcGiven = @{ N = 0 }
$bcSeen = [System.Collections.Generic.List[object]]::new(); $bcError = $null
try {
$null = Invoke-PkRunspaceBatch -MaxConcurrent 1 -WorkerScript $bcTokenWorker -ResultHandler { param($r) $bcSeen.Add($r) }.GetNewClosure() -NextJobProvider {
if ($bcGiven.N -ge 1) { return $null }
$bcGiven.N++; $j = [hashtable]::new(); $j['canceltoken'] = $bcMine.Token; $j['Id'] = 2; return $j
}.GetNewClosure()
} catch { $bcError = [string]$_ }
Test-Assert ($null -eq $bcError -and $bcSeen.Count -eq 1 -and $bcSeen[0] -eq $true) ("-NextJobProvider: the same (got the caller's: " + ($bcSeen -join ',') + "; " + $bcError + ")")
$bcMine.Dispose()
# Two keys differing only in case would collapse into one in the worker, the
# later silently winning: refused, naming both.
$bcJob = [hashtable]::new(); $bcJob['Id'] = 1; $bcJob['id'] = 2
$bcRan = [System.Collections.Generic.List[object]]::new(); $bcError = $null
try { $null = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams @(@{ Id = 0 }, $bcJob) -WorkerScript { param($Id) $Id } -ResultHandler { param($r) $bcRan.Add($r) }.GetNewClosure() } catch { $bcError = [string]$_ }
Test-Assert ($bcError -match "job 1 supplies '(Id|id)' and '(Id|id)', which differ only in case" -and $bcError -match 'Nothing was started' -and $bcRan.Count -eq 0) ("-JobParams: a case-sensitive job with 'Id' and 'id' is refused before anything starts (" + $bcError + "; ran " + $bcRan.Count + ")")
$bcGiven = @{ N = 0 }
$bcRan = [System.Collections.Generic.List[object]]::new(); $bcError = $null
try {
$null = Invoke-PkRunspaceBatch -MaxConcurrent 1 -WorkerScript { param($Id) $Id } -ResultHandler { param($r) $bcRan.Add($r) }.GetNewClosure() -NextJobProvider {
$bcGiven.N++
if ($bcGiven.N -eq 1) { return @{ Id = 0 } }
if ($bcGiven.N -eq 2) { $j = [hashtable]::new(); $j['Id'] = 1; $j['id'] = 2; return $j }
return $null
}.GetNewClosure()
} catch { $bcError = [string]$_ }
Test-Assert ($bcError -match "a job from -NextJobProvider supplies '(Id|id)' and '(Id|id)', which differ only in case" -and ($bcRan -join ',') -eq '0') ("-NextJobProvider: the same, refused as it arrives (" + $bcError + "; ran " + ($bcRan -join ',') + ")")
# Not refused: a mandatory CancelToken (the kit supplies it), Mandatory =
# $false, and parameter sets (which parameters are needed depends on the set).
$bcOut = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams @(@{ Id = 1 }) -WorkerScript { param([Parameter(Mandatory)][System.Threading.CancellationToken]$CancelToken, [Parameter(Mandatory = $false)]$Opt, $Id) $Id }
Expand Down
34 changes: 31 additions & 3 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3864,6 +3864,10 @@ function Invoke-PkRunspaceBatch {
# A fixed list can be checked before a single worker starts.
if ($null -ne $JobParams) {
for ($jobIndex = 0; $jobIndex -lt $JobParams.Count; $jobIndex++) {
$caseKeys = Get-PkCaseDuplicateJobKey -Job $JobParams[$jobIndex]
if ($null -ne $caseKeys) {
throw ("Invoke-PkRunspaceBatch: job " + $jobIndex + " supplies " + $caseKeys + ", which differ only in case. Parameter names don't, so the worker would get only one of them. Keep one. Nothing was started.")
}
foreach ($key in $JobParams[$jobIndex].Keys) {
if (-not $declaredParams.Contains([string]$key)) {
throw ("Invoke-PkRunspaceBatch: job " + $jobIndex + " supplies '" + $key + "', but -WorkerScript's param() block doesn't declare it. Nothing was started.")
Expand Down Expand Up @@ -4002,13 +4006,22 @@ function Invoke-PkRunspaceBatch {
if ($null -ne $sbKey) { throw (Get-PkScriptBlockJobMessage -Job "a job from -NextJobProvider" -Key $sbKey) }
}

$ps = [PowerShell]::Create()
$ps.RunspacePool = $pool
$jobValues = @{}
foreach ($key in $nextJob.Keys) { $jobValues[$key] = $nextJob[$key] }
if ($declaredParams.Contains('CancelToken') -and -not $nextJob.ContainsKey('CancelToken')) {
# $jobValues is case-insensitive, like binding: fewer keys than
# the job means a case-sensitive [hashtable]::new() job had two
# differing only in case (a -JobParams job's were refused before
# the run).
if ($jobValues.Count -ne $nextJob.Count) {
throw ("Invoke-PkRunspaceBatch: a job from -NextJobProvider supplies " + (Get-PkCaseDuplicateJobKey -Job $nextJob) + ", which differ only in case. Parameter names don't, so the worker would get only one of them. Keep one.")
}
# And read here, not on the job, so a case-sensitive job's
# 'canceltoken' counts as its own.
if ($declaredParams.Contains('CancelToken') -and -not $jobValues.ContainsKey('CancelToken')) {
$jobValues['CancelToken'] = $Cts.Token
}
$ps = [PowerShell]::Create()
$ps.RunspacePool = $pool
$jobLocation = ""
if ([System.IO.Directory]::Exists($workerLocation)) { $jobLocation = $workerLocation }
# A local scope too, so variables a job sets (preference
Expand Down Expand Up @@ -4194,6 +4207,21 @@ function Get-PkCommandKind {
return 'UnloadableModuleFunction'
}

function Get-PkCaseDuplicateJobKey {
# Two keys of a job hashtable that differ only in case, as "'Id' and
# 'id'", else $null. Only a case-sensitive [hashtable]::new() can hold
# them; the worker's copy is case-insensitive, like parameter binding, so
# they'd collapse into one, the later silently winning.
param([Parameter(Mandatory)][hashtable]$Job)
$seen = @{}
foreach ($key in $Job.Keys) {
$name = [string]$key
if ($seen.ContainsKey($name)) { return ("'" + $seen[$name] + "' and '" + $name + "'") }
$seen[$name] = $name
}
return $null
}

function Get-PkScriptBlockJobKey {
# The first key of a job hashtable whose value is a scriptblock, or holds
# one up to three levels down (in arrays, lists, dictionaries and
Expand Down