Skip to content

Add async shutdown method to HTTPClient #551

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 3 commits into from
Feb 7, 2022
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
34 changes: 34 additions & 0 deletions Sources/AsyncHTTPClient/AsyncAwait/HTTPClient+shutdown.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if compiler(>=5.5.2) && canImport(_Concurrency)

extension HTTPClient {
/// Shuts down the client and `EventLoopGroup` if it was created by the client.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func shutdown() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
self.shutdown { error in
switch error {
case .none:
continuation.resume()
case .some(let error):
continuation.resume(throwing: error)
}
}
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extension AsyncAwaitEndToEndTests {
("testImmediateDeadline", testImmediateDeadline),
("testInvalidURL", testInvalidURL),
("testRedirectChangesHostHeader", testRedirectChangesHostHeader),
("testShutdown", testShutdown),
]
}
}
13 changes: 13 additions & 0 deletions Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,19 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
}
#endif
}

func testShutdown() {
#if compiler(>=5.5.2) && canImport(_Concurrency)
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
XCTAsyncTest {
let client = makeDefaultHTTPClient()
try await client.shutdown()
await XCTAssertThrowsError(try await client.shutdown()) { error in
XCTAssertEqualTypeAndValue(error, HTTPClientError.alreadyShutdown)
}
}
#endif
}
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
Expand Down
4 changes: 2 additions & 2 deletions scripts/soundness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##
## This source file is part of the AsyncHTTPClient open source project
##
## Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors
## Copyright (c) 2018-2022 Apple Inc. and the AsyncHTTPClient project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
Expand All @@ -18,7 +18,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/'
sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/' -e 's/2022/YEARS/'
}

printf "=> Checking linux tests... "
Expand Down