@@ -451,7 +451,6 @@ final class MQTTNIOTests: XCTestCase {
451
451
XCTFail ( " \( error) " )
452
452
}
453
453
}
454
- _ = try client2. connect ( cleanSession: true ) . wait ( )
455
454
_ = try client2. connect ( cleanSession: false ) . wait ( )
456
455
_ = try client2. subscribe ( to: [ . init( topicFilter: " testPersistentAtLeastOnce " , qos: . atLeastOnce) ] ) . wait ( )
457
456
try client. publish ( to: " testPersistentAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
@@ -461,11 +460,49 @@ final class MQTTNIOTests: XCTestCase {
461
460
// client2 should receive this publish as we have reconnected with clean session set to false
462
461
try client. publish ( to: " testPersistentAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
463
462
Thread . sleep ( forTimeInterval: 1 )
463
+
464
+ wait ( for: [ expectation] , timeout: 5.0 )
465
+
466
+ try client. disconnect ( ) . wait ( )
467
+ try client2. disconnect ( ) . wait ( )
468
+ }
469
+
470
+ func testNonPersistentSession( ) throws {
471
+ let expectation = XCTestExpectation ( description: " testPersistentSession " )
472
+ expectation. expectedFulfillmentCount = 1
473
+ expectation. assertForOverFulfill = true
474
+
475
+ 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"}"#
476
+ let payload = ByteBufferAllocator ( ) . buffer ( string: payloadString)
477
+
478
+ let client = self . createClient ( identifier: " testPersistentSession_publisher " )
479
+ defer { XCTAssertNoThrow ( try client. syncShutdownGracefully ( ) ) }
480
+ _ = try client. connect ( ) . wait ( )
481
+ let client2 = self . createClient ( identifier: " testPersistentSession_subscriber " )
482
+ defer { XCTAssertNoThrow ( try client2. syncShutdownGracefully ( ) ) }
483
+
484
+ client2. addPublishListener ( named: " test " ) { result in
485
+ switch result {
486
+ case . success( let publish) :
487
+ var buffer = publish. payload
488
+ let string = buffer. readString ( length: buffer. readableBytes)
489
+ XCTAssertEqual ( string, payloadString)
490
+ expectation. fulfill ( )
491
+
492
+ case . failure( let error) :
493
+ XCTFail ( " \( error) " )
494
+ }
495
+ }
496
+ _ = try client2. connect ( cleanSession: false ) . wait ( )
497
+ _ = try client2. subscribe ( to: [ . init( topicFilter: " testPersistentAtLeastOnce " , qos: . atLeastOnce) ] ) . wait ( )
498
+ try client. publish ( to: " testPersistentAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
499
+ Thread . sleep ( forTimeInterval: 1 )
500
+ // disconnect and reconnect with clean session
464
501
try client2. disconnect ( ) . wait ( )
465
- // should not receive previous publish on connect as this is a cleanSession
466
502
_ = try client2. connect ( cleanSession: true ) . wait ( )
467
503
// client2 should not receive this publish as we have reconnected with clean session set to true
468
504
try client. publish ( to: " testPersistentAtLeastOnce " , payload: payload, qos: . atLeastOnce) . wait ( )
505
+ Thread . sleep ( forTimeInterval: 1 )
469
506
470
507
wait ( for: [ expectation] , timeout: 5.0 )
471
508
0 commit comments