diff --git a/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java b/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java index 126a50ed7..c7016dc03 100644 --- a/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java +++ b/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java @@ -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 diff --git a/src/native/mqtt_connection.c b/src/native/mqtt_connection.c index 340f51614..b6f7c1c90 100644 --- a/src/native/mqtt_connection.c +++ b/src/native/mqtt_connection.c @@ -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( diff --git a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java index 98030d53c..b6f3f617e 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java +++ b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java @@ -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 connectionConfigTransformer = null; protected void setConnectionConfigTransformer(Consumer connectionConfigTransformer) { this.connectionConfigTransformer = connectionConfigTransformer; @@ -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)); @@ -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); } }; diff --git a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java index 342974ab9..300abd4d8 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java +++ b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java @@ -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; @@ -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); } } @@ -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); } } };