Skip to content

Commit 2d7ae2b

Browse files
weissiartemredkin
authored andcommitted
minor: improve code style (#103)
1 parent 8f9023c commit 2d7ae2b

File tree

5 files changed

+77
-78
lines changed

5 files changed

+77
-78
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let package = Package(
3131
),
3232
.testTarget(
3333
name: "AsyncHTTPClientTests",
34-
dependencies: ["AsyncHTTPClient", "NIOFoundationCompat"]
34+
dependencies: ["NIO", "NIOConcurrencyHelpers", "NIOSSL", "AsyncHTTPClient", "NIOFoundationCompat"]
3535
),
3636
]
3737
)

Sources/AsyncHTTPClient/HTTPHandler.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,17 +551,15 @@ internal class TaskHandler<Delegate: HTTPClientResponseDelegate>: ChannelInbound
551551
}
552552

553553
private func writeBody(request: HTTPClient.Request, context: ChannelHandlerContext) -> EventLoopFuture<Void> {
554-
if let body = request.body {
555-
return body.stream(HTTPClient.Body.StreamWriter { part in
556-
let future = context.writeAndFlush(self.wrapOutboundOut(.body(part)))
557-
future.whenSuccess { _ in
558-
self.delegate.didSendRequestPart(task: self.task, part)
559-
}
560-
return future
561-
})
562-
} else {
554+
guard let body = request.body else {
563555
return context.eventLoop.makeSucceededFuture(())
564556
}
557+
558+
return body.stream(HTTPClient.Body.StreamWriter { part in
559+
context.writeAndFlush(self.wrapOutboundOut(.body(part))).map {
560+
self.delegate.didSendRequestPart(task: self.task, part)
561+
}
562+
})
565563
}
566564

567565
public func read(context: ChannelHandlerContext) {

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ class HTTPClientInternalTests: XCTestCase {
7474
}
7575

7676
func testProxyStreaming() throws {
77-
let httpBin = HttpBin()
77+
let httpBin = HTTPBin()
7878
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
7979
defer {
8080
XCTAssertNoThrow(try httpClient.syncShutdown())
81-
httpBin.shutdown()
81+
XCTAssertNoThrow(try httpBin.shutdown())
8282
}
8383

8484
let body: HTTPClient.Body = .stream(length: 50) { writer in
@@ -104,11 +104,11 @@ class HTTPClientInternalTests: XCTestCase {
104104
}
105105

106106
func testProxyStreamingFailure() throws {
107-
let httpBin = HttpBin()
107+
let httpBin = HTTPBin()
108108
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
109109
defer {
110110
XCTAssertNoThrow(try httpClient.syncShutdown())
111-
httpBin.shutdown()
111+
XCTAssertNoThrow(try httpBin.shutdown())
112112
}
113113

114114
var body: HTTPClient.Body = .stream(length: 50) { _ in
@@ -165,11 +165,11 @@ class HTTPClientInternalTests: XCTestCase {
165165

166166
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
167167
let promise: EventLoopPromise<Channel> = httpClient.eventLoopGroup.next().makePromise()
168-
let httpBin = HttpBin(channelPromise: promise)
168+
let httpBin = HTTPBin(channelPromise: promise)
169169

170170
defer {
171171
XCTAssertNoThrow(try httpClient.syncShutdown())
172-
httpBin.shutdown()
172+
XCTAssertNoThrow(try httpBin.shutdown())
173173
}
174174

175175
let request = try Request(url: "http://localhost:\(httpBin.port)/custom")

Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import AsyncHTTPClient
1616
import Foundation
1717
import NIO
18+
import NIOConcurrencyHelpers
1819
import NIOHTTP1
1920
import NIOSSL
2021

@@ -90,9 +91,10 @@ internal final class RecordingHandler<Input, Output>: ChannelDuplexHandler {
9091
}
9192
}
9293

93-
internal class HttpBin {
94+
internal final class HTTPBin {
9495
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
9596
let serverChannel: Channel
97+
let isShutdown: Atomic<Bool> = .init(value: false)
9698

9799
var port: Int {
98100
return Int(self.serverChannel.localAddress!.port!)
@@ -125,7 +127,7 @@ internal class HttpBin {
125127
}
126128
}.flatMap {
127129
if ssl {
128-
return HttpBin.configureTLS(channel: channel).flatMap {
130+
return HTTPBin.configureTLS(channel: channel).flatMap {
129131
channel.pipeline.addHandler(HttpBinHandler(channelPromise: channelPromise))
130132
}
131133
} else {
@@ -135,8 +137,13 @@ internal class HttpBin {
135137
}.bind(host: "127.0.0.1", port: 0).wait()
136138
}
137139

138-
func shutdown() {
139-
try! self.group.syncShutdownGracefully()
140+
func shutdown() throws {
141+
self.isShutdown.store(true)
142+
try self.group.syncShutdownGracefully()
143+
}
144+
145+
deinit {
146+
assert(self.isShutdown.load(), "HTTPBin not shutdown before deinit")
140147
}
141148
}
142149

@@ -186,7 +193,7 @@ final class HTTPProxySimulator: ChannelInboundHandler, RemovableChannelHandler {
186193

187194
switch self.option {
188195
case .tls:
189-
_ = HttpBin.configureTLS(channel: context.channel)
196+
_ = HTTPBin.configureTLS(channel: context.channel)
190197
case .plaintext: break
191198
}
192199
}
@@ -311,20 +318,16 @@ internal final class HttpBinHandler: ChannelInboundHandler {
311318
response.add(body)
312319
self.resps.prepend(response)
313320
case .end:
314-
if let promise = self.channelPromise {
315-
promise.succeed(context.channel)
316-
}
321+
self.channelPromise?.succeed(context.channel)
317322
if self.resps.isEmpty {
318323
return
319324
}
320325
let response = self.resps.removeFirst()
321326
context.write(wrapOutboundOut(.head(response.head)), promise: nil)
322-
if let body = response.body {
323-
let data = body.withUnsafeReadableBytes {
324-
Data(bytes: $0.baseAddress!, count: $0.count)
325-
}
326-
327-
let serialized = try! JSONEncoder().encode(RequestInfo(data: String(data: data, encoding: .utf8)!))
327+
if var body = response.body {
328+
let data = body.readData(length: body.readableBytes)!
329+
let serialized = try! JSONEncoder().encode(RequestInfo(data: String(decoding: data,
330+
as: Unicode.UTF8.self)))
328331

329332
var responseBody = context.channel.allocator.buffer(capacity: serialized.count)
330333
responseBody.writeBytes(serialized)
@@ -395,9 +398,7 @@ internal final class HttpBinForSSLUncleanShutdownHandler: ChannelInboundHandler
395398
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
396399
switch self.unwrapInboundIn(data) {
397400
case .head(let req):
398-
if let promise = self.channelPromise {
399-
promise.succeed(context.channel)
400-
}
401+
self.channelPromise?.succeed(context.channel)
401402

402403
let response: String?
403404
switch req.uri {
@@ -440,7 +441,7 @@ internal final class HttpBinForSSLUncleanShutdownHandler: ChannelInboundHandler
440441
context.writeAndFlush(self.wrapOutboundOut(buffer), promise: nil)
441442
}
442443

443-
_ = context.channel.pipeline.removeHandler(name: "NIOSSLServerHandler").map { _ in
444+
context.channel.pipeline.removeHandler(name: "NIOSSLServerHandler").whenSuccess {
444445
context.close(promise: nil)
445446
}
446447
case .body:

0 commit comments

Comments
 (0)