Skip to content

Commit

Permalink
timewait in fe ut
Browse files Browse the repository at this point in the history
Signed-off-by: pengyu <[email protected]>
  • Loading branch information
py023 committed Feb 23, 2024
1 parent 18f7329 commit e936e07
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand Down Expand Up @@ -332,6 +335,26 @@ public static void initHttpServer() throws IllegalArgException, InterruptedExcep
socket.setReuseAddress(true);
HTTP_PORT = socket.getLocalPort();
URI = "http://localhost:" + HTTP_PORT + "/api/" + DB_NAME + "/" + TABLE_NAME;

// make sure socket's port enters TIME_WAIT state, so other ServerSocket(0) wouldn't pick up the same port
CountDownLatch latch = new CountDownLatch(1);
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", HTTP_PORT));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();

// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (Exception e) {
throw new IllegalStateException("Could not find a free TCP/IP port to start HTTP Server on");
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class BDBDebuggerTest {
private static final Logger LOG = LogManager.getLogger(BDBDebuggerTest.class);
Expand Down Expand Up @@ -93,6 +92,25 @@ private int findValidPort() {
} catch (SocketException e) {
LOG.info("The port {} is invalid and try another port", port);
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ private int findValidPort() {
} catch (SocketException e) {
LOG.info("The port {} is invalid and try another port", port);
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ private int findValidPort() {
} catch (SocketException e) {
LOG.info("The port {} is invalid and try another port", port);
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ private int findValidPort() {
} catch (SocketException e) {
LOG.info("The port {} is invalid and try another port", port);
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ private static int findValidPort() {
} catch (SocketException e) {
System.out.println("The port " + port + " is invalid and try another port.");
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port to start HTTP Server on");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,25 @@ public static int findValidPort() {
} catch (SocketException e) {
System.out.println("The port " + port + " is invalid and try another port.");
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port to start HTTP Server on");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,25 @@ public static int findValidPort() {
} catch (SocketException e) {
System.out.println("The port " + port + " is invalid and try another port.");
}

CountDownLatch latch = new CountDownLatch(1);
final int serverPort = port;
new Thread(() -> {
try (Socket clientSocket = new Socket()) {
clientSocket.connect(new InetSocketAddress("localhost", serverPort));
latch.await(); // Wait until the server closes the connection
} catch (IOException | InterruptedException e) {
// CHECKSTYLE IGNORE THIS LINE
}
}).start();
// Accept a connection from the client
try (Socket serverConn = socket.accept()) {
// FIXME: handle empty block
System.out.println("A client connected");
} catch (IOException e) {
// CHECKSTYLE IGNORE THIS LINE
}
latch.countDown(); // Signal that the server has closed the connection
} catch (IOException e) {
throw new IllegalStateException("Could not find a free TCP/IP port to start HTTP Server on");
}
Expand Down

0 comments on commit e936e07

Please sign in to comment.