@@ -23,19 +23,6 @@ function Wait-ForJobRunning
2323 }
2424}
2525
26- function Wait-ForExpectedRSCount
27- {
28- param (
29- $expectedRSCount
30- )
31-
32- $iteration = 20
33- while (((Get-Runspace ).Count -ne $expectedRSCount ) -and ($iteration -- -gt 0 ))
34- {
35- Start-Sleep - Milliseconds 100
36- }
37- }
38-
3926Describe ' Basic ThreadJob Tests' - Tags ' CI' {
4027
4128 BeforeAll {
@@ -74,6 +61,21 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
7461 param ([string]$param1)
7562 Write-Output "$param1 $using:Var1 $using:Var2"
7663'@ > $scriptFilePath5
64+
65+ $WaitForCountFnScript = @'
66+ function Wait-ForExpectedRSCount
67+ {
68+ param (
69+ $expectedRSCount
70+ )
71+
72+ $iteration = 20
73+ while (((Get-Runspace).Count -ne $expectedRSCount) -and ($iteration-- -gt 0))
74+ {
75+ Start-Sleep -Milliseconds 100
76+ }
77+ }
78+ '@
7779 }
7880
7981 AfterEach {
@@ -265,6 +267,8 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
265267
266268 It ' ThreadJob Runspaces should be cleaned up at completion' {
267269
270+ $script = $WaitForCountFnScript + @'
271+
268272 try
269273 {
270274 Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
@@ -276,21 +280,27 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
276280 $job3 = Start-ThreadJob -ScriptBlock { "Hello 3!" }
277281 $job4 = Start-ThreadJob -ScriptBlock { "Hello 4!" }
278282
279- $job1 , $job2 , $job3 , $job4 | Wait-Job
283+ $null = $ job1,$job2,$job3,$job4 | Wait-Job
280284
281285 # Allow for runspace clean up to happen
282286 Wait-ForExpectedRSCount $rsStartCount
283287
284- ( Get-Runspace ).Count | Should - Be $rsStartCount
288+ Write-Output (( Get-Runspace).Count -eq $rsStartCount)
285289 }
286290 finally
287291 {
288292 Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
289293 }
294+ '@
295+
296+ $result = & " $PSHOME /pwsh" - c $script
297+ $result | Should - BeExactly " True"
290298 }
291299
292300 It ' ThreadJob Runspaces should be cleaned up after job removal' {
293301
302+ $script = $WaitForCountFnScript + @'
303+
294304 try {
295305 Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
296306 $rsStartCount = (Get-Runspace).Count
@@ -302,22 +312,26 @@ Describe 'Basic ThreadJob Tests' -Tags 'CI' {
302312 $job4 = Start-ThreadJob -ScriptBlock { Start-Sleep -Seconds 60 }
303313
304314 Wait-ForExpectedRSCount ($rsStartCount + 4)
305- ( Get-Runspace ).Count | Should - Be ($rsStartCount + 4 )
315+ Write-Output (( Get-Runspace).Count -eq ($rsStartCount + 4) )
306316
307317 # Stop two jobs
308318 $job1 | Remove-Job -Force
309319 $job3 | Remove-Job -Force
310320
311321 Wait-ForExpectedRSCount ($rsStartCount + 2)
312- ( Get-Runspace ).Count | Should - Be ($rsStartCount + 2 )
322+ Write-Output (( Get-Runspace).Count -eq ($rsStartCount + 2) )
313323 }
314324 finally
315325 {
316326 Get-Job | where PSJobTypeName -eq "ThreadJob" | Remove-Job -Force
317327 }
318328
319329 Wait-ForExpectedRSCount $rsStartCount
320- (Get-Runspace ).Count | Should - Be $rsStartCount
330+ Write-Output ((Get-Runspace).Count -eq $rsStartCount)
331+ '@
332+
333+ $result = & " $PSHOME /pwsh" - c $script
334+ $result | Should - BeExactly " True" , " True" , " True"
321335 }
322336
323337 It ' ThreadJob jobs should work with Receive-Job -AutoRemoveJob' {
0 commit comments