Skip to content

Commit ef331a6

Browse files
authored
Add more tests (#84)
* Add testBadAuthenticationMethod and testAuth * Don't throw error on already closed channel when closing * swift format
1 parent 2a6ea26 commit ef331a6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Sources/MQTTNIO/MQTTClient.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ public final class MQTTClient {
185185
let closeError: Error?
186186
switch result {
187187
case .failure(let error):
188-
closeError = error
188+
if case ChannelError.alreadyClosed = error {
189+
closeError = nil
190+
} else {
191+
closeError = error
192+
}
189193
case .success:
190194
closeError = nil
191195
}

Tests/MQTTNIOTests/MQTTNIOv5Tests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ final class MQTTNIOv5Tests: XCTestCase {
284284
try client2.disconnect().wait()
285285
}
286286

287+
func testBadAuthenticationMethod() throws {
288+
let client = self.createClient(identifier: "testSessionExpiryInterval")
289+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
290+
XCTAssertThrowsError(_ = try client.v5.connect(properties: [.authenticationMethod("test")]).wait()) { error in
291+
switch error {
292+
case MQTTError.reasonError(let reason):
293+
XCTAssertEqual(reason, .badAuthenticationMethod)
294+
default:
295+
XCTFail("\(error)")
296+
}
297+
}
298+
}
299+
287300
func testInvalidTopicName() throws {
288301
let client = self.createClient(identifier: "testInvalidTopicName")
289302
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
@@ -327,6 +340,24 @@ final class MQTTNIOv5Tests: XCTestCase {
327340
try client.disconnect().wait()
328341
}
329342

343+
func testAuth() throws {
344+
let client = self.createClient(identifier: "testReauth")
345+
defer { XCTAssertNoThrow(try client.syncShutdownGracefully()) }
346+
347+
_ = try client.v5.connect().wait()
348+
let authFuture = client.v5.auth(properties: []) { _, eventLoop in
349+
return eventLoop.makeSucceededFuture(.init(reason: .continueAuthentication, properties: []))
350+
}
351+
XCTAssertThrowsError(try authFuture.wait()) { error in
352+
switch error {
353+
case MQTTError.serverClosedConnection:
354+
break
355+
default:
356+
XCTFail("\(error)")
357+
}
358+
}
359+
}
360+
330361
func testSubscribeAll() throws {
331362
if ProcessInfo.processInfo.environment["CI"] != nil {
332363
return

0 commit comments

Comments
 (0)