@@ -15,28 +15,28 @@ final class MQTTNIOTests: XCTestCase {
15
15
16
16
func testConnectWithWill( ) throws {
17
17
let client = self . createClient ( identifier: " testConnectWithWill " )
18
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
18
19
_ = try client. connect (
19
20
will: ( topicName: " MyWillTopic " , payload: ByteBufferAllocator ( ) . buffer ( string: " Test payload " ) , qos: . atLeastOnce, retain: false )
20
21
) . wait ( )
21
22
try client. ping ( ) . wait ( )
22
23
try client. disconnect ( ) . wait ( )
23
- try client. syncShutdownGracefully ( )
24
24
}
25
25
26
26
func testConnectWithUsernameAndPassword( ) throws {
27
27
let client = self . createClient ( identifier: " testConnectWithWill " , configuration: . init( userName: " adam " , password: " password123 " ) )
28
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
28
29
_ = try client. connect ( ) . wait ( )
29
30
try client. ping ( ) . wait ( )
30
31
try client. disconnect ( ) . wait ( )
31
- try client. syncShutdownGracefully ( )
32
32
}
33
33
34
34
func testWebsocketConnect( ) throws {
35
35
let client = self . createWebSocketClient ( identifier: " testWebsocketConnect " )
36
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
36
37
_ = try client. connect ( ) . wait ( )
37
38
try client. ping ( ) . wait ( )
38
39
try client. disconnect ( ) . wait ( )
39
- try client. syncShutdownGracefully ( )
40
40
}
41
41
42
42
#if canImport(NIOSSL)
@@ -50,47 +50,48 @@ final class MQTTNIOTests: XCTestCase {
50
50
51
51
func testWebsocketAndSSLConnect( ) throws {
52
52
let client = try createWebSocketAndSSLClient ( identifier: " testWebsocketAndSSLConnect " )
53
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
53
54
_ = try client. connect ( ) . wait ( )
54
55
try client. ping ( ) . wait ( )
55
56
try client. disconnect ( ) . wait ( )
56
- try client. syncShutdownGracefully ( )
57
57
}
58
58
#endif
59
59
60
60
func testMQTTPublishQoS0( ) throws {
61
61
let client = self . createClient ( identifier: " testMQTTPublishQoS0 " )
62
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
62
63
_ = try client. connect ( ) . wait ( )
63
64
try client. publish ( to: " testMQTTPublishQoS " , payload: ByteBufferAllocator ( ) . buffer ( string: " Test payload " ) , qos: . atMostOnce) . wait ( )
64
65
try client. disconnect ( ) . wait ( )
65
- try client. syncShutdownGracefully ( )
66
66
}
67
67
68
68
func testMQTTPublishQoS1( ) throws {
69
69
let client = self . createClient ( identifier: " testMQTTPublishQoS1 " )
70
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
70
71
_ = try client. connect ( ) . wait ( )
71
72
try client. publish ( to: " testMQTTPublishQoS " , payload: ByteBufferAllocator ( ) . buffer ( string: " Test payload " ) , qos: . atLeastOnce) . wait ( )
72
73
try client. disconnect ( ) . wait ( )
73
- try client. syncShutdownGracefully ( )
74
74
}
75
75
76
76
func testMQTTPublishQoS2( ) throws {
77
77
let client = self . createWebSocketClient ( identifier: " testMQTTPublishQoS2 " )
78
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
78
79
_ = try client. connect ( ) . wait ( )
79
80
try client. publish ( to: " testMQTTPublishQoS " , payload: ByteBufferAllocator ( ) . buffer ( string: " Test payload " ) , qos: . exactlyOnce) . wait ( )
80
81
try client. disconnect ( ) . wait ( )
81
- try client. syncShutdownGracefully ( )
82
82
}
83
83
84
84
func testMQTTPingreq( ) throws {
85
85
let client = self . createClient ( identifier: " testMQTTPingreq " )
86
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
86
87
_ = try client. connect ( ) . wait ( )
87
88
try client. ping ( ) . wait ( )
88
89
try client. disconnect ( ) . wait ( )
89
- try client. syncShutdownGracefully ( )
90
90
}
91
91
92
92
func testMQTTSubscribe( ) throws {
93
93
let client = self . createClient ( identifier: " testMQTTSubscribe " )
94
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
94
95
_ = try client. connect ( ) . wait ( )
95
96
let sub = try client. subscribe (
96
97
to: [
@@ -101,13 +102,12 @@ final class MQTTNIOTests: XCTestCase {
101
102
XCTAssertEqual ( sub. returnCodes [ 0 ] , . grantedQoS1)
102
103
XCTAssertEqual ( sub. returnCodes [ 1 ] , . grantedQoS2)
103
104
try client. disconnect ( ) . wait ( )
104
- try client. syncShutdownGracefully ( )
105
105
}
106
106
107
107
func testMQTTServerDisconnect( ) throws {
108
108
let expectation = XCTestExpectation ( description: " testMQTTServerDisconnect " )
109
109
expectation. expectedFulfillmentCount = 1
110
-
110
+
111
111
struct MQTTForceDisconnectMessage : MQTTPacket {
112
112
var type : MQTTPacketType { . PUBLISH }
113
113
var description : String { " FORCEDISCONNECT " }
@@ -124,26 +124,27 @@ final class MQTTNIOTests: XCTestCase {
124
124
}
125
125
126
126
let client = self . createClient ( identifier: " testMQTTServerDisconnect " )
127
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
127
128
_ = try client. connect ( ) . wait ( )
128
129
try client. connection? . sendMessageNoWait ( MQTTForceDisconnectMessage ( ) ) . wait ( )
129
- client. addCloseListener ( named: " Test " ) { result in
130
+ client. addCloseListener ( named: " Test " ) { _ in
130
131
expectation. fulfill ( )
131
132
}
132
-
133
+
133
134
wait ( for: [ expectation] , timeout: 5.0 )
134
-
135
+
135
136
XCTAssertFalse ( client. isActive ( ) )
136
- try client. syncShutdownGracefully ( )
137
137
}
138
138
139
139
func testMQTTPublishRetain( ) throws {
140
140
let expectation = XCTestExpectation ( description: " testMQTTPublishRetain " )
141
141
expectation. expectedFulfillmentCount = 1
142
-
142
+
143
143
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"}"#
144
144
let payload = ByteBufferAllocator ( ) . buffer ( string: payloadString)
145
145
146
146
let client = self . createWebSocketClient ( identifier: " testMQTTPublishToClient_publisher " )
147
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
147
148
_ = try client. connect ( ) . wait ( )
148
149
client. addPublishListener ( named: " test " ) { result in
149
150
switch result {
@@ -162,27 +163,29 @@ final class MQTTNIOTests: XCTestCase {
162
163
wait ( for: [ expectation] , timeout: 5.0 )
163
164
164
165
try client. disconnect ( ) . wait ( )
165
- try client. syncShutdownGracefully ( )
166
166
}
167
167
168
168
func testMQTTPublishToClient( ) throws {
169
169
let expectation = XCTestExpectation ( description: " testMQTTPublishToClient " )
170
170
expectation. expectedFulfillmentCount = 2
171
-
171
+
172
172
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"}"#
173
173
let payload = ByteBufferAllocator ( ) . buffer ( string: payloadString)
174
174
175
175
let client = self . createWebSocketClient ( identifier: " testMQTTPublishToClient_publisher " )
176
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
176
177
_ = try client. connect ( ) . wait ( )
177
178
let client2 = self . createWebSocketClient ( identifier: " testMQTTPublishToClient_subscriber " )
179
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
180
+
178
181
client2. addPublishListener ( named: " test " ) { result in
179
182
switch result {
180
183
case . success( let publish) :
181
184
var buffer = publish. payload
182
185
let string = buffer. readString ( length: buffer. readableBytes)
183
186
XCTAssertEqual ( string, payloadString)
184
187
expectation. fulfill ( )
185
-
188
+
186
189
case . failure( let error) :
187
190
XCTFail ( " \( error) " )
188
191
}
@@ -192,26 +195,27 @@ final class MQTTNIOTests: XCTestCase {
192
195
_ = try client2. subscribe ( to: [ . init( topicFilter: " testExactlyOnce " , qos: . exactlyOnce) ] ) . wait ( )
193
196
try client. publish ( to: " testAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
194
197
try client. publish ( to: " testExactlyOnce " , payload: payload, qos: . exactlyOnce) . wait ( )
195
-
198
+
196
199
wait ( for: [ expectation] , timeout: 5.0 )
197
200
198
201
try client. disconnect ( ) . wait ( )
199
202
try client2. disconnect ( ) . wait ( )
200
- try client. syncShutdownGracefully ( )
201
- try client2. syncShutdownGracefully ( )
202
203
}
203
204
204
205
func testUnsubscribe( ) throws {
205
206
let expectation = XCTestExpectation ( description: " testMQTTPublishToClient " )
206
207
expectation. expectedFulfillmentCount = 1
207
208
expectation. assertForOverFulfill = true
208
-
209
+
209
210
let payloadString = #"test payload"#
210
211
let payload = ByteBufferAllocator ( ) . buffer ( string: payloadString)
211
212
212
213
let client = self . createClient ( identifier: " testUnsubscribe_publisher " )
214
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
213
215
_ = try client. connect ( ) . wait ( )
214
216
let client2 = self . createClient ( identifier: " testUnsubscribe_subscriber " )
217
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
218
+
215
219
client2. addPublishListener ( named: " test " ) { result in
216
220
switch result {
217
221
case . success( let publish) :
@@ -234,51 +238,52 @@ final class MQTTNIOTests: XCTestCase {
234
238
235
239
try client. disconnect ( ) . wait ( )
236
240
try client2. disconnect ( ) . wait ( )
237
- try client. syncShutdownGracefully ( )
238
- try client2. syncShutdownGracefully ( )
239
241
}
240
242
241
243
func testMQTTPublishToClientLargePayload( ) throws {
242
244
let expectation = XCTestExpectation ( description: " testMQTTPublishToClientLargePayload " )
243
245
expectation. expectedFulfillmentCount = 1
244
-
246
+
245
247
let payloadSize = 65537
246
248
let payloadData = Data ( count: payloadSize)
247
249
let payload = ByteBufferAllocator ( ) . buffer ( data: payloadData)
248
250
249
251
let client = self . createClient ( identifier: " testMQTTPublishToClientLargePayload_publisher " )
252
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
250
253
_ = try client. connect ( ) . wait ( )
251
254
let client2 = self . createClient ( identifier: " testMQTTPublishToClientLargePayload_subscriber " )
255
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
256
+
252
257
client2. addPublishListener ( named: " test " ) { result in
253
258
switch result {
254
259
case . success( let publish) :
255
260
var buffer = publish. payload
256
261
let data = buffer. readData ( length: buffer. readableBytes)
257
262
XCTAssertEqual ( data, payloadData)
258
263
expectation. fulfill ( )
259
-
264
+
260
265
case . failure( let error) :
261
266
XCTFail ( " \( error) " )
262
267
}
263
268
}
264
269
_ = try client2. connect ( ) . wait ( )
265
270
_ = try client2. subscribe ( to: [ . init( topicFilter: " testLargeAtLeastOnce " , qos: . atLeastOnce) ] ) . wait ( )
266
271
try client. publish ( to: " testLargeAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
267
-
272
+
268
273
wait ( for: [ expectation] , timeout: 5.0 )
269
274
270
275
try client. disconnect ( ) . wait ( )
271
276
try client2. disconnect ( ) . wait ( )
272
- try client. syncShutdownGracefully ( )
273
- try client2. syncShutdownGracefully ( )
274
277
}
275
278
276
279
func testCloseListener( ) throws {
277
280
let expectation = XCTestExpectation ( description: " testCloseListener " )
278
281
expectation. expectedFulfillmentCount = 1
279
-
282
+
280
283
let client = self . createWebSocketClient ( identifier: " testCloseListener " )
284
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
281
285
let client2 = self . createWebSocketClient ( identifier: " testCloseListener " )
286
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
282
287
283
288
client. addCloseListener ( named: " Reconnect " ) { result in
284
289
switch result {
@@ -296,23 +301,22 @@ final class MQTTNIOTests: XCTestCase {
296
301
wait ( for: [ expectation] , timeout: 5.0 )
297
302
298
303
try client2. disconnect ( ) . wait ( )
299
- try client. syncShutdownGracefully ( )
300
- try client2. syncShutdownGracefully ( )
301
304
}
302
305
303
306
func testDoubleConnect( ) throws {
304
307
let client = self . createClient ( identifier: " DoubleConnect " )
305
308
_ = try client. connect ( cleanSession: true ) . wait ( )
309
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
306
310
let sessionPresent = try client. connect ( cleanSession: false ) . wait ( )
307
311
let sessionPresent2 = try client. connect ( cleanSession: false ) . wait ( )
308
312
XCTAssertFalse ( sessionPresent)
309
313
XCTAssertTrue ( sessionPresent2)
310
314
try client. disconnect ( ) . wait ( )
311
- try client. syncShutdownGracefully ( )
312
315
}
313
316
314
317
func testSessionPresent( ) throws {
315
318
let client = self . createClient ( identifier: " testSessionPresent " )
319
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
316
320
317
321
_ = try client. connect ( cleanSession: true ) . wait ( )
318
322
var connack = try client. connect ( cleanSession: false ) . wait ( )
@@ -331,8 +335,11 @@ final class MQTTNIOTests: XCTestCase {
331
335
let payload = ByteBufferAllocator ( ) . buffer ( string: payloadString)
332
336
333
337
let client = self . createClient ( identifier: " testPersistentSession_publisher " )
338
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
334
339
_ = try client. connect ( ) . wait ( )
335
340
let client2 = self . createClient ( identifier: " testPersistentSession_subscriber " )
341
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
342
+
336
343
client2. addPublishListener ( named: " test " ) { result in
337
344
switch result {
338
345
case . success( let publish) :
@@ -360,13 +367,11 @@ final class MQTTNIOTests: XCTestCase {
360
367
try client. publish ( to: " testPersistentAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
361
368
// should not receive previous publish on connect as this is a cleanSession
362
369
_ = try client2. connect ( cleanSession: true ) . wait ( )
363
-
370
+
364
371
wait ( for: [ expectation] , timeout: 5.0 )
365
372
366
373
try client. disconnect ( ) . wait ( )
367
374
try client2. disconnect ( ) . wait ( )
368
- try client. syncShutdownGracefully ( )
369
- try client2. syncShutdownGracefully ( )
370
375
}
371
376
372
377
func testSubscribeAll( ) throws {
@@ -400,10 +405,10 @@ final class MQTTNIOTests: XCTestCase {
400
405
eventLoopGroupProvider: . shared( elg) ,
401
406
logger: self . logger
402
407
)
408
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
403
409
_ = try client. connect ( ) . wait ( )
404
410
_ = try client. ping ( ) . wait ( )
405
411
try client. disconnect ( ) . wait ( )
406
- try client. syncShutdownGracefully ( )
407
412
#endif
408
413
}
409
414
0 commit comments