Skip to content

Mark Task.wait() noasync and provide Task.get() #668

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
Feb 14, 2023
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
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()")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to just #if around the available attribute and avoid the duplicate impl, if you wanted to

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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