Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion FlyingFox/Sources/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public final actor HTTPServer {
handlers.appendRoute(route, handler: handler)
}

public func start() async throws {
public func run() async throws {
guard state == nil else {
logger.logCritical("server error: already started")
throw SocketError.unsupportedAddress
Expand All @@ -109,6 +109,11 @@ public final actor HTTPServer {
}
}

@available(*, deprecated, renamed: "run")
public func start() async throws {
try await run()
}

func preparePoolAndSocket() async throws -> Socket {
do {
try await config.pool.prepare()
Expand Down
2 changes: 1 addition & 1 deletion FlyingFox/Tests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct HTTPClientTests {
func client_sends_request() async throws {
// given
let server = HTTPServer(address: .loopback(port: 0))
let task = Task { try await server.start() }
let task = Task { try await server.run() }
defer { task.cancel() }
let client = _HTTPClient()

Expand Down
12 changes: 6 additions & 6 deletions FlyingFox/Tests/HTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ actor HTTPServerTests {
self.stopServer = server
Task {
try await HTTPServer.$preferConnectionsDiscarding.withValue(preferConnectionsDiscarding) {
try await server.start()
try await server.run()
}
}
return try await server.waitForListeningPort()
Expand All @@ -54,7 +54,7 @@ actor HTTPServerTests {
@discardableResult
func startServer(_ server: HTTPServer) async throws -> Task<Void, any Error> {
self.stopServer = server
let task = Task { try await server.start() }
let task = Task { try await server.run() }
try await server.waitUntilListening()
return task
}
Expand All @@ -69,7 +69,7 @@ actor HTTPServerTests {
try await startServer(server)

await #expect(throws: SocketError.unsupportedAddress) {
try await server.start()
try await server.run()
}
}

Expand All @@ -93,9 +93,9 @@ actor HTTPServerTests {
defer { try! socket.close() }

await #expect(throws: SocketError.self) {
try await server.start()
try await server.run()
}
// await AsyncAssertThrowsError(try await server.start(), of: SocketError.self) {
// await AsyncAssertThrowsError(try await server.run(), of: SocketError.self) {
// XCTAssertTrue(
// $0.errorDescription?.contains("Address already in use") == true
// )
Expand Down Expand Up @@ -430,7 +430,7 @@ actor HTTPServerTests {
return true
}

Task { try await server.start() }
Task { try await server.run() }
self.stopServer = server

#expect(try await waiting.value == true)
Expand Down
2 changes: 1 addition & 1 deletion FlyingFox/XCTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class HTTPClientTests: XCTestCase {
func testClient() async throws {
// given
let server = HTTPServer(address: .loopback(port: 0))
let task = Task { try await server.start() }
let task = Task { try await server.run() }
defer { task.cancel() }
let client = _HTTPClient()

Expand Down
10 changes: 5 additions & 5 deletions FlyingFox/XCTests/HTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class HTTPServerTests: XCTestCase {
self.stopServer = server
Task {
try await HTTPServer.$preferConnectionsDiscarding.withValue(preferConnectionsDiscarding) {
try await server.start()
try await server.run()
}
}
return try await server.waitForListeningPort()
Expand All @@ -54,7 +54,7 @@ final class HTTPServerTests: XCTestCase {
@discardableResult
func startServer(_ server: HTTPServer) async throws -> Task<Void, any Error> {
self.stopServer = server
let task = Task { try await server.start() }
let task = Task { try await server.run() }
try await server.waitUntilListening()
return task
}
Expand All @@ -67,7 +67,7 @@ final class HTTPServerTests: XCTestCase {
let server = HTTPServer.make()
try await startServer(server)

await AsyncAssertThrowsError(try await server.start(), of: SocketError.self) {
await AsyncAssertThrowsError(try await server.run(), of: SocketError.self) {
XCTAssertEqual($0, .unsupportedAddress)
}
}
Expand All @@ -89,7 +89,7 @@ final class HTTPServerTests: XCTestCase {
let socket = try await server.makeSocketAndListen()
defer { try! socket.close() }

await AsyncAssertThrowsError(try await server.start(), of: SocketError.self) {
await AsyncAssertThrowsError(try await server.run(), of: SocketError.self) {
XCTAssertTrue(
$0.errorDescription?.contains("Address already in use") == true
)
Expand Down Expand Up @@ -405,7 +405,7 @@ final class HTTPServerTests: XCTestCase {
return true
}

Task { try await server.start() }
Task { try await server.run() }
self.stopServer = server

await AsyncAssertEqual(try await waiting.value, true)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ Start the server by providing a port number:
import FlyingFox

let server = HTTPServer(port: 80)
try await server.start()
try await server.run()
```

The server runs within the the current task. To stop the server, cancel the task terminating all connections immediatley:

```swift
let task = Task { try await server.start() }
let task = Task { try await server.run() }
task.cancel()
```

Expand All @@ -72,7 +72,7 @@ Retrieve the current listening address:
await server.listeningAddress
```

> Note: iOS will hangup the listening socket when an app is suspended in the background. Once the app returns to the foreground, `HTTPServer.start()` detects this, throwing `SocketError.disconnected`. The server must then be started once more.
> Note: iOS will hangup the listening socket when an app is suspended in the background. Once the app returns to the foreground, `HTTPServer.run()` detects this, throwing `SocketError.disconnected`. The server must then be started once more.

## Handlers

Expand Down Expand Up @@ -360,7 +360,7 @@ struct MyHandler {
}

let server = HTTPServer(port: 80, handler: MyHandler())
try await server.start()
try await server.run()
```

The annotations are implemented via [SE-0389 Attached Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md).
Expand Down