mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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.
This commit is contained in:
committed by
Travis Plunk
parent
7a51b446e5
commit
bc807322ed
@@ -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" {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user