From 0dc5b6174ee29d03aa99b1897dc03e137960b820 Mon Sep 17 00:00:00 2001 From: CodegassW Date: Thu, 4 Apr 2024 11:23:03 -0400 Subject: [PATCH 1/3] Add Assertion to the SharedTimerTest --- .../com/microsoft/sqlserver/jdbc/SharedTimerTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java index 6aa8bff2e..e06b3c94f 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java @@ -5,14 +5,16 @@ import java.util.ArrayList; import java.util.concurrent.*; +import static org.junit.jupiter.api.Assertions.assertFalse; + class SharedTimerTest { @Test void getTimer() throws InterruptedException, ExecutionException, TimeoutException { final int iterations = 500; - ExecutorService executor = Executors.newFixedThreadPool(2); + try { ArrayList> futures = new ArrayList<>(iterations); for (int i = 0; i < iterations; i++) { @@ -22,6 +24,11 @@ void getTimer() throws InterruptedException, ExecutionException, TimeoutExceptio CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); } finally { executor.shutdown(); + if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS)) { + executor.shutdownNow(); + } } + + assertFalse(SharedTimer.isRunning(), "SharedTimer should be stopped after all references are removed."); } } From acd75d86bef46b1639341a3114ba781f5b6345bf Mon Sep 17 00:00:00 2001 From: CodegassW Date: Wed, 17 Apr 2024 14:01:12 -0400 Subject: [PATCH 2/3] Refactor SharedTimerTest with import and wait time, add error string to TestResource --- .../sqlserver/jdbc/SharedTimerTest.java | 56 ++++++++++--------- .../sqlserver/jdbc/TestResource.java | 1 + 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java index e06b3c94f..410dcdc3b 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java @@ -1,34 +1,40 @@ package com.microsoft.sqlserver.jdbc; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; import java.util.ArrayList; -import java.util.concurrent.*; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.Test; class SharedTimerTest { - @Test - void getTimer() throws InterruptedException, ExecutionException, TimeoutException { - final int iterations = 500; - ExecutorService executor = Executors.newFixedThreadPool(2); - - try { - ArrayList> futures = new ArrayList<>(iterations); - for (int i = 0; i < iterations; i++) { - futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); - } - - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); - } finally { - executor.shutdown(); - if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS)) { - executor.shutdownNow(); - } - } - - assertFalse(SharedTimer.isRunning(), "SharedTimer should be stopped after all references are removed."); - } + @Test + void getTimer() throws InterruptedException, ExecutionException, TimeoutException { + final int iterations = 500; + ExecutorService executor = Executors.newFixedThreadPool(2); + + try { + ArrayList> futures = new ArrayList<>(iterations); + for (int i = 0; i < iterations; i++) { + futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); + } + + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); + } finally { + executor.shutdown(); + // 5000ms wait time for the AzureDB connection to close, need full test in the + // test lab for the exact time + if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) { + executor.shutdownNow(); + } + } + + assertFalse(SharedTimer.isRunning(), TestResource.getResource("R_sharedTimerStopOnNoRef")); + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java b/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java index aa4252f95..b08190fc9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java @@ -213,6 +213,7 @@ protected Object[][] getContents() { {"R_objectNullOrEmpty", "The {0} is null or empty."}, {"R_cekDecryptionFailed", "Failed to decrypt a column encryption key using key store provider: {0}."}, {"R_connectTimedOut", "connect timed out"}, + {"R_sharedTimerStopOnNoRef","SharedTimer should be stopped after all references are removed."}, {"R_sessionKilled", "Cannot continue the execution because the session is in the kill state"}, {"R_failedFedauth", "Failed to acquire fedauth token: "}, {"R_noLoginModulesConfiguredForJdbcDriver", From 24944b88f0d7fc2babaf6b092de87860bef676f4 Mon Sep 17 00:00:00 2001 From: Jeff Wasty Date: Fri, 26 Apr 2024 11:00:20 -0700 Subject: [PATCH 3/3] Format --- .../sqlserver/jdbc/SharedTimerTest.java | 47 ++++++++++--------- .../sqlserver/jdbc/TestResource.java | 5 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java index 410dcdc3b..48a70dd29 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/SharedTimerTest.java @@ -12,29 +12,30 @@ import org.junit.jupiter.api.Test; + class SharedTimerTest { - @Test - void getTimer() throws InterruptedException, ExecutionException, TimeoutException { - final int iterations = 500; - ExecutorService executor = Executors.newFixedThreadPool(2); - - try { - ArrayList> futures = new ArrayList<>(iterations); - for (int i = 0; i < iterations; i++) { - futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); - } - - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); - } finally { - executor.shutdown(); - // 5000ms wait time for the AzureDB connection to close, need full test in the - // test lab for the exact time - if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) { - executor.shutdownNow(); - } - } - - assertFalse(SharedTimer.isRunning(), TestResource.getResource("R_sharedTimerStopOnNoRef")); - } + @Test + void getTimer() throws InterruptedException, ExecutionException, TimeoutException { + final int iterations = 500; + ExecutorService executor = Executors.newFixedThreadPool(2); + + try { + ArrayList> futures = new ArrayList<>(iterations); + for (int i = 0; i < iterations; i++) { + futures.add(CompletableFuture.runAsync(() -> SharedTimer.getTimer().removeRef(), executor)); + } + + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(2, TimeUnit.MINUTES); + } finally { + executor.shutdown(); + // 5000ms wait time for the AzureDB connection to close, need full test in the + // test lab for the exact time + if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) { + executor.shutdownNow(); + } + } + + assertFalse(SharedTimer.isRunning(), TestResource.getResource("R_sharedTimerStopOnNoRef")); + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java b/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java index b08190fc9..03109ff93 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java @@ -66,8 +66,7 @@ protected Object[][] getContents() { {"R_invalidClientSecret", "AADSTS7000215: Invalid client secret provided"}, {"R_invalidCertFields", "Error reading certificate, please verify the location of the certificate.signed fields invalid"}, - {"R_invalidAADAuth", - "Failed to authenticate the user {0} in Active Directory (Authentication={1})"}, + {"R_invalidAADAuth", "Failed to authenticate the user {0} in Active Directory (Authentication={1})"}, {"R_failedValidate", "failed to validate values in $0} "}, {"R_tableNotDropped", "table not dropped. "}, {"R_connectionReset", "Connection reset"}, {"R_unknownException", "Unknown exception"}, {"R_deadConnection", "Dead connection should be invalid"}, @@ -213,7 +212,7 @@ protected Object[][] getContents() { {"R_objectNullOrEmpty", "The {0} is null or empty."}, {"R_cekDecryptionFailed", "Failed to decrypt a column encryption key using key store provider: {0}."}, {"R_connectTimedOut", "connect timed out"}, - {"R_sharedTimerStopOnNoRef","SharedTimer should be stopped after all references are removed."}, + {"R_sharedTimerStopOnNoRef", "SharedTimer should be stopped after all references are removed."}, {"R_sessionKilled", "Cannot continue the execution because the session is in the kill state"}, {"R_failedFedauth", "Failed to acquire fedauth token: "}, {"R_noLoginModulesConfiguredForJdbcDriver",