From 1cc7ecdd7703c04a4732a1af6bc3fa3417a9ca64 Mon Sep 17 00:00:00 2001 From: Steve Kim <86316075+sbSteveK@users.noreply.github.com> Date: Mon, 11 Sep 2023 08:54:24 -0700 Subject: [PATCH] Release native references in failed tests (#677) Some tests were not cleaning up native references because close() was being called in a failing try block. These calls have been moved to a finally block to insure native references get cleaned up so tests don't time out after failing. --- .../awssdk/crt/test/CrtTestFixture.java | 21 ++++++++------- .../crt/test/MqttClientConnectionFixture.java | 4 ++- .../test/MqttClientConnectionMethodTest.java | 26 +++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java b/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java index ee79c466e..98046bb3a 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java +++ b/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java @@ -42,6 +42,9 @@ private void SetPropertyFromEnv(String name){ // Setup System properties from environment variables set by builder for use by unit tests. private void SetupTestProperties(){ + // Indicate that the system properties have been setup + System.setProperty("are.test.properties.setup", "true"); + SetPropertyFromEnv("AWS_TEST_IS_CI"); SetPropertyFromEnv("AWS_TEST_MQTT311_ROOT_CA"); SetPropertyFromEnv("ENDPOINT"); @@ -216,12 +219,16 @@ public void setup() { } Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup begin"); + // TODO this CrtTestContext should be removed as we are using System Properties for tests now. context = new CrtTestContext(); - CrtPlatform platform = CRT.getPlatformImpl(); - if (platform != null) { - platform.testSetup(context); - } else { - SetupTestProperties(); + // System properties for tests only need to be setup once + if (System.getProperty("are.test.properties.setup") != "true"){ + CrtPlatform platform = CRT.getPlatformImpl(); + if (platform != null) { + platform.testSetup(context); + } else { + SetupTestProperties(); + } } Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup end"); @@ -237,10 +244,6 @@ public void tearDown() { context = null; - EventLoopGroup.closeStaticDefault(); - HostResolver.closeStaticDefault(); - ClientBootstrap.closeStaticDefault(); - CrtResource.waitForNoResources(); if (CRT.getOSIdentifier() != "android") { try { 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 a4a07aa83..98030d53c 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java +++ b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionFixture.java @@ -348,7 +348,9 @@ public void onConnectionClosed(OnConnectionClosedReturn data) { connected.get(); result = true; } - client.close(); + finally { + client.close(); + } } catch (Exception ex) { fail("Exception during connect: " + ex.toString()); diff --git a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionMethodTest.java b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionMethodTest.java index 8e67465ec..6ff650d0a 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionMethodTest.java +++ b/src/test/java/software/amazon/awssdk/crt/test/MqttClientConnectionMethodTest.java @@ -67,6 +67,8 @@ public void ConnDC_Cred_UC1() null, null); disconnect(); + } + finally { close(); } } @@ -98,6 +100,8 @@ public void ConnDC_Cred_UC2() null, null); disconnect(); + } + finally { close(); } } @@ -122,6 +126,8 @@ public void ConnDC_Cred_UC3() null, null); disconnect(); + } + finally { close(); } } @@ -157,6 +163,8 @@ public void ConnDC_Cred_UC4() null, null); disconnect(); + } + finally { close(); } } @@ -187,6 +195,8 @@ public void ConnWS_Cred_UC1() CredentialsProvider provider = builder.build();) { connectWebsocketsWithCredentialsProvider(provider, AWS_TEST_MQTT311_IOT_CORE_HOST, 443, tlsContext, null, null, null); disconnect(); + } + finally { close(); } } @@ -208,6 +218,8 @@ public void ConnWS_Cred_UC2() CredentialsProvider provider = builder.build();) { connectWebsocketsWithCredentialsProvider(provider, AWS_TEST_MQTT311_IOT_CORE_HOST, 443, tlsContext, null, null, null); disconnect(); + } + finally { close(); } } @@ -237,6 +249,8 @@ public void ConnWS_Cred_UC3() CredentialsProvider provider = builder.build();) { connectWebsocketsWithCredentialsProvider(provider, AWS_TEST_MQTT311_IOT_CORE_HOST, 443, tlsContext, null, null, null); disconnect(); + } + finally { close(); } } @@ -270,6 +284,8 @@ public void ConnWS_Cred_UC4() CredentialsProvider provider = builder.build();) { connectWebsocketsWithCredentialsProvider(provider, AWS_TEST_MQTT311_IOT_CORE_HOST, 443, tlsContext, null, null, null); disconnect(); + } + finally { close(); } } @@ -336,6 +352,8 @@ public void ConnDC_UC3() null, null); disconnect(); + } + finally { close(); } } @@ -369,6 +387,8 @@ public void ConnDC_UC4() null, null); disconnect(); + } + finally { close(); } } @@ -400,6 +420,8 @@ public void ConnDC_UC5() null, proxyOptions); disconnect(); + } + finally { close(); } } @@ -470,6 +492,8 @@ public void ConnWS_UC3() null, null); disconnect(); + } + finally { close(); } } @@ -501,6 +525,8 @@ public void ConnWS_UC4() null, proxyOptions); disconnect(); + } + finally { close(); } }