Skip to content

Commit 5efd2b2

Browse files
authored
Require SwiftFormat to run on PRs (#75)
* Add sanity script and GitHub workflow * Run swift format * Fix up shutdown calls for client in tests
1 parent 82727b3 commit 5efd2b2

File tree

6 files changed

+126
-60
lines changed

6 files changed

+126
-60
lines changed

.github/workflows/sanity.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Sanity Check
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
sanity-check:
8+
runs-on: macOS-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v1
12+
with:
13+
fetch-depth: 1
14+
- name: Install Dependencies
15+
run: |
16+
brew install mint
17+
mint install nicklockwood/[email protected] --no-link
18+
- name: run script
19+
run: ./scripts/sanity.sh

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
/.build
33
/.swiftpm
4+
/docs
45
/Packages
56
/*.xcodeproj
67
xcuserdata/

Sources/MQTTNIO/MQTTClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public final class MQTTClient {
125125

126126
deinit {
127127
guard isShutdown.load() else {
128-
preconditionFailure("Client not shut down before the deinit. Please call client.syncShutdown() when no longer needed.")
128+
preconditionFailure("Client not shut down before the deinit. Please call client.syncShutdownGracefully() when no longer needed.")
129129
}
130130
}
131131

@@ -380,7 +380,7 @@ public final class MQTTClient {
380380
private var lock = Lock()
381381
}
382382

383-
extension MQTTClient {
383+
internal extension MQTTClient {
384384
/// connect to broker
385385
func connect(
386386
packet: MQTTConnectPacket,

Tests/MQTTNIOTests/MQTTNIOTests.swift

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ final class MQTTNIOTests: XCTestCase {
1515

1616
func testConnectWithWill() throws {
1717
let client = self.createClient(identifier: "testConnectWithWill")
18+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
1819
_ = try client.connect(
1920
will: (topicName: "MyWillTopic", payload: ByteBufferAllocator().buffer(string: "Test payload"), qos: .atLeastOnce, retain: false)
2021
).wait()
2122
try client.ping().wait()
2223
try client.disconnect().wait()
23-
try client.syncShutdownGracefully()
2424
}
2525

2626
func testConnectWithUsernameAndPassword() throws {
2727
let client = self.createClient(identifier: "testConnectWithWill", configuration: .init(userName: "adam", password: "password123"))
28+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
2829
_ = try client.connect().wait()
2930
try client.ping().wait()
3031
try client.disconnect().wait()
31-
try client.syncShutdownGracefully()
3232
}
3333

3434
func testWebsocketConnect() throws {
3535
let client = self.createWebSocketClient(identifier: "testWebsocketConnect")
36+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
3637
_ = try client.connect().wait()
3738
try client.ping().wait()
3839
try client.disconnect().wait()
39-
try client.syncShutdownGracefully()
4040
}
4141

4242
#if canImport(NIOSSL)
@@ -50,47 +50,48 @@ final class MQTTNIOTests: XCTestCase {
5050

5151
func testWebsocketAndSSLConnect() throws {
5252
let client = try createWebSocketAndSSLClient(identifier: "testWebsocketAndSSLConnect")
53+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
5354
_ = try client.connect().wait()
5455
try client.ping().wait()
5556
try client.disconnect().wait()
56-
try client.syncShutdownGracefully()
5757
}
5858
#endif
5959

6060
func testMQTTPublishQoS0() throws {
6161
let client = self.createClient(identifier: "testMQTTPublishQoS0")
62+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
6263
_ = try client.connect().wait()
6364
try client.publish(to: "testMQTTPublishQoS", payload: ByteBufferAllocator().buffer(string: "Test payload"), qos: .atMostOnce).wait()
6465
try client.disconnect().wait()
65-
try client.syncShutdownGracefully()
6666
}
6767

6868
func testMQTTPublishQoS1() throws {
6969
let client = self.createClient(identifier: "testMQTTPublishQoS1")
70+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
7071
_ = try client.connect().wait()
7172
try client.publish(to: "testMQTTPublishQoS", payload: ByteBufferAllocator().buffer(string: "Test payload"), qos: .atLeastOnce).wait()
7273
try client.disconnect().wait()
73-
try client.syncShutdownGracefully()
7474
}
7575

7676
func testMQTTPublishQoS2() throws {
7777
let client = self.createWebSocketClient(identifier: "testMQTTPublishQoS2")
78+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
7879
_ = try client.connect().wait()
7980
try client.publish(to: "testMQTTPublishQoS", payload: ByteBufferAllocator().buffer(string: "Test payload"), qos: .exactlyOnce).wait()
8081
try client.disconnect().wait()
81-
try client.syncShutdownGracefully()
8282
}
8383

8484
func testMQTTPingreq() throws {
8585
let client = self.createClient(identifier: "testMQTTPingreq")
86+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
8687
_ = try client.connect().wait()
8788
try client.ping().wait()
8889
try client.disconnect().wait()
89-
try client.syncShutdownGracefully()
9090
}
9191

9292
func testMQTTSubscribe() throws {
9393
let client = self.createClient(identifier: "testMQTTSubscribe")
94+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
9495
_ = try client.connect().wait()
9596
let sub = try client.subscribe(
9697
to: [
@@ -101,13 +102,12 @@ final class MQTTNIOTests: XCTestCase {
101102
XCTAssertEqual(sub.returnCodes[0], .grantedQoS1)
102103
XCTAssertEqual(sub.returnCodes[1], .grantedQoS2)
103104
try client.disconnect().wait()
104-
try client.syncShutdownGracefully()
105105
}
106106

107107
func testMQTTServerDisconnect() throws {
108108
let expectation = XCTestExpectation(description: "testMQTTServerDisconnect")
109109
expectation.expectedFulfillmentCount = 1
110-
110+
111111
struct MQTTForceDisconnectMessage: MQTTPacket {
112112
var type: MQTTPacketType { .PUBLISH }
113113
var description: String { "FORCEDISCONNECT" }
@@ -124,26 +124,27 @@ final class MQTTNIOTests: XCTestCase {
124124
}
125125

126126
let client = self.createClient(identifier: "testMQTTServerDisconnect")
127+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
127128
_ = try client.connect().wait()
128129
try client.connection?.sendMessageNoWait(MQTTForceDisconnectMessage()).wait()
129-
client.addCloseListener(named: "Test") { result in
130+
client.addCloseListener(named: "Test") { _ in
130131
expectation.fulfill()
131132
}
132-
133+
133134
wait(for: [expectation], timeout: 5.0)
134-
135+
135136
XCTAssertFalse(client.isActive())
136-
try client.syncShutdownGracefully()
137137
}
138138

139139
func testMQTTPublishRetain() throws {
140140
let expectation = XCTestExpectation(description: "testMQTTPublishRetain")
141141
expectation.expectedFulfillmentCount = 1
142-
142+
143143
let payloadString = #"{"from":1000000,"to":1234567,"type":1,"content":"I am a beginner in swift and I am studying hard!!测试\n\n test, message","timestamp":1607243024,"nonce":"pAx2EsUuXrVuiIU3GGOGHNbUjzRRdT5b","sign":"ff902e31a6a5f5343d70a3a93ac9f946adf1caccab539c6f3a6"}"#
144144
let payload = ByteBufferAllocator().buffer(string: payloadString)
145145

146146
let client = self.createWebSocketClient(identifier: "testMQTTPublishToClient_publisher")
147+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
147148
_ = try client.connect().wait()
148149
client.addPublishListener(named: "test") { result in
149150
switch result {
@@ -162,27 +163,29 @@ final class MQTTNIOTests: XCTestCase {
162163
wait(for: [expectation], timeout: 5.0)
163164

164165
try client.disconnect().wait()
165-
try client.syncShutdownGracefully()
166166
}
167167

168168
func testMQTTPublishToClient() throws {
169169
let expectation = XCTestExpectation(description: "testMQTTPublishToClient")
170170
expectation.expectedFulfillmentCount = 2
171-
171+
172172
let payloadString = #"{"from":1000000,"to":1234567,"type":1,"content":"I am a beginner in swift and I am studying hard!!测试\n\n test, message","timestamp":1607243024,"nonce":"pAx2EsUuXrVuiIU3GGOGHNbUjzRRdT5b","sign":"ff902e31a6a5f5343d70a3a93ac9f946adf1caccab539c6f3a6"}"#
173173
let payload = ByteBufferAllocator().buffer(string: payloadString)
174174

175175
let client = self.createWebSocketClient(identifier: "testMQTTPublishToClient_publisher")
176+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
176177
_ = try client.connect().wait()
177178
let client2 = self.createWebSocketClient(identifier: "testMQTTPublishToClient_subscriber")
179+
defer { XCTAssertNoThrow(try client2.syncShutdownGracefully()) }
180+
178181
client2.addPublishListener(named: "test") { result in
179182
switch result {
180183
case .success(let publish):
181184
var buffer = publish.payload
182185
let string = buffer.readString(length: buffer.readableBytes)
183186
XCTAssertEqual(string, payloadString)
184187
expectation.fulfill()
185-
188+
186189
case .failure(let error):
187190
XCTFail("\(error)")
188191
}
@@ -192,26 +195,27 @@ final class MQTTNIOTests: XCTestCase {
192195
_ = try client2.subscribe(to: [.init(topicFilter: "testExactlyOnce", qos: .exactlyOnce)]).wait()
193196
try client.publish(to: "testAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
194197
try client.publish(to: "testExactlyOnce", payload: payload, qos: .exactlyOnce).wait()
195-
198+
196199
wait(for: [expectation], timeout: 5.0)
197200

198201
try client.disconnect().wait()
199202
try client2.disconnect().wait()
200-
try client.syncShutdownGracefully()
201-
try client2.syncShutdownGracefully()
202203
}
203204

204205
func testUnsubscribe() throws {
205206
let expectation = XCTestExpectation(description: "testMQTTPublishToClient")
206207
expectation.expectedFulfillmentCount = 1
207208
expectation.assertForOverFulfill = true
208-
209+
209210
let payloadString = #"test payload"#
210211
let payload = ByteBufferAllocator().buffer(string: payloadString)
211212

212213
let client = self.createClient(identifier: "testUnsubscribe_publisher")
214+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
213215
_ = try client.connect().wait()
214216
let client2 = self.createClient(identifier: "testUnsubscribe_subscriber")
217+
defer { XCTAssertNoThrow(try client2.syncShutdownGracefully()) }
218+
215219
client2.addPublishListener(named: "test") { result in
216220
switch result {
217221
case .success(let publish):
@@ -234,51 +238,52 @@ final class MQTTNIOTests: XCTestCase {
234238

235239
try client.disconnect().wait()
236240
try client2.disconnect().wait()
237-
try client.syncShutdownGracefully()
238-
try client2.syncShutdownGracefully()
239241
}
240242

241243
func testMQTTPublishToClientLargePayload() throws {
242244
let expectation = XCTestExpectation(description: "testMQTTPublishToClientLargePayload")
243245
expectation.expectedFulfillmentCount = 1
244-
246+
245247
let payloadSize = 65537
246248
let payloadData = Data(count: payloadSize)
247249
let payload = ByteBufferAllocator().buffer(data: payloadData)
248250

249251
let client = self.createClient(identifier: "testMQTTPublishToClientLargePayload_publisher")
252+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
250253
_ = try client.connect().wait()
251254
let client2 = self.createClient(identifier: "testMQTTPublishToClientLargePayload_subscriber")
255+
defer { XCTAssertNoThrow(try client2.syncShutdownGracefully()) }
256+
252257
client2.addPublishListener(named: "test") { result in
253258
switch result {
254259
case .success(let publish):
255260
var buffer = publish.payload
256261
let data = buffer.readData(length: buffer.readableBytes)
257262
XCTAssertEqual(data, payloadData)
258263
expectation.fulfill()
259-
264+
260265
case .failure(let error):
261266
XCTFail("\(error)")
262267
}
263268
}
264269
_ = try client2.connect().wait()
265270
_ = try client2.subscribe(to: [.init(topicFilter: "testLargeAtLeastOnce", qos: .atLeastOnce)]).wait()
266271
try client.publish(to: "testLargeAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
267-
272+
268273
wait(for: [expectation], timeout: 5.0)
269274

270275
try client.disconnect().wait()
271276
try client2.disconnect().wait()
272-
try client.syncShutdownGracefully()
273-
try client2.syncShutdownGracefully()
274277
}
275278

276279
func testCloseListener() throws {
277280
let expectation = XCTestExpectation(description: "testCloseListener")
278281
expectation.expectedFulfillmentCount = 1
279-
282+
280283
let client = self.createWebSocketClient(identifier: "testCloseListener")
284+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
281285
let client2 = self.createWebSocketClient(identifier: "testCloseListener")
286+
defer { XCTAssertNoThrow(try client2.syncShutdownGracefully()) }
282287

283288
client.addCloseListener(named: "Reconnect") { result in
284289
switch result {
@@ -296,23 +301,22 @@ final class MQTTNIOTests: XCTestCase {
296301
wait(for: [expectation], timeout: 5.0)
297302

298303
try client2.disconnect().wait()
299-
try client.syncShutdownGracefully()
300-
try client2.syncShutdownGracefully()
301304
}
302305

303306
func testDoubleConnect() throws {
304307
let client = self.createClient(identifier: "DoubleConnect")
305308
_ = try client.connect(cleanSession: true).wait()
309+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
306310
let sessionPresent = try client.connect(cleanSession: false).wait()
307311
let sessionPresent2 = try client.connect(cleanSession: false).wait()
308312
XCTAssertFalse(sessionPresent)
309313
XCTAssertTrue(sessionPresent2)
310314
try client.disconnect().wait()
311-
try client.syncShutdownGracefully()
312315
}
313316

314317
func testSessionPresent() throws {
315318
let client = self.createClient(identifier: "testSessionPresent")
319+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
316320

317321
_ = try client.connect(cleanSession: true).wait()
318322
var connack = try client.connect(cleanSession: false).wait()
@@ -331,8 +335,11 @@ final class MQTTNIOTests: XCTestCase {
331335
let payload = ByteBufferAllocator().buffer(string: payloadString)
332336

333337
let client = self.createClient(identifier: "testPersistentSession_publisher")
338+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
334339
_ = try client.connect().wait()
335340
let client2 = self.createClient(identifier: "testPersistentSession_subscriber")
341+
defer { XCTAssertNoThrow(try client2.syncShutdownGracefully()) }
342+
336343
client2.addPublishListener(named: "test") { result in
337344
switch result {
338345
case .success(let publish):
@@ -360,13 +367,11 @@ final class MQTTNIOTests: XCTestCase {
360367
try client.publish(to: "testPersistentAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
361368
// should not receive previous publish on connect as this is a cleanSession
362369
_ = try client2.connect(cleanSession: true).wait()
363-
370+
364371
wait(for: [expectation], timeout: 5.0)
365372

366373
try client.disconnect().wait()
367374
try client2.disconnect().wait()
368-
try client.syncShutdownGracefully()
369-
try client2.syncShutdownGracefully()
370375
}
371376

372377
func testSubscribeAll() throws {
@@ -400,10 +405,10 @@ final class MQTTNIOTests: XCTestCase {
400405
eventLoopGroupProvider: .shared(elg),
401406
logger: self.logger
402407
)
408+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
403409
_ = try client.connect().wait()
404410
_ = try client.ping().wait()
405411
try client.disconnect().wait()
406-
try client.syncShutdownGracefully()
407412
#endif
408413
}
409414

0 commit comments

Comments
 (0)