Skip to content

Commit 8113707

Browse files
committed
Mark Task.wait() noasync and provide Task.get()
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
1 parent 9401037 commit 8113707

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Sources/AsyncHTTPClient/HTTPHandler.swift

+22-2
Original file line numberDiff line numberDiff line change
@@ -750,13 +750,33 @@ extension HTTPClient {
750750
return self.promise.futureResult
751751
}
752752

753+
#if swift(>=5.7)
753754
/// Waits for execution of this request to complete.
754755
///
755-
/// - returns: The value of the `EventLoopFuture` when it completes.
756-
/// - throws: The error value of the `EventLoopFuture` if it errors.
756+
/// - returns: The value of ``futureResult`` when it completes.
757+
/// - throws: The error value of ``futureResult`` if it errors.
758+
@available(*, noasync, message: "wait() can block indefinitely, prefer get()", renamed: "get()")
757759
public func wait() throws -> Response {
758760
return try self.promise.futureResult.wait()
759761
}
762+
#else
763+
/// Waits for execution of this request to complete.
764+
///
765+
/// - returns: The value of ``futureResult`` when it completes.
766+
/// - throws: The error value of ``futureResult`` if it errors.
767+
public func wait() throws -> Response {
768+
return try self.promise.futureResult.wait()
769+
}
770+
#endif
771+
772+
/// Provides the result of this request.
773+
///
774+
/// - returns: The value of ``futureResult`` when it completes.
775+
/// - throws: The error value of ``futureResult`` if it errors.
776+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
777+
public func get() async throws -> Response {
778+
return try await self.promise.futureResult.get()
779+
}
760780

761781
/// Cancels the request execution.
762782
public func cancel() {

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift

+20
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,26 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
725725
}
726726
}
727727
}
728+
729+
func testUsingGetMethodInsteadOfWait() {
730+
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
731+
XCTAsyncTest {
732+
let bin = HTTPBin(.http2(compress: false))
733+
defer { XCTAssertNoThrow(try bin.shutdown()) }
734+
let client = makeDefaultHTTPClient()
735+
defer { XCTAssertNoThrow(try client.syncShutdown()) }
736+
let request = try HTTPClient.Request(url: "https://localhost:\(bin.port)/get")
737+
738+
guard let response = await XCTAssertNoThrowWithResult(
739+
try await client.execute(request: request).get()
740+
) else {
741+
return
742+
}
743+
744+
XCTAssertEqual(response.status, .ok)
745+
XCTAssertEqual(response.version, .http2)
746+
}
747+
}
728748
}
729749

730750
extension AsyncSequence where Element == ByteBuffer {

0 commit comments

Comments
 (0)