This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbutils.git


The following commit(s) were added to refs/heads/master by this push:
     new 7ab9ae4  Fix unreliable asynchronous test
7ab9ae4 is described below

commit 7ab9ae4c0f722deb2a8d1b8a043c90aa3f8c8c92
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Mon Nov 25 10:39:52 2024 -0500

    Fix unreliable asynchronous test
---
 pom.xml                                             |  6 ++++++
 .../commons/dbutils/AsyncQueryRunnerTest.java       | 21 ++++++++++-----------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index 490fa98..9fc4c69 100644
--- a/pom.xml
+++ b/pom.xml
@@ -54,6 +54,12 @@
       <version>5.14.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.17.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <distributionManagement>
diff --git a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java 
b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
index 558871b..121be58 100644
--- a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
+++ b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
@@ -31,6 +31,8 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -39,6 +41,7 @@ import java.util.concurrent.TimeUnit;
 import javax.sql.DataSource;
 
 import org.apache.commons.dbutils.handlers.ArrayHandler;
+import org.apache.commons.lang3.stream.Streams;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -372,18 +375,14 @@ public class AsyncQueryRunnerTest {
 
     @Test
     public void testInsertUsesGivenQueryRunner() throws Exception {
-        final QueryRunner mockQueryRunner = mock(QueryRunner.class, 
org.mockito.Mockito.withSettings().verboseLogging() // debug for Continuum
-        );
+        final QueryRunner mockQueryRunner = mock(QueryRunner.class, 
org.mockito.Mockito.withSettings().verboseLogging()); // debug for Continuum
         runner = new AsyncQueryRunner(Executors.newSingleThreadExecutor(), 
mockQueryRunner);
-
-        runner.insert("1", handler);
-        runner.insert("2", handler, "param1");
-        runner.insert(conn, "3", handler);
-        runner.insert(conn, "4", handler, "param1");
-
-        // give the Executor time to submit all insert statements. Otherwise 
the following verify statements will fail from time to time.
-        TimeUnit.MILLISECONDS.sleep(50);
-
+        final List<Future<Object[]>> futures = new ArrayList<>();
+        futures.add(runner.insert("1", handler));
+        futures.add(runner.insert("2", handler, "param1"));
+        futures.add(runner.insert(conn, "3", handler));
+        futures.add(runner.insert(conn, "4", handler, "param1"));
+        Streams.failableStream(futures).forEach(f -> f.get(10, 
TimeUnit.SECONDS));
         verify(mockQueryRunner).insert("1", handler);
         verify(mockQueryRunner).insert("2", handler, "param1");
         verify(mockQueryRunner).insert(conn, "3", handler);

Reply via email to