Skip to content

Commit

Permalink
Only emit connection disconnect event for non internal disconnect (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfianlosari authored Aug 9, 2023
1 parent 50c98ba commit 00ccab8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CourierMQTT/MQTT/Client/IMQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol IMQTTClient {

func connect(connectOptions: ConnectOptions)
func reconnect()
func disconnect()
func disconnect(isInternal: Bool)

func subscribe(_ topics: [(topic: String, qos: QoS)])
func unsubscribe(_ topics: [String])
Expand Down
12 changes: 7 additions & 5 deletions CourierMQTT/MQTT/Client/MQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ class MQTTClient: IMQTTClient {
return
}
eventHandler.onEvent(.init(connectionInfo: options, event: .reconnect))
disconnect()
disconnect(isInternal: true)
connect(connectOptions: options)
}

func disconnect() {
eventHandler.onEvent(.init(connectionInfo: connectOptions, event: .connectionDisconnect))
func disconnect(isInternal: Bool) {
if !isInternal {
eventHandler.onEvent(.init(connectionInfo: connectOptions, event: .connectionDisconnect))
}
isInitialized = false
disconnectMqtt()
}
Expand Down Expand Up @@ -185,7 +187,7 @@ class MQTTClient: IMQTTClient {
}

func destroy() {
disconnect()
disconnect(isInternal: false)
connectOptions = nil
removeObserveAppLifecycle()
removeObserveNetwork()
Expand Down Expand Up @@ -218,7 +220,7 @@ extension MQTTClient {

extension MQTTClient: KeepAliveFailureHandler {
func handleKeepAliveFailure() {
disconnect()
disconnect(isInternal: true)
reconnect()
}
}
2 changes: 1 addition & 1 deletion CourierMQTT/MQTT/MQTTCourierClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class MQTTCourierClient: CourierClient {

extension MQTTCourierClient: IAuthFailureHandler {
func handleAuthFailure() {
client.disconnect()
client.disconnect(isInternal: true)
connectionServiceProvider.clearCachedAuthResponse()
connect()
}
Expand Down
27 changes: 25 additions & 2 deletions CourierTests/Core/MQTT/Client/MQTTClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class MQTTClientTests: XCTestCase {
} else {
XCTAssert(false)
}
XCTAssertEqual(self.mockEventHandler.invokedOnEventParametersList.count, 1)
XCTAssertTrue(mockConnection.invokedConnect)
}

Expand Down Expand Up @@ -114,10 +115,19 @@ class MQTTClientTests: XCTestCase {
XCTAssertTrue(mockConnection.invokedUnsubscribe)
XCTAssertEqual(mockConnection.invokedUnsubscribeParameters?.topics.first, "fbon")
}

func testDisconnectIsInternalTrue() {
mockConnection.stubbedIsConnected = true
sut.disconnect(isInternal: true)

XCTAssertTrue(self.mockEventHandler.invokedOnEventParametersList.isEmpty)
XCTAssertFalse(sut.isInitialized)
XCTAssertTrue(mockConnection.invokedDisconnect)
}

func testDisconnect() {
func testDisconnectIsNotInternalFalse() {
mockConnection.stubbedIsConnected = true
sut.disconnect()
sut.disconnect(isInternal: false)

if case .connectionDisconnect = self.mockEventHandler.invokedOnEventParametersList.first!!.event.type {
XCTAssert(true)
Expand Down Expand Up @@ -145,6 +155,12 @@ class MQTTClientTests: XCTestCase {

XCTAssertFalse(sut.isInitialized)
XCTAssertTrue(mockConnection.invokedDisconnect)

if case .connectionDisconnect = self.mockEventHandler.invokedOnEventParametersList.first!!.event.type {
XCTAssert(true)
} else {
XCTAssert(false)
}

XCTAssertNil(sut.connectOptions)
XCTAssertEqual(mockNotificationCenter.invokedRemoveObserverWithNameParametersList[0].name, UIApplication.willResignActiveNotification)
Expand All @@ -168,6 +184,12 @@ class MQTTClientTests: XCTestCase {

XCTAssertFalse(sut.isInitialized)
XCTAssertTrue(mockConnection.invokedDisconnect)

if case .connectionDisconnect = self.mockEventHandler.invokedOnEventParametersList.first!!.event.type {
XCTAssert(true)
} else {
XCTAssert(false)
}

XCTAssertNil(sut.connectOptions)
XCTAssertEqual(mockNotificationCenter.invokedRemoveObserverWithNameParametersList[0].name, UIApplication.didEnterBackgroundNotification)
Expand All @@ -186,6 +208,7 @@ class MQTTClientTests: XCTestCase {
sut.handleKeepAliveFailure()
XCTAssertTrue(mockConnection.invokedDisconnect)
XCTAssertTrue(mockConnection.invokedConnect)
XCTAssertEqual(self.mockEventHandler.invokedOnEventParametersList.count, 1)
}

func testResetWithActiveNotifications() {
Expand Down
2 changes: 1 addition & 1 deletion CourierTests/Core/MQTT/MQTTCourierClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class MQTTCourierClientTests: XCTestCase {

func testHandleAuthFailure() {
sut.handleAuthFailure()
// XCTAssertTrue(mockEventHandler.invokedReset)
XCTAssertTrue(mockClient.invokedDisconnectParameters!.isInternal)
if case .connectionServiceAuthStart = self.mockEventHandler.invokedOnEventParameters?.event.type {
XCTAssert(true)
} else {
Expand Down
6 changes: 5 additions & 1 deletion CourierTests/Core/Mocks/MockMQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ class MockMQTTClient: IMQTTClient {

var invokedDisconnect = false
var invokedDisconnectCount = 0
var invokedDisconnectParameters: (isInternal: Bool, Void)?
var invokedDisconnectParametersList = [(isInternal: Bool, Void)]()

func disconnect() {
func disconnect(isInternal: Bool) {
invokedDisconnect = true
invokedDisconnectCount += 1
invokedDisconnectParameters = (isInternal, ())
invokedDisconnectParametersList.append((isInternal, ()))
}

var invokedSubscribe = false
Expand Down

0 comments on commit 00ccab8

Please sign in to comment.