Skip to content

Commit

Permalink
use TestSocketUtils.findAvailableTcpPort
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Dec 1, 2024
1 parent e2697b2 commit 3e34e53
Showing 1 changed file with 3 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package grpcstarter.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.util.TestSocketUtils.findAvailableTcpPort;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.protobuf.SimpleRequest;
import io.grpc.testing.protobuf.SimpleResponse;
import io.grpc.testing.protobuf.SimpleServiceGrpc;
import java.net.ServerSocket;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -24,8 +23,8 @@
*/
class DynamicRefreshTests {

static int port1 = getRandomPort();
static int port2 = getRandomPort();
static int port1 = findAvailableTcpPort();
static int port2 = findAvailableTcpPort();
static Server server1;
static Server server2;

Expand Down Expand Up @@ -89,11 +88,4 @@ public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> respo
})
.build();
}

@SneakyThrows
private static int getRandomPort() {
try (ServerSocket ss = new ServerSocket(0)) {
return ss.getLocalPort();
}
}
}

0 comments on commit 3e34e53

Please sign in to comment.