Skip to content

Commit 1118370

Browse files
authored
Apply swift-format changes and CI (#472)
Motivation: Standardised formatting makes the project cleaner to look at and work on. Modifications: Setup swift-format with the same settings as Swift-NIO Result: Common formatting throughout
1 parent 9358611 commit 1118370

File tree

125 files changed

+21973
-11561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+21973
-11561
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
1111
with:
1212
license_header_check_project_name: "SwiftNIO"
13-
format_check_enabled: false
13+
format_check_enabled: true
1414
shell_check_enabled: false
1515

1616
unit-tests:

.swift-format

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"version" : 1,
3+
"indentation" : {
4+
"spaces" : 4
5+
},
6+
"tabWidth" : 4,
7+
"fileScopedDeclarationPrivacy" : {
8+
"accessLevel" : "private"
9+
},
10+
"spacesAroundRangeFormationOperators" : false,
11+
"indentConditionalCompilationBlocks" : false,
12+
"indentSwitchCaseLabels" : false,
13+
"lineBreakAroundMultilineExpressionChainComponents" : false,
14+
"lineBreakBeforeControlFlowKeywords" : false,
15+
"lineBreakBeforeEachArgument" : true,
16+
"lineBreakBeforeEachGenericRequirement" : true,
17+
"lineLength" : 120,
18+
"maximumBlankLines" : 1,
19+
"respectsExistingLineBreaks" : true,
20+
"prioritizeKeepingFunctionOutputTogether" : true,
21+
"rules" : {
22+
"AllPublicDeclarationsHaveDocumentation" : false,
23+
"AlwaysUseLiteralForEmptyCollectionInit" : false,
24+
"AlwaysUseLowerCamelCase" : false,
25+
"AmbiguousTrailingClosureOverload" : true,
26+
"BeginDocumentationCommentWithOneLineSummary" : false,
27+
"DoNotUseSemicolons" : true,
28+
"DontRepeatTypeInStaticProperties" : true,
29+
"FileScopedDeclarationPrivacy" : true,
30+
"FullyIndirectEnum" : true,
31+
"GroupNumericLiterals" : true,
32+
"IdentifiersMustBeASCII" : true,
33+
"NeverForceUnwrap" : false,
34+
"NeverUseForceTry" : false,
35+
"NeverUseImplicitlyUnwrappedOptionals" : false,
36+
"NoAccessLevelOnExtensionDeclaration" : true,
37+
"NoAssignmentInExpressions" : true,
38+
"NoBlockComments" : true,
39+
"NoCasesWithOnlyFallthrough" : true,
40+
"NoEmptyTrailingClosureParentheses" : true,
41+
"NoLabelsInCasePatterns" : true,
42+
"NoLeadingUnderscores" : false,
43+
"NoParensAroundConditions" : true,
44+
"NoVoidReturnOnFunctionSignature" : true,
45+
"OmitExplicitReturns" : true,
46+
"OneCasePerLine" : true,
47+
"OneVariableDeclarationPerLine" : true,
48+
"OnlyOneTrailingClosureArgument" : true,
49+
"OrderedImports" : true,
50+
"ReplaceForEachWithForLoop" : true,
51+
"ReturnVoidInsteadOfEmptyTuple" : true,
52+
"UseEarlyExits" : false,
53+
"UseExplicitNilCheckInConditions" : false,
54+
"UseLetInEveryBoundCaseVariable" : false,
55+
"UseShorthandTypeNames" : true,
56+
"UseSingleLinePropertyGetter" : false,
57+
"UseSynthesizedInitializer" : false,
58+
"UseTripleSlashForDocumentationComments" : true,
59+
"UseWhereClausesInForLoops" : false,
60+
"ValidateDocumentationComments" : false
61+
}
62+
}

FuzzTesting/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ let package = Package(
3030
.product(name: "NIOCore", package: "swift-nio"),
3131
.product(name: "NIOEmbedded", package: "swift-nio"),
3232
]
33-
),
33+
)
3434
]
3535
)

FuzzTesting/Sources/FuzzHTTP2/main.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@
1919
// See LICENSE.txt for license information:
2020
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
2121

22-
2322
import Foundation
24-
2523
import NIOCore
2624
import NIOEmbedded
2725
import NIOHTTP1
2826
import NIOHTTP2
2927

30-
fileprivate func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
28+
private func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
3129
let channel = EmbeddedChannel()
3230
defer {
3331
_ = try? channel.finish()
3432
}
3533
_ = try channel.configureHTTP2Pipeline(
3634
mode: .server,
37-
initialLocalSettings: [HTTP2Setting(parameter: .maxConcurrentStreams, value: 1<<23)],
35+
initialLocalSettings: [HTTP2Setting(parameter: .maxConcurrentStreams, value: 1 << 23)],
3836
inboundStreamInitializer: nil
3937
).wait()
4038
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/foo")).wait()
@@ -48,12 +46,12 @@ fileprivate func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
4846

4947
@_cdecl("LLVMFuzzerTestOneInput")
5048
public func FuzzServer(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
51-
let bytes = UnsafeRawBufferPointer(start: start, count: count)
52-
do {
53-
let _ = try fuzzInput(bytes)
54-
} catch {
55-
// Errors parsing are to be expected since not all inputs will be well formed.
56-
}
49+
let bytes = UnsafeRawBufferPointer(start: start, count: count)
50+
do {
51+
let _ = try fuzzInput(bytes)
52+
} catch {
53+
// Errors parsing are to be expected since not all inputs will be well formed.
54+
}
5755

58-
return 0
56+
return 0
5957
}

IntegrationTests/tests_01_allocation_counters/test_01_resources/shared.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@ import NIOHTTP2
1818

1919
/// Test use only. Allows abstracting over the two multiplexer implementations to write common testing code
2020
internal protocol MultiplexerChannelCreator {
21-
func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer)
21+
func createStreamChannel(
22+
promise: EventLoopPromise<Channel>?,
23+
_ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer
24+
)
2225
func createStreamChannel(_ initializer: @escaping NIOChannelInitializer) -> EventLoopFuture<Channel>
2326
}
2427

25-
extension HTTP2StreamMultiplexer: MultiplexerChannelCreator { }
26-
extension NIOHTTP2Handler.StreamMultiplexer: MultiplexerChannelCreator { }
28+
extension HTTP2StreamMultiplexer: MultiplexerChannelCreator {}
29+
extension NIOHTTP2Handler.StreamMultiplexer: MultiplexerChannelCreator {}
2730

2831
/// Have two `EmbeddedChannel` objects send and receive data from each other until
2932
/// they make no forward progress.
3033
func interactInMemory(_ first: EmbeddedChannel, _ second: EmbeddedChannel) throws {
31-
precondition(first.eventLoop === second.eventLoop, "interactInMemory assumes both channels are on the same event loop.")
34+
precondition(
35+
first.eventLoop === second.eventLoop,
36+
"interactInMemory assumes both channels are on the same event loop."
37+
)
3238
var operated: Bool
3339

3440
func readBytesFromChannel(_ channel: EmbeddedChannel) throws -> ByteBuffer? {
35-
return try channel.readOutbound(as: ByteBuffer.self)
41+
try channel.readOutbound(as: ByteBuffer.self)
3642
}
3743

3844
repeat {

IntegrationTests/tests_01_allocation_counters/test_01_resources/test_1k_requests.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ struct ServerOnly1KRequestsBenchmark {
5252
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
5353
headers.add(name: ":path", value: "/", indexing: .indexable)
5454
headers.add(name: ":scheme", value: "https", indexing: .indexable)
55-
headers.add(name: "user-agent",
56-
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
57-
indexing: .nonIndexable)
55+
headers.add(
56+
name: "user-agent",
57+
value:
58+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
59+
indexing: .nonIndexable
60+
)
5861
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
5962

6063
var hpackEncoder = HPACKEncoder(allocator: .init())
@@ -108,7 +111,7 @@ struct ServerOnly1KRequestsBenchmark {
108111
return settingsCopy
109112
}
110113

111-
init(concurrentStreams: Int, pipelineConfigurator: (Channel) throws -> ()) throws {
114+
init(concurrentStreams: Int, pipelineConfigurator: (Channel) throws -> Void) throws {
112115
self.concurrentStreams = concurrentStreams
113116

114117
self.channel = EmbeddedChannel()
@@ -122,7 +125,7 @@ struct ServerOnly1KRequestsBenchmark {
122125
initialBytes.writeImmutableBuffer(self.settingsACK)
123126

124127
try self.channel.writeInbound(initialBytes)
125-
while try self.channel.readOutbound(as: ByteBuffer.self) != nil { }
128+
while try self.channel.readOutbound(as: ByteBuffer.self) != nil {}
126129
}
127130

128131
func tearDown() {
@@ -142,15 +145,15 @@ struct ServerOnly1KRequestsBenchmark {
142145
private mutating func sendInterleavedRequests(_ interleavedRequests: Int) throws -> Int {
143146
var streamID = self.streamID
144147

145-
for _ in 0 ..< interleavedRequests {
148+
for _ in 0..<interleavedRequests {
146149
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
147150
try self.channel.writeInbound(self.headersFrame)
148151
streamID = streamID.advanced(by: 2)
149152
}
150153

151154
streamID = self.streamID
152155

153-
for _ in 0 ..< interleavedRequests {
156+
for _ in 0..<interleavedRequests {
154157
self.dataFrame.setInteger(UInt32(Int32(streamID)), at: self.dataFrame.readerIndex + 5)
155158
try self.channel.writeInbound(self.dataFrame)
156159
streamID = streamID.advanced(by: 2)
@@ -169,7 +172,7 @@ struct ServerOnly1KRequestsBenchmark {
169172
}
170173
}
171174

172-
fileprivate class TestServer: ChannelInboundHandler {
175+
private class TestServer: ChannelInboundHandler {
173176
public typealias InboundIn = HTTP2Frame.FramePayload
174177
public typealias OutboundOut = HTTP2Frame.FramePayload
175178

@@ -198,50 +201,58 @@ fileprivate class TestServer: ChannelInboundHandler {
198201
func run(identifier: String) {
199202
var interleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 100) { channel in
200203
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
201-
return streamChannel.pipeline.addHandler(TestServer())
204+
streamChannel.pipeline.addHandler(TestServer())
202205
}.wait()
203206
}
204207

205208
measure(identifier: identifier + "_interleaved") {
206-
return try! interleaved.run()
209+
try! interleaved.run()
207210
}
208211

209212
interleaved.tearDown()
210213

211214
var noninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1) { channel in
212215
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
213-
return streamChannel.pipeline.addHandler(TestServer())
216+
streamChannel.pipeline.addHandler(TestServer())
214217
}.wait()
215218
}
216219

217220
measure(identifier: identifier + "_noninterleaved") {
218-
return try! noninterleaved.run()
221+
try! noninterleaved.run()
219222
}
220223

221224
noninterleaved.tearDown()
222225

223226
//
224227
// MARK: - Inline HTTP2 multiplexer tests
225228
var inlineInterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 100) { channel in
226-
_ = try channel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { streamChannel -> EventLoopFuture<Void> in
227-
return streamChannel.pipeline.addHandler(TestServer())
229+
_ = try channel.configureHTTP2Pipeline(
230+
mode: .server,
231+
connectionConfiguration: .init(),
232+
streamConfiguration: .init()
233+
) { streamChannel -> EventLoopFuture<Void> in
234+
streamChannel.pipeline.addHandler(TestServer())
228235
}.wait()
229236
}
230237

231238
measure(identifier: identifier + "_inline_interleaved") {
232-
return try! inlineInterleaved.run()
239+
try! inlineInterleaved.run()
233240
}
234241

235242
inlineInterleaved.tearDown()
236243

237244
var inlineNoninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1) { channel in
238-
_ = try channel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { streamChannel -> EventLoopFuture<Void> in
239-
return streamChannel.pipeline.addHandler(TestServer())
245+
_ = try channel.configureHTTP2Pipeline(
246+
mode: .server,
247+
connectionConfiguration: .init(),
248+
streamConfiguration: .init()
249+
) { streamChannel -> EventLoopFuture<Void> in
250+
streamChannel.pipeline.addHandler(TestServer())
240251
}.wait()
241252
}
242253

243254
measure(identifier: identifier + "_inline_noninterleaved") {
244-
return try! inlineNoninterleaved.run()
255+
try! inlineNoninterleaved.run()
245256
}
246257

247258
inlineNoninterleaved.tearDown()

IntegrationTests/tests_01_allocation_counters/test_01_resources/test_client_server_h1_request_response.swift

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,58 @@ final class ServerHandler: ChannelInboundHandler {
3939
}
4040
}
4141

42-
4342
final class ClientHandler: ChannelInboundHandler {
4443
typealias InboundIn = HTTPClientResponsePart
4544
typealias OutboundOut = HTTPClientRequestPart
4645

4746
func channelActive(context: ChannelHandlerContext) {
4847
// Send a request.
49-
let head = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .GET, uri: "/", headers: HTTPHeaders([("host", "localhost")]))
48+
let head = HTTPRequestHead(
49+
version: .init(major: 2, minor: 0),
50+
method: .GET,
51+
uri: "/",
52+
headers: HTTPHeaders([("host", "localhost")])
53+
)
5054
context.write(self.wrapOutboundOut(.head(head)), promise: nil)
5155
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
5256
}
5357
}
5458

5559
func run(identifier: String) {
5660
testRun(identifier: identifier) { clientChannel in
57-
return try! clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait()
61+
try! clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait()
5862
} serverPipelineConfigurator: { serverChannel in
5963
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server) { channel in
60-
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
64+
channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
6165
}.wait()
6266
}
6367

6468
//
6569
// MARK: - Inline HTTP2 multiplexer tests
6670
testRun(identifier: identifier + "_inline") { clientChannel in
67-
return try! clientChannel.configureHTTP2Pipeline(mode: .client, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
68-
return channel.eventLoop.makeSucceededVoidFuture()
71+
try! clientChannel.configureHTTP2Pipeline(
72+
mode: .client,
73+
connectionConfiguration: .init(),
74+
streamConfiguration: .init()
75+
) { channel in
76+
channel.eventLoop.makeSucceededVoidFuture()
6977
}.wait()
7078
} serverPipelineConfigurator: { serverChannel in
71-
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
72-
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
79+
_ = try! serverChannel.configureHTTP2Pipeline(
80+
mode: .server,
81+
connectionConfiguration: .init(),
82+
streamConfiguration: .init()
83+
) { channel in
84+
channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
7385
}.wait()
7486
}
7587
}
7688

77-
private func testRun(identifier: String, clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator, serverPipelineConfigurator: (Channel) throws -> ()) {
89+
private func testRun(
90+
identifier: String,
91+
clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator,
92+
serverPipelineConfigurator: (Channel) throws -> Void
93+
) {
7894
let loop = EmbeddedEventLoop()
7995

8096
measure(identifier: identifier) {
@@ -91,7 +107,9 @@ private func testRun(identifier: String, clientPipelineConfigurator: (Channel) t
91107

92108
let promise = clientChannel.eventLoop.makePromise(of: Channel.self)
93109
clientMultiplexer.createStreamChannel(promise: promise) { channel in
94-
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https), ClientHandler()])
110+
channel.pipeline.addHandlers([
111+
HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https), ClientHandler(),
112+
])
95113
}
96114
clientChannel.embeddedEventLoop.run()
97115
let child = try! promise.futureResult.wait()
@@ -108,4 +126,3 @@ private func testRun(identifier: String, clientPipelineConfigurator: (Channel) t
108126
return sumOfStreamIDs
109127
}
110128
}
111-

0 commit comments

Comments
 (0)