Skip to content

Commit f3830d1

Browse files
authored
Add async shutdown method to HTTPClient (#551)
1 parent 7b3415c commit f3830d1

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#if compiler(>=5.5.2) && canImport(_Concurrency)
16+
17+
extension HTTPClient {
18+
/// Shuts down the client and `EventLoopGroup` if it was created by the client.
19+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
20+
func shutdown() async throws {
21+
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
22+
self.shutdown { error in
23+
switch error {
24+
case .none:
25+
continuation.resume()
26+
case .some(let error):
27+
continuation.resume(throwing: error)
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
#endif

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extension AsyncAwaitEndToEndTests {
4040
("testImmediateDeadline", testImmediateDeadline),
4141
("testInvalidURL", testInvalidURL),
4242
("testRedirectChangesHostHeader", testRedirectChangesHostHeader),
43+
("testShutdown", testShutdown),
4344
]
4445
}
4546
}

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,19 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
437437
}
438438
#endif
439439
}
440+
441+
func testShutdown() {
442+
#if compiler(>=5.5.2) && canImport(_Concurrency)
443+
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return }
444+
XCTAsyncTest {
445+
let client = makeDefaultHTTPClient()
446+
try await client.shutdown()
447+
await XCTAssertThrowsError(try await client.shutdown()) { error in
448+
XCTAssertEqualTypeAndValue(error, HTTPClientError.alreadyShutdown)
449+
}
450+
}
451+
#endif
452+
}
440453
}
441454

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

scripts/soundness.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
##
44
## This source file is part of the AsyncHTTPClient open source project
55
##
6-
## Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors
6+
## Copyright (c) 2018-2022 Apple Inc. and the AsyncHTTPClient project authors
77
## Licensed under Apache License v2.0
88
##
99
## See LICENSE.txt for license information
@@ -18,7 +18,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1818

1919
function replace_acceptable_years() {
2020
# this needs to replace all acceptable forms with 'YEARS'
21-
sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/'
21+
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/'
2222
}
2323

2424
printf "=> Checking linux tests... "

0 commit comments

Comments
 (0)