Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit ed93821

Browse files
Fix for #198 start-polaris throwing error (#200)
* Fix for #198 start-polaris throwing error * Getting rid of the odd beforeAll * Server shouldn't be stopped * Restored accidentally removed test
1 parent 934d9a6 commit ed93821

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

Public/Start-Polaris.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function Start-Polaris {
7474
)
7575

7676
if ( -not $Polaris) {
77+
Write-Verbose 'No Polaris found. Creating new.'
7778
CreateNewPolarisIfNeeded
7879
$Polaris = $Script:Polaris
7980
}
@@ -82,6 +83,11 @@ function Start-Polaris {
8283
Use-PolarisJsonBodyParserMiddleware -Polaris $Polaris
8384
}
8485

86+
Write-Verbose "Starting polaris listening"
87+
Write-Verbose " Port: $Port"
88+
Write-Verbose " Https.IsPreset: $($Https.IsPresent)"
89+
Write-Verbose " Auth: $Auth"
90+
Write-Verbose " HostName: $HostName"
8591
$Polaris.Start( $Port, $Https.IsPresent, $Auth, $HostName)
8692

8793
return $Polaris

Tests/unit/PolarisServer.Tests.ps1

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,30 @@ Import-Module -Name $PSScriptRoot\..\..\Polaris.psd1
77

88
Describe "Test webserver use" {
99

10-
BeforeAll {
11-
12-
$IsUnix = $PSVersionTable.Platform -eq "Unix"
10+
Context "Test starting and stopping of the server" {
1311

14-
$Port = Get-Random -Minimum 8000 -Maximum 8999
12+
It "Should allow starting and stopping the server" {
13+
$Port = Get-Random -Minimum 8000 -Maximum 8999
14+
$Polaris = Start-Polaris -Port $Port
15+
Stop-Polaris
16+
$Polaris.Listener.IsListening | Should Be $false
1517

16-
# Start the app
17-
$Polaris = Start-Polaris -Port $Port -MinRunspaces 1 -MaxRunspaces 5 # all params are optional
18+
$Polaris = Start-Polaris -Port $Port
19+
$Polaris.Listener.IsListening | Should be $true
20+
}
1821

19-
}
22+
It "Should allow running Start-Polaris multiple times without error" {
23+
$Port = Get-Random -Minimum 8000 -Maximum 8999
24+
$Polaris = Start-Polaris -Port $Port
25+
$Polaris.Listener.IsListening | Should Be $true
2026

21-
Context "Test starting and stopping of the server" {
22-
BeforeAll {
23-
if (-not $IsUnix) {
24-
Stop-Polaris
25-
$Polaris = Get-Polaris
26-
$Polaris.Listener.IsListening | Should Be $false
27-
28-
$Polaris = Start-Polaris -Port 9998
29-
$Polaris.Listener.IsListening | Should be $true
30-
}
27+
$Polaris = Start-Polaris -Port $Port
28+
$Polaris.Listener.IsListening | Should be $true
3129
}
3230

3331
It "Allows a custom logger" {
34-
$Polaris = Get-Polaris
32+
$Port = Get-Random -Minimum 8000 -Maximum 8999
33+
$Polaris = Start-Polaris -Port $Port
3534
$Polaris.Logger = {
3635
param($Word)
3736
$Word | Out-File "TestDrive:\test.log" -NoNewline

lib/Polaris.Class.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,30 @@ class Polaris {
234234
[string]$HostName
235235
) {
236236
$this.StopServer = $false
237-
$this.InitListener($Port, $Https, $Auth, $HostName)
238-
$this.Listener.BeginGetContext($this.ContextHandler, $this)
237+
if($this.Listener -eq $null) {
238+
Write-Debug "Creating new listener"
239+
$this.InitListener($Port, $Https, $Auth, $HostName)
240+
$this.Listener.BeginGetContext($this.ContextHandler, $this)
241+
} else {
242+
Write-Debug "Listener object already created"
243+
if($this.Listener.IsListening){
244+
Write-Debug "Listener is already in a listening state. Doing nothing"
245+
} else {
246+
Write-Debug "Listener object exists but has shut down. Reinitializing..."
247+
$this.Stop()
248+
$this.InitListener($Port, $Https, $Auth, $HostName)
249+
$this.Listener.BeginGetContext($this.ContextHandler, $this)
250+
}
251+
}
239252
}
240253

241254
[void] Stop () {
242255
$this.StopServer = $true
243256
$this.Listener.Close()
244257
$this.Listener.Dispose()
245258
$this.Log("Server Stopped.")
246-
247259
}
260+
248261
[void] InitListener (
249262
[int]$Port,
250263
[bool]$Https,

0 commit comments

Comments
 (0)