Fixes #2534 by replacing expensive WMI query with Win32 API calls (#2535)

* Fixes #2534 by replacing expensive WMI query with Win32 API calls

* fix break on unix build

* added tests for #2535

* although test passed, fixing exception that shows up

* fixed Describe text

* addressing code review feedback

* addressing review feedback to comment on why sleep is needed
added check that test processes are created before we try to kill them

* fixed test to timeout and pending fix for #2561
This commit is contained in:
Steve Lee
2016-10-31 09:41:13 -07:00
committed by Mike Richmond
parent 0e8c809ffb
commit 44eb20dc8f
8 changed files with 261 additions and 45 deletions
@@ -0,0 +1,55 @@
Describe "Native Command Processor" -tags "Feature" {
BeforeAll {
# Find where test/powershell is so we can find the createchildprocess command relative to it
$powershellTestDir = $PSScriptRoot
while ($powershellTestDir -notmatch 'test[\\/]powershell$') {
$powershellTestDir = Split-Path $powershellTestDir
}
$createchildprocess = Join-Path (Split-Path $powershellTestDir) tools/CreateChildProcess/bin/createchildprocess
}
# If powershell receives a StopProcessing, it should kill the native process and all child processes
# this test should pass and no longer Penidng when #2561 is fixed
It "Should kill native process tree" {
Test-Path $createchildprocess | Should Be $true
# make sure no test processes are running
# on Linux, the Process class truncates the name so filter using Where-Object
Get-Process | Where-Object {$_.Name -like 'createchildproc'} | Stop-Process
[int] $numToCreate = 2
$ps = [PowerShell]::Create().AddCommand($createchildprocess)
$ps.AddParameter($numToCreate)
$async = $ps.BeginInvoke()
$ps.InvocationStateInfo.State | Should Be "Running"
[bool] $childrenCreated = $false
while (-not $childrenCreated)
{
$childprocesses = Get-Process | Where-Object {$_.Name -like 'createchildproc'}
if ($childprocesses.count -eq $numToCreate+1)
{
$childrenCreated = $true
}
}
$startTime = Get-Date
$beginsync = $ps.BeginStop($null, $async)
# wait no more than 5 secs for the processes to be terminated, otherwise test has failed
while (((Get-Date) - $startTime).TotalSeconds -lt 5)
{
if (($childprocesses.hasexited -eq $true).count -eq $numToCreate+1)
{
break
}
}
$childprocesses = Get-Process | Where-Object {$_.Name -like 'createchildproc'}
$count = $childprocesses.count
$childprocesses | Stop-Process
$count | Should Be 0
}
}