From a22d9f1fe902e27feb658b6815e50938765b5282 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Tue, 6 Feb 2024 16:45:50 -0800 Subject: [PATCH 1/5] Fix duplicated onConnectionSuccess and onConenctionFailure calls --- .../amazon/awssdk/crt/mqtt/MqttClientConnection.java | 11 ----------- 1 file changed, 11 deletions(-) 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 From 9e159d331dbbbce8835eb1222a91ddddf1b2d44b Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 7 Feb 2024 11:04:03 -0800 Subject: [PATCH 2/5] Add tests --- .../awssdk/crt/mqtt/MqttClientConnection.java | 11 +++++++++++ .../crt/test/MqttClientConnectionFixture.java | 15 +++++++++++++++ .../awssdk/crt/test/MqttClientConnectionTest.java | 7 +++++++ 3 files changed, 33 insertions(+) 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 c7016dc03..126a50ed7 100644 --- a/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java +++ b/src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java @@ -226,6 +226,17 @@ 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/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..4d3c92a7a 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(); + + // Check that onConnectionSuccess was called only once. + assertEquals("onConnectionSuccess was called more than once", 1, connectionEventsStatistics.onConnectionSuccessCalled); } } @@ -212,6 +216,9 @@ public void testConnectDisconnectEventsUnhappy() { fail(ex.toString()); } close(); + + // Check that onConnectionFailure was called only once. + assertEquals("onConnectionFailure was called more than once", 1, connectionEventsStatistics.onConnectionFailureCalled); } } }; From 24679ac23f291fa9fef76f89426b90aef27c85b7 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 7 Feb 2024 11:09:54 -0800 Subject: [PATCH 3/5] Fix tests --- .../amazon/awssdk/crt/test/MqttClientConnectionTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 4d3c92a7a..300abd4d8 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java +++ b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionTest.java @@ -181,8 +181,8 @@ public void testConnectDisconnectEventsHappy() { } close(); - // Check that onConnectionSuccess was called only once. - assertEquals("onConnectionSuccess was called more than once", 1, connectionEventsStatistics.onConnectionSuccessCalled); + assertEquals("Unexpected onConnectionSuccess call: ", 1, connectionEventsStatistics.onConnectionSuccessCalled); + assertEquals("Unexpected onConnectionClosed call: ", 1, connectionEventsStatistics.onConnectionClosedCalled); } } @@ -217,8 +217,8 @@ public void testConnectDisconnectEventsUnhappy() { } close(); - // Check that onConnectionFailure was called only once. - assertEquals("onConnectionFailure was called more than once", 1, connectionEventsStatistics.onConnectionFailureCalled); + assertEquals("Unexpected onConnectionFailure call: ", 1, connectionEventsStatistics.onConnectionFailureCalled); + assertEquals("Unexpected onConnectionClosed call: ", 0, connectionEventsStatistics.onConnectionClosedCalled); } } }; From e6aa7731abe5d9e14065b1d61d472132f2138d0e Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 7 Feb 2024 11:10:05 -0800 Subject: [PATCH 4/5] Remove duplicated calls --- .../amazon/awssdk/crt/mqtt/MqttClientConnection.java | 11 ----------- 1 file changed, 11 deletions(-) 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 From 2cb421e49acf4a597a3d3f6bc02db32bc6110c1d Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 7 Feb 2024 11:59:05 -0800 Subject: [PATCH 5/5] Register on_connection_success/failure in mqtt5to3 adapter --- src/native/mqtt_connection.c | 2 ++ 1 file changed, 2 insertions(+) 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(