Files
PowerShell/test/powershell/engine/Api/BasicEngine.Tests.ps1
T
Dongbo Wang 9b94f18ccf Fix BeginInvoke/EndInvoke to return results when Stop or BeginStop/EndStop was called previously (#7917)
The root cause is that `OutputBuffer` is not cleaned up (set to `null`) in `Stop` and `BeginStop/EndStop` when the powershell instance owns the `OutputBuffer` object. Since the pipeline has been intentionally stopped by the caller, the `OutputBuffer` should be set to `null` as well when it's owned by the PowerShell instance, just like how it's cleaned up in `EndInvoke`.
2018-10-12 10:46:57 -07:00

91 lines
3.1 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe 'Basic engine APIs' -Tags "CI" {
Context 'powershell::Create' {
It 'can create default instance' {
[powershell]::Create() | Should -Not -BeNullOrEmpty
}
It "can load the default snapin 'Microsoft.WSMan.Management'" -skip:(-not $IsWindows) {
$ps = [powershell]::Create()
$ps.AddScript("Get-Command -Name Test-WSMan") > $null
$result = $ps.Invoke()
$result.Count | Should -Be 1
$result[0].Source | Should -BeExactly "Microsoft.WSMan.Management"
}
}
Context 'executioncontext' {
It 'args are passed correctly' {
$result = $ExecutionContext.SessionState.InvokeCommand.InvokeScript('"`$args:($args); `$input:($input)"', 1, 2, 3)
$result | Should -BeExactly '$args:(1 2 3); $input:()'
}
}
}
Describe "Clean up open Runspaces when exit powershell process" -Tags "Feature" {
It "PowerShell process should not freeze at exit" {
$command = @'
-c $rs = [runspacefactory]::CreateRunspacePool(1,5)
$rs.Open()
$ps = [powershell]::Create()
$ps.RunspacePool = $rs
$null = $ps.AddScript(1).Invoke()
write-host should_not_stop_responding_at_exit
exit
'@
$process = Start-Process pwsh -ArgumentList $command -PassThru
Wait-UntilTrue -sb { $process.HasExited } -TimeoutInMilliseconds 5000 -IntervalInMilliseconds 1000 > $null
$expect = "powershell process exits in 5 seconds"
if (-not $process.HasExited) {
Stop-Process -InputObject $process -Force -ErrorAction SilentlyContinue
"powershell process doesn't exit in 5 seconds" | Should -Be $expect
} else {
$expect | Should -Be $expect
}
}
}
Describe "EndInvoke() should return a collection of results" -Tags "CI" {
BeforeAll {
$ps = [powershell]::Create()
$ps.AddScript("'Hello'; 'World'") > $null
}
It "BeginInvoke() and then EndInvoke() should return a collection of results" {
$async = $ps.BeginInvoke()
$result = $ps.EndInvoke($async)
$result.Count | Should -BeExactly 2
$result[0] | Should -BeExactly "Hello"
$result[1] | Should -BeExactly "World"
}
It "BeginInvoke() and then EndInvoke() should return a collection of results after a previous Stop() call" {
$async = $ps.BeginInvoke()
$ps.Stop()
$async = $ps.BeginInvoke()
$result = $ps.EndInvoke($async)
$result.Count | Should -BeExactly 2
$result[0] | Should -BeExactly "Hello"
$result[1] | Should -BeExactly "World"
}
It "BeginInvoke() and then EndInvoke() should return a collection of results after a previous BeginStop()/EndStop() call" {
$asyncInvoke = $ps.BeginInvoke()
$asyncStop = $ps.BeginStop($null, $null)
$ps.EndStop($asyncStop)
$asyncInvoke = $ps.BeginInvoke()
$result = $ps.EndInvoke($asyncInvoke)
$result.Count | Should -BeExactly 2
$result[0] | Should -BeExactly "Hello"
$result[1] | Should -BeExactly "World"
}
}