Skip to content

Commit

Permalink
Fix duplicated onConnectionSuccess and onConnectionFailure calls (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored Feb 8, 2024
1 parent 8f8109b commit 7126375
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,6 @@ private void onConnectionComplete(int errorCode, boolean sessionPresent) {
}
connectAck = null;
}

MqttClientConnectionEvents callbacks = config.getConnectionCallbacks();
if (callbacks != null) {
if (errorCode == 0) {
OnConnectionSuccessReturn returnData = new OnConnectionSuccessReturn(sessionPresent);
callbacks.onConnectionSuccess(returnData);
} else {
OnConnectionFailureReturn returnData = new OnConnectionFailureReturn(errorCode);
callbacks.onConnectionFailure(returnData);
}
}
}

// Called when the connection drops or is disconnected. If errorCode == 0, the
Expand Down
2 changes: 2 additions & 0 deletions src/native/mqtt_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClientConnectio
return (jlong)NULL;
}

aws_mqtt_client_connection_set_connection_result_handlers(
connection->client_connection, s_on_connection_success, connection, s_on_connection_failure, connection);
aws_mqtt_client_connection_set_connection_interruption_handlers(
connection->client_connection, s_on_connection_interrupted, connection, s_on_connection_resumed, connection);
aws_mqtt_client_connection_set_connection_closed_handler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public class MqttClientConnectionFixture extends CrtTestFixture {
String caRoot = null;
String iotEndpoint = null;

class ConnectionEventsStatistics {
int onConnectionSuccessCalled;
int onConnectionFailureCalled;
int onConnectionResumedCalled;
int onConnectionInterruptedCalled;
int onConnectionClosedCalled;
};

ConnectionEventsStatistics connectionEventsStatistics = new ConnectionEventsStatistics();

Consumer<MqttConnectionConfig> connectionConfigTransformer = null;
protected void setConnectionConfigTransformer(Consumer<MqttConnectionConfig> connectionConfigTransformer) {
this.connectionConfigTransformer = connectionConfigTransformer;
Expand Down Expand Up @@ -155,11 +165,13 @@ boolean connectDirectWithConfigThrows(TlsContext tlsContext, String endpoint, in
MqttClientConnectionEvents events = new MqttClientConnectionEvents() {
@Override
public void onConnectionResumed(boolean sessionPresent) {
connectionEventsStatistics.onConnectionResumedCalled++;
System.out.println("Connection resumed");
}

@Override
public void onConnectionInterrupted(int errorCode) {
connectionEventsStatistics.onConnectionInterruptedCalled++;
if (!disconnecting) {
System.out.println(
"Connection interrupted: error: " + errorCode + " " + CRT.awsErrorString(errorCode));
Expand All @@ -169,18 +181,21 @@ public void onConnectionInterrupted(int errorCode) {
@Override
public void onConnectionFailure(OnConnectionFailureReturn data) {
System.out.println("Connection failed with error: " + data.getErrorCode() + " " + CRT.awsErrorString(data.getErrorCode()));
connectionEventsStatistics.onConnectionFailureCalled++;
onConnectionFailureFuture.complete(data);
}

@Override
public void onConnectionSuccess(OnConnectionSuccessReturn data) {
System.out.println("Connection success. Session present: " + data.getSessionPresent());
connectionEventsStatistics.onConnectionSuccessCalled++;
onConnectionSuccessFuture.complete(data);
}

@Override
public void onConnectionClosed(OnConnectionClosedReturn data) {
System.out.println("Connection disconnected successfully");
connectionEventsStatistics.onConnectionClosedCalled++;
onConnectionClosedFuture.complete(data);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.junit.Assume;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -179,6 +180,9 @@ public void testConnectDisconnectEventsHappy() {
fail(ex.toString());
}
close();

assertEquals("Unexpected onConnectionSuccess call: ", 1, connectionEventsStatistics.onConnectionSuccessCalled);
assertEquals("Unexpected onConnectionClosed call: ", 1, connectionEventsStatistics.onConnectionClosedCalled);
}
}

Expand Down Expand Up @@ -212,6 +216,9 @@ public void testConnectDisconnectEventsUnhappy() {
fail(ex.toString());
}
close();

assertEquals("Unexpected onConnectionFailure call: ", 1, connectionEventsStatistics.onConnectionFailureCalled);
assertEquals("Unexpected onConnectionClosed call: ", 0, connectionEventsStatistics.onConnectionClosedCalled);
}
}
};

0 comments on commit 7126375

Please sign in to comment.