Skip to content

TestFoundation: stop internal http server on test case tear down #2958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Tests/Foundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class _TCPSocket {
}
}

deinit {
func closeSocket() throws {
guard _socket != nil else { return }
#if os(Windows)
if listening { shutdown(_socket, SD_BOTH) }
Expand All @@ -248,6 +248,11 @@ class _TCPSocket {
if listening { shutdown(_socket, CInt(SHUT_RDWR)) }
close(_socket)
#endif
_socket = nil
}

deinit {
try? closeSocket()
}
}

Expand Down Expand Up @@ -311,6 +316,10 @@ class _HTTPServer {
return _HTTPServer(socket: connection)
}

public func stop() throws {
try tcpSocket.closeSocket()
}

public func request() throws -> _HTTPRequest {

var reader = _SocketDataReader(socket: tcpSocket)
Expand Down Expand Up @@ -960,7 +969,9 @@ class LoopbackServerTest : XCTestCase {
try? subServer.readAndRespond()
}
} catch {
NSLog("httpServer: \(error)")
if (serverActive) { // Ignore errors thrown on shutdown
NSLog("httpServer: \(error)")
}
}
}
serverPort = -2
Expand All @@ -984,6 +995,7 @@ class LoopbackServerTest : XCTestCase {

override class func tearDown() {
serverActive = false
try? testServer?.stop()
super.tearDown()
}
}