Skip to content
Merged
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
50 changes: 32 additions & 18 deletions test/powershell/Modules/ThreadJob/ThreadJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ function Wait-ForJobRunning
}
}

function Wait-ForExpectedRSCount
{
param (
$expectedRSCount
)

$iteration = 20
while (((Get-Runspace).Count -ne $expectedRSCount) -and ($iteration-- -gt 0))
{
Start-Sleep -Milliseconds 100
}
}

Describe 'Basic ThreadJob Tests' -Tags 'CI' {

BeforeAll {
Expand Down Expand Up @@ -74,6 +61,21 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
param ([string]$param1)
Write-Output "$param1 $using:Var1 $using:Var2"
'@ > $scriptFilePath5

$WaitForCountFnScript = @'
function Wait-ForExpectedRSCount
{
param (
$expectedRSCount
)

$iteration = 20
while (((Get-Runspace).Count -ne $expectedRSCount) -and ($iteration-- -gt 0))
{
Start-Sleep -Milliseconds 100
}
}
'@
}

AfterEach {
Expand Down Expand Up @@ -265,6 +267,8 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {

It 'ThreadJob Runspaces should be cleaned up at completion' {

$script = $WaitForCountFnScript + @'

try
{
Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
Expand All @@ -276,21 +280,27 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
$job3 = Start-ThreadJob -ScriptBlock { "Hello 3!" }
$job4 = Start-ThreadJob -ScriptBlock { "Hello 4!" }

$job1,$job2,$job3,$job4 | Wait-Job
$null = $job1,$job2,$job3,$job4 | Wait-Job

# Allow for runspace clean up to happen
Wait-ForExpectedRSCount $rsStartCount

(Get-Runspace).Count | Should -Be $rsStartCount
Write-Output ((Get-Runspace).Count -eq $rsStartCount)
}
finally
{
Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
}
'@

$result = & "$PSHOME/pwsh" -c $script
$result | Should -BeExactly "True"
}

It 'ThreadJob Runspaces should be cleaned up after job removal' {

$script = $WaitForCountFnScript + @'

try {
Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
$rsStartCount = (Get-Runspace).Count
Expand All @@ -302,22 +312,26 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
$job4 = Start-ThreadJob -ScriptBlock { Start-Sleep -Seconds 60 }

Wait-ForExpectedRSCount ($rsStartCount + 4)
(Get-Runspace).Count | Should -Be ($rsStartCount + 4)
Write-Output ((Get-Runspace).Count -eq ($rsStartCount + 4))

# Stop two jobs
$job1 | Remove-Job -Force
$job3 | Remove-Job -Force

Wait-ForExpectedRSCount ($rsStartCount + 2)
(Get-Runspace).Count | Should -Be ($rsStartCount + 2)
Write-Output ((Get-Runspace).Count -eq ($rsStartCount + 2))
}
finally
{
Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
}

Wait-ForExpectedRSCount $rsStartCount
(Get-Runspace).Count | Should -Be $rsStartCount
Write-Output ((Get-Runspace).Count -eq $rsStartCount)
'@

$result = & "$PSHOME/pwsh" -c $script
$result | Should -BeExactly "True","True","True"
}

It 'ThreadJob jobs should work with Receive-Job -AutoRemoveJob' {
Expand Down