Skip to content

Commit 9d7bb0f

Browse files
committed
Remove unnecessary concurrency in test
1 parent 9177bae commit 9d7bb0f

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/test/org/firebirdsql/jdbc/FBConnectionTest.java

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
import org.firebirdsql.jaybird.xca.FBManagedConnection;
3838
import org.junit.jupiter.api.Test;
3939
import org.junit.jupiter.api.Timeout;
40+
import org.junit.jupiter.api.extension.ExtendWith;
4041
import org.junit.jupiter.api.extension.RegisterExtension;
4142
import org.junit.jupiter.params.ParameterizedTest;
4243
import org.junit.jupiter.params.provider.EnumSource;
4344
import org.junit.jupiter.params.provider.ValueSource;
45+
import org.mockito.Mock;
46+
import org.mockito.junit.jupiter.MockitoExtension;
4447

4548
import java.nio.charset.StandardCharsets;
4649
import java.sql.*;
@@ -73,6 +76,7 @@
7376
* @author Roman Rokytskyy
7477
* @author Mark Rotteveel
7578
*/
79+
@ExtendWith(MockitoExtension.class)
7680
class FBConnectionTest {
7781

7882
private static final String CREATE_TABLE = """
@@ -784,27 +788,23 @@ void legacyAuthUserCannotConnectByDefault() throws Exception {
784788

785789
@Test
786790
@Timeout(10)
787-
void setNetworkTimeout_isUsed() throws Exception {
791+
void setNetworkTimeout_isUsed(@Mock ExecutorService executor) throws Exception {
788792
assumeThat("Test assumes pure Java implementation (native doesn't support setNetworkTimeout)",
789793
GDS_TYPE, isPureJavaType());
790-
ExecutorService executor = Executors.newSingleThreadExecutor();
791-
792-
// Workaround for bug with TPBMapper being shared (has conflict with testTpbMapping)
793-
Properties props = getDefaultPropertiesForConnection();
794-
props.put("TRANSACTION_READ_COMMITTED", "read_committed,rec_version,write,wait");
795-
try (Connection connection1 = getConnectionViaDriverManager();
796-
Statement statement1 = connection1.createStatement();
797-
Connection connection2 = DriverManager.getConnection(getUrl(), props)) {
794+
try (var connection1 = getConnectionViaDriverManager();
795+
var statement1 = connection1.createStatement();
796+
var connection2 = getConnectionViaDriverManager(
797+
"TRANSACTION_READ_COMMITTED", "read_committed,rec_version,write,wait")) {
798798
executeCreateTable(connection1, "create table locking (id integer, colval varchar(50))");
799799
statement1.execute("insert into locking(id, colval) values (1, 'abc')");
800800

801-
try (ResultSet rs1 = statement1.executeQuery("select id, colval from locking with lock")) {
801+
try (var rs1 = statement1.executeQuery("select id, colval from locking with lock")) {
802802
rs1.next();
803803

804804
connection2.setNetworkTimeout(executor, 50);
805805
long start = 0;
806-
try (Statement statement2 = connection2.createStatement();
807-
ResultSet rs2 = statement2.executeQuery("select id, colval from locking with lock")) {
806+
try (var statement2 = connection2.createStatement();
807+
var rs2 = statement2.executeQuery("select id, colval from locking with lock")) {
808808
start = System.currentTimeMillis();
809809
// Fetch will block waiting for lock held by statement1/rs1 to be released
810810
SQLException exception = assertThrows(SQLException.class, rs2::next);
@@ -818,20 +818,14 @@ void setNetworkTimeout_isUsed() throws Exception {
818818
}
819819

820820
assertTrue(connection2.isClosed(), "Expected connection2 to be closed by timeout");
821-
} finally {
822-
executor.shutdown();
823821
}
824822
}
825823

826824
@Test
827-
void setNetworkTimeout_invalidTimeout() throws Exception {
828-
ExecutorService executorService = Executors.newSingleThreadExecutor();
829-
try (Connection connection = getConnectionViaDriverManager()) {
830-
SQLException exception = assertThrows(SQLException.class,
831-
() -> connection.setNetworkTimeout(executorService, -1));
825+
void setNetworkTimeout_invalidTimeout(@Mock ExecutorService executor) throws Exception {
826+
try (var connection = getConnectionViaDriverManager()) {
827+
var exception = assertThrows(SQLException.class, () -> connection.setNetworkTimeout(executor, -1));
832828
assertThat(exception, fbMessageStartsWith(JaybirdErrorCodes.jb_invalidTimeout));
833-
} finally {
834-
executorService.shutdown();
835829
}
836830
}
837831

@@ -844,42 +838,24 @@ void setNetworkTimeout_invalidExecutor() throws Exception {
844838
}
845839

846840
@Test
847-
void setNetworkTimeout_getAndSetSeries() throws Exception {
841+
void setNetworkTimeout_getAndSetSeries(@Mock ExecutorService executor) throws Exception {
848842
assumeThat("Type is pure Java", GDS_TYPE, isPureJavaType());
849-
ExecutorService executorService = Executors.newSingleThreadExecutor();
850-
try (Connection connection = getConnectionViaDriverManager()) {
843+
try (var connection = getConnectionViaDriverManager()) {
851844
assertEquals(0, connection.getNetworkTimeout(), "Expected 0 as initial network timeout");
852845

853-
connection.setNetworkTimeout(executorService, 500);
854-
final CyclicBarrier barrier = new CyclicBarrier(2);
855-
Runnable waitForBarrier = () -> {
856-
try {
857-
barrier.await(500, TimeUnit.MILLISECONDS);
858-
} catch (TimeoutException | InterruptedException | BrokenBarrierException e) {
859-
e.printStackTrace();
860-
}
861-
};
862-
executorService.execute(waitForBarrier);
863-
barrier.await(500, TimeUnit.MILLISECONDS);
846+
connection.setNetworkTimeout(executor, 500);
864847
assertEquals(500, connection.getNetworkTimeout(), "Unexpected getNetworkTimeout");
865848

866-
barrier.reset();
867-
connection.setNetworkTimeout(executorService, 0);
868-
executorService.execute(waitForBarrier);
869-
barrier.await(500, TimeUnit.MILLISECONDS);
849+
connection.setNetworkTimeout(executor, 0);
870850
assertEquals(0, connection.getNetworkTimeout(), "Unexpected getNetworkTimeout");
871-
} finally {
872-
executorService.shutdown();
873851
}
874852
}
875853

876854
@Test
877855
void testWireCompression() throws Exception {
878856
assumeThat("Test only works with pure java connections", GDS_TYPE, isPureJavaType());
879857
assumeTrue(getDefaultSupportInfo().supportsWireCompression(), "Test requires wire compression");
880-
Properties props = getDefaultPropertiesForConnection();
881-
props.setProperty("wireCompression", "true");
882-
try (Connection connection = DriverManager.getConnection(getUrl(), props)) {
858+
try (var connection = getConnectionViaDriverManager("wireCompression", "true")) {
883859
assertTrue(connection.isValid(0));
884860
GDSServerVersion serverVersion =
885861
connection.unwrap(FirebirdConnection.class).getFbDatabase().getServerVersion();

0 commit comments

Comments
 (0)