Skip to content

Commit

Permalink
feat: fix to not use random port
Browse files Browse the repository at this point in the history
  • Loading branch information
dachshu committed May 6, 2024
1 parent 12a536d commit bbc35b7
Showing 1 changed file with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.time.Duration;
import java.util.Random;

import com.linecorp.armeria.common.util.OsType;
import com.linecorp.armeria.common.util.SystemInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -64,10 +61,9 @@ protected void configure(ServerBuilder sb) {

@Test
void shouldDelayDisconnectByServerSideIfClientDoesNotHandleConnectionClose() throws IOException {
final Random random = new Random();
final short localPort = (short) random.nextInt(Short.MAX_VALUE + 1);
try (Socket socket = new Socket("127.0.0.1", server.httpPort(), null, localPort)) {
try (Socket socket = new Socket("127.0.0.1", server.httpPort())) {
socket.setSoTimeout(100000);
final int socketPort = socket.getLocalPort();
final PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.print("GET /close" + " HTTP/1.1\r\n");
writer.print("\r\n");
Expand Down Expand Up @@ -102,23 +98,19 @@ void shouldDelayDisconnectByServerSideIfClientDoesNotHandleConnectionClose() thr
defaultHttp1ConnectionCloseDelayMillis + 1000
);

// Additional test to check that the socket is in the CLOSED state
if (SystemInfo.osType() == OsType.LINUX || SystemInfo.osType() == OsType.MAC) {
socket.close();
try (Socket reuseSock = new Socket()) {
assertThatCode(() -> reuseSock.bind(new InetSocketAddress((InetAddress) null, localPort)))
.doesNotThrowAnyException();
}
socket.close();
try (Socket reuseSock = new Socket()) {
assertThatCode(() -> reuseSock.bind(new InetSocketAddress((InetAddress) null, socketPort)))
.doesNotThrowAnyException();
}
}
}

@Test
void shouldWaitForDisconnectByClientSideFirst() throws IOException {
final Random random = new Random();
final short localPort = (short) random.nextInt(Short.MAX_VALUE + 1);
try (Socket socket = new Socket("127.0.0.1", server.httpPort(), null, localPort)) {
try (Socket socket = new Socket("127.0.0.1", server.httpPort())) {
socket.setSoTimeout(100000);
final int socketPort = socket.getLocalPort();
final PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.print("GET /close" + " HTTP/1.1\r\n");
writer.print("\r\n");
Expand All @@ -143,16 +135,15 @@ void shouldWaitForDisconnectByClientSideFirst() throws IOException {
assertThat(hasConnectionClose).isTrue();
assertThat(server.server().numConnections()).isEqualTo(1);

// Additional test to check that the socket is in the TIMED_WAIT state
if (SystemInfo.osType() == OsType.LINUX || SystemInfo.osType() == OsType.MAC) {
socket.close();
try (Socket reuseSock = new Socket()) {
assertThatThrownBy(
() -> reuseSock.bind(new InetSocketAddress((InetAddress) null, localPort)))
.isInstanceOf(BindException.class)
.hasMessageContaining("Address already in use");
}
}
socket.close();
assertThatThrownBy(
() -> {
final Socket reuseSock = new Socket("127.0.0.1", server.httpPort(), null, socketPort);
reuseSock.close();
})
.isInstanceOf(BindException.class)
.hasMessageContaining("Address already in use");

await().untilAsserted(() -> assertThat(server.server().numConnections()).isZero());
}
}
Expand Down

0 comments on commit bbc35b7

Please sign in to comment.