From bc807322ed0fb8c12d00f39f947636398ebb244f Mon Sep 17 00:00:00 2001 From: "James Truher [MSFT]" Date: Fri, 9 Jun 2017 10:12:46 -0700 Subject: [PATCH] Alter test httplistener to use a new runspace rather than a job (#3966) This improves the performance of the listener by not relying on a new process starting up to run the listener. It improves the debugability of tests by providing more direct access to the session executing the listener. It also reverses the blocking nature of starting the listener. By default, Start-HttpListener will no longer block, you need to use -Foreground to have Start-HttpListener block. Lastly, create a way to catch errors if the listener has a problem. If code in the listener throws, it emits an error record. --- .../WebCmdlets.Tests.ps1 | 6 ++-- .../Modules/HttpListener/HttpListener.psm1 | 29 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index d01c52a655..4d76a6c3ca 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -146,11 +146,12 @@ function GetTestData Describe "Invoke-WebRequest tests" -Tags "Feature" { BeforeAll { - $null = Start-HttpListener -AsJob -Port 8080 + $response = Start-HttpListener -Port 8080 } AfterAll { $null = Stop-HttpListener -Port 8080 + $response.PowerShell.Dispose() } # Validate the output of Invoke-WebRequest @@ -576,11 +577,12 @@ Describe "Invoke-WebRequest tests" -Tags "Feature" { Describe "Invoke-RestMethod tests" -Tags "Feature" { BeforeAll { - $null = Start-HttpListener -AsJob -Port 8081 + $response = Start-HttpListener -Port 8081 } AfterAll { $null = Stop-HttpListener -Port 8081 + $response.PowerShell.Dispose() } It "Invoke-RestMethod returns User-Agent" { diff --git a/test/tools/Modules/HttpListener/HttpListener.psm1 b/test/tools/Modules/HttpListener/HttpListener.psm1 index 1d6a795d26..becb21ac80 100644 --- a/test/tools/Modules/HttpListener/HttpListener.psm1 +++ b/test/tools/Modules/HttpListener/HttpListener.psm1 @@ -37,7 +37,7 @@ Function Start-HTTPListener { [Int] $Port = 8080, [Parameter()] - [switch] $AsJob + [switch] $Foreground ) Process { @@ -214,19 +214,36 @@ Function Start-HTTPListener { $response.Close() } } + catch + { + $errormsg = $_ | convertto-json + Write-Error $errormsg + } finally { $listener.Stop() + Write-Information "Listener is stopped" -InformationAction Continue } } - if ($AsJob) - { - Start-Job -ScriptBlock $script -ArgumentList $Port - } - else + if ($Foreground) { & $script -Port $Port } + else + { + $ps = [powershell]::Create() + $null = $ps.AddScript($script) + $null = $ps.AddParameter("port",$port) + $AsyncResponse = $ps.BeginInvoke() + # include the AsyncResponse in the return object + # it can be used to determine whether execution + # is still underway, and may be useful in debugging + # if something goes amiss + [pscustomobject]@{ + PowerShell = $ps + AsyncResponse = $AsyncResponse + } + } } }