Skip to content

Commit 6fc4771

Browse files
author
Johannes Weiss
committed
add a future-returning shutdown method
1 parent 8994e1f commit 6fc4771

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/AsyncHTTPClient/AsyncAwait/HTTPClient+shutdown.swift

+21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import NIOCore
16+
17+
extension HTTPClient {
18+
public func shutdown() -> EventLoopFuture<Void> {
19+
switch self.eventLoopGroupProvider {
20+
case .shared(let group):
21+
let promise = group.any().makePromise(of: Void.self)
22+
self.shutdown(queue: .global()) { error in
23+
if let error = error {
24+
promise.fail(error)
25+
} else {
26+
promise.succeed(())
27+
}
28+
}
29+
return promise.futureResult
30+
case .createNew:
31+
preconditionFailure("Cannot use the shutdown() method which returns a future when owning the EventLoopGroup. Please use the one of the other shutdown methods.")
32+
}
33+
}
34+
}
35+
1536
#if compiler(>=5.5.2) && canImport(_Concurrency)
1637

1738
extension HTTPClient {

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -3463,4 +3463,9 @@ class HTTPClientTests: XCTestCase {
34633463
XCTAssertEqual(response?.version, .http1_1)
34643464
XCTAssertEqual(response?.body?.readableBytes, 10_000)
34653465
}
3466+
3467+
func testShutdownWithFutures() {
3468+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
3469+
XCTAssertNoThrow(try httpClient.shutdown().wait())
3470+
}
34663471
}

0 commit comments

Comments
 (0)