Skip to content

Commit

Permalink
Mark Task.wait() noasync and provide Task.get() (#668)
Browse files Browse the repository at this point in the history
Motivation

Task.wait() is a convenience method to avoid needing to wait for the
response future. This has had the effect of "laundering" a noasync
warning from EventLoopFuture.wait(), hiding it within a purely sync call
that may itself be used in an async context.

We should discourage using these and prefer using .get() instead.

Modifications

Mark Task.wait() noasync.
Add Task.get() for backward compatibility.

Result

Safer migration to Swift concurrency
  • Loading branch information
Lukasa authored Feb 14, 2023
1 parent e264599 commit 864c8d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,33 @@ extension HTTPClient {
return self.promise.futureResult
}

#if swift(>=5.7)
/// Waits for execution of this request to complete.
///
/// - returns: The value of the `EventLoopFuture` when it completes.
/// - throws: The error value of the `EventLoopFuture` if it errors.
/// - returns: The value of ``futureResult`` when it completes.
/// - throws: The error value of ``futureResult`` if it errors.
@available(*, noasync, message: "wait() can block indefinitely, prefer get()", renamed: "get()")
public func wait() throws -> Response {
return try self.promise.futureResult.wait()
}
#else
/// Waits for execution of this request to complete.
///
/// - returns: The value of ``futureResult`` when it completes.
/// - throws: The error value of ``futureResult`` if it errors.
public func wait() throws -> Response {
return try self.promise.futureResult.wait()
}
#endif

/// Provides the result of this request.
///
/// - returns: The value of ``futureResult`` when it completes.
/// - throws: The error value of ``futureResult`` if it errors.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func get() async throws -> Response {
return try await self.promise.futureResult.get()
}

/// Cancels the request execution.
public func cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extension AsyncAwaitEndToEndTests {
("testRejectsInvalidCharactersInHeaderFieldNames_http2", testRejectsInvalidCharactersInHeaderFieldNames_http2),
("testRejectsInvalidCharactersInHeaderFieldValues_http1", testRejectsInvalidCharactersInHeaderFieldValues_http1),
("testRejectsInvalidCharactersInHeaderFieldValues_http2", testRejectsInvalidCharactersInHeaderFieldValues_http2),
("testUsingGetMethodInsteadOfWait", testUsingGetMethodInsteadOfWait),
]
}
}
20 changes: 20 additions & 0 deletions Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,26 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
}
}
}

func testUsingGetMethodInsteadOfWait() {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
XCTAsyncTest {
let bin = HTTPBin(.http2(compress: false))
defer { XCTAssertNoThrow(try bin.shutdown()) }
let client = makeDefaultHTTPClient()
defer { XCTAssertNoThrow(try client.syncShutdown()) }
let request = try HTTPClient.Request(url: "https://localhost:\(bin.port)/get")

guard let response = await XCTAssertNoThrowWithResult(
try await client.execute(request: request).get()
) else {
return
}

XCTAssertEqual(response.status, .ok)
XCTAssertEqual(response.version, .http2)
}
}
}

extension AsyncSequence where Element == ByteBuffer {
Expand Down

0 comments on commit 864c8d9

Please sign in to comment.