Skip to content

Commit 2d7ae2b

Browse files
weissiartemredkin
authored andcommitted
minor: improve code style (swift-server#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
privatefunc writeBody(request:HTTPClient.Request, context:ChannelHandlerContext)->EventLoopFuture<Void>{
554-
iflet body = request.body {
555-
return body.stream(HTTPClient.Body.StreamWriter{ part in
556-
letfuture= 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+
guardlet 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
publicfunc 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-
lethttpBin=HttpBin()
77+
lethttpBin=HTTPBin()
7878
lethttpClient=HTTPClient(eventLoopGroupProvider:.createNew)
7979
defer{
8080
XCTAssertNoThrow(try httpClient.syncShutdown())
81-
httpBin.shutdown()
81+
XCTAssertNoThrow(tryhttpBin.shutdown())
8282
}
8383

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

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

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

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

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

175175
letrequest=tryRequest(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-
internalclassHttpBin{
94+
internalfinalclassHTTPBin{
9495
letgroup=MultiThreadedEventLoopGroup(numberOfThreads:1)
9596
letserverChannel:Channel
97+
letisShutdown:Atomic<Bool>=.init(value:false)
9698

9799
varport:Int{
98100
returnInt(self.serverChannel.localAddress!.port!)
@@ -125,7 +127,7 @@ internal class HttpBin{
125127
}
126128
}.flatMap{
127129
if ssl {
128-
returnHttpBin.configureTLS(channel: channel).flatMap{
130+
returnHTTPBin.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+
tryself.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
switchself.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-
iflet promise =self.channelPromise {
315-
promise.succeed(context.channel)
316-
}
321+
self.channelPromise?.succeed(context.channel)
317322
ifself.resps.isEmpty {
318323
return
319324
}
320325
letresponse=self.resps.removeFirst()
321326
context.write(wrapOutboundOut(.head(response.head)), promise:nil)
322-
iflet body = response.body {
323-
letdata= body.withUnsafeReadableBytes{
324-
Data(bytes: $0.baseAddress!, count: $0.count)
325-
}
326-
327-
letserialized=try!JSONEncoder().encode(RequestInfo(data:String(data: data, encoding:.utf8)!))
327+
ifvar body = response.body {
328+
letdata= body.readData(length: body.readableBytes)!
329+
letserialized=try!JSONEncoder().encode(RequestInfo(data:String(decoding: data,
330+
as:Unicode.UTF8.self)))
328331

329332
varresponseBody= 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
switchself.unwrapInboundIn(data){
397400
case.head(let req):
398-
iflet promise =self.channelPromise {
399-
promise.succeed(context.channel)
400-
}
401+
self.channelPromise?.succeed(context.channel)
401402

402403
letresponse: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)