Skip to content

Commit f9668ae

Browse files
stevenschlanskertomix26
authored andcommitted
Clean up socket resource closure
1 parent d745fde commit f9668ae

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

src/main/java/io/zonky/test/db/postgres/embedded/EmbeddedPostgres.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.net.InetSocketAddress;
2525
import java.net.ServerSocket;
2626
import java.net.Socket;
27-
import java.net.SocketException;
28-
import java.net.UnknownHostException;
2927
import java.nio.ByteBuffer;
3028
import java.nio.channels.AsynchronousFileChannel;
3129
import java.nio.channels.Channel;
@@ -328,28 +326,12 @@ private void waitForServerStartup(StopWatch watch) throws IOException
328326

329327
private void verifyReady() throws SQLException
330328
{
331-
final InetAddress localhost;
332-
try {
333-
localhost = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
334-
} catch (final UnknownHostException e) {
335-
throw new AssertionError("localhost unknown?", e);
336-
}
337-
final Socket sock = new Socket();
338-
try {
329+
final InetAddress localhost = InetAddress.getLoopbackAddress();
330+
try (Socket sock = new Socket()) {
339331
sock.setSoTimeout((int) Duration.ofMillis(500).toMillis());
340-
} catch (final SocketException e) {
341-
throw new RuntimeException("error setting socket timeout", e);
342-
}
343-
try {
344332
sock.connect(new InetSocketAddress(localhost, port), (int) Duration.ofMillis(500).toMillis());
345333
} catch (final IOException e) {
346334
throw new SQLException("connect failed", e);
347-
} finally {
348-
try {
349-
sock.close();
350-
} catch (final IOException e) {
351-
LOG.trace("i/o exception closing test socket", e);
352-
}
353335
}
354336
try (Connection c = getPostgresDatabase().getConnection() ;
355337
Statement s = c.createStatement() ;

0 commit comments

Comments
 (0)