Skip to content

Commit 2310017

Browse files
committed
Update persistent session tests
1 parent 05fbcc5 commit 2310017

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Tests/MQTTNIOTests/MQTTNIOTests.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ final class MQTTNIOTests: XCTestCase {
451451
XCTFail("\(error)")
452452
}
453453
}
454-
_ = try client2.connect(cleanSession: true).wait()
455454
_ = try client2.connect(cleanSession: false).wait()
456455
_ = try client2.subscribe(to: [.init(topicFilter: "testPersistentAtLeastOnce", qos: .atLeastOnce)]).wait()
457456
try client.publish(to: "testPersistentAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
@@ -461,11 +460,49 @@ final class MQTTNIOTests: XCTestCase {
461460
// client2 should receive this publish as we have reconnected with clean session set to false
462461
try client.publish(to: "testPersistentAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
463462
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
464501
try client2.disconnect().wait()
465-
// should not receive previous publish on connect as this is a cleanSession
466502
_ = try client2.connect(cleanSession: true).wait()
467503
// client2 should not receive this publish as we have reconnected with clean session set to true
468504
try client.publish(to: "testPersistentAtLeastOnce", payload: payload, qos: .atLeastOnce).wait()
505+
Thread.sleep(forTimeInterval: 1)
469506

470507
wait(for: [expectation], timeout: 5.0)
471508

0 commit comments

Comments
 (0)