Skip to content

Commit

Permalink
Fix unreliable asynchronous test
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 25, 2024
1 parent a94528e commit 7ab9ae4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
21 changes: 10 additions & 11 deletions src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
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;
Expand All @@ -39,6 +41,7 @@
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;
Expand Down Expand Up @@ -372,18 +375,14 @@ public void testGoodUpdatePmdTrue() throws Exception {

@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);
Expand Down

0 comments on commit 7ab9ae4

Please sign in to comment.