Skip to content

Commit

Permalink
Misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 15, 2024
1 parent 1ae5b96 commit 6e80e70
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 51 deletions.
69 changes: 23 additions & 46 deletions src/test/org/firebirdsql/common/BlackholeServer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*
* $Id$
*
* Firebird Open Source J2EE Connector - JDBC Driver
* Firebird Open Source JDBC Driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
Expand All @@ -14,7 +12,7 @@
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a CVS history command.
* can be obtained from a source control history command.
*
* All rights reserved.
*/
Expand All @@ -26,9 +24,11 @@
import java.net.Socket;
import java.net.SocketTimeoutException;

import static java.lang.System.Logger.Level.ERROR;

/**
* Simple socket server that consumes everything sent to it, but never responds; for testing (connect) timeouts.
* <p>
* <p>
* Assumption of this implementation is that there will only be one
* client at a time!
* </p>
Expand All @@ -38,97 +38,74 @@ public final class BlackholeServer implements Runnable {
private volatile boolean active = true;
private ServerSocket server;
public static final int LIVENESS_TIMEOUT = 500;

/**
* Constructs the ServerSocket with an ephemeral port, this port can be retrieved using {@link #getPort()}.
*
*
* @throws IOException If an I/O error occurs when opening the socket.
*/
public BlackholeServer() throws IOException {
this(0);
}

/**
* Constructs the ServerSocket with the specified port.
*
*
* @throws IOException If an I/O error occurs when opening the socket.
*/
public BlackholeServer(int port) throws IOException {
server = new ServerSocket(port);
server = new ServerSocket(port, 1);
server.setSoTimeout(LIVENESS_TIMEOUT);
}

/**
* Stops the server after (at max) 2x the LIVENESS_TIMEOUT
*/
public void stop() {
active = false;
}

/**
* Port number
* @return The portnumber if connected, or -1 if not connected
*/
public int getPort() {
if (server == null) {
return -1;
} else {
return server.getLocalPort();
}
return server != null ? server.getLocalPort() : -1;
}

@Override
public void run() {
try {
try (var ignored = server) {
while (active) {
Socket socket = null;
try {
socket = server.accept();
try (Socket socket = server.accept()) {
readFromSocket(socket);
} catch (SocketTimeoutException e) {
// Expected timeout for liveness of checking active
continue;
}
readFromSocket(socket);
}
} catch (Exception e) {
e.printStackTrace();
System.getLogger(getClass().getName()).log(ERROR, "BlackHoleServer terminated with exception", e);
} finally {
try {
server.close();
} catch (Exception ex) {
// Ignore
ex.printStackTrace();
} finally {
server = null;
}
server = null;
}
}

private void readFromSocket(Socket socket) {
try {
try (var ignored = socket) {
socket.setSoTimeout(LIVENESS_TIMEOUT);
InputStream in = socket.getInputStream();
while (active) {
try {
if (in.read() == -1) {
break;
}
if (in.read() == -1) return;
} catch (SocketTimeoutException e) {
// Expected timeout for liveness of checking active
continue;
} catch (IOException ioe) {
// Other errors: end read
break;
return;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) socket.close();
} catch (Exception e) {
// Ignore
}
System.getLogger(getClass().getName()).log(ERROR, "readFromSocket terminated with exception", e);
}
}
}
12 changes: 8 additions & 4 deletions src/test/org/firebirdsql/common/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ private ConfigHelper() {
* when {@code connection} does not support {@code RDB$CONFIG} (i.e. Firebird 3.0 or earlier)
*/
public static String getConfigValue(Connection connection, String configOption) throws SQLException {
var supportInfo = FirebirdSupportInfo.supportInfoFor(connection);
if (!supportInfo.supportsRDB$CONFIG()) {
throw new UnsupportedOperationException("This Firebird server does not support RDB$CONFIG");
}
requireRDB$CONFIGSupport(connection);
try (var pstmt = connection.prepareStatement(
"select RDB$CONFIG_VALUE from RDB$CONFIG where RDB$CONFIG_NAME = ?")) {
pstmt.setString(1, configOption);
Expand All @@ -71,6 +68,13 @@ public static String getConfigValue(Connection connection, String configOption)
}
}

private static void requireRDB$CONFIGSupport(Connection connection) {
var supportInfo = FirebirdSupportInfo.supportInfoFor(connection);
if (!supportInfo.supportsRDB$CONFIG()) {
throw new UnsupportedOperationException("This Firebird server does not support RDB$CONFIG");
}
}

/**
* Retrieves the value of configuration option {@code configOption} from the {@code RDB$CONFIG} table as an optional
* int.
Expand Down
2 changes: 1 addition & 1 deletion src/test/org/firebirdsql/common/FBTestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class FBTestProperties {
try {
Class.forName(FBDriver.class.getName());
} catch (ClassNotFoundException ex) {
throw new ExceptionInInitializerError("No suitable driver.");
throw new ExceptionInInitializerError("Could not load FBDriver class");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/org/firebirdsql/common/SimpleServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void closeConnection() throws IOException {
/**
* Closes the open connection and the server socket.
*/
@Override
public void close() throws IOException {
try (serverSocket){
closeConnection();
Expand Down

0 comments on commit 6e80e70

Please sign in to comment.