Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to start bigtable emulator on fixed port #1193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,6 +40,8 @@
/**
* Wraps the Bigtable emulator in a java api.
*
* Uses port-number from BIGTABLE_EMULATOR_HOST environment variable to create bundled emulator instances
*
* <p>This class will use the golang binaries embedded in this jar to launch the emulator as an
* external process and redirect its output to a {@link Logger}.
*/
Expand All @@ -52,6 +54,12 @@ public class Emulator {
private boolean isStopped = true;
private Thread shutdownHook;

//Set environment variable as <HOST>:<RANDOMPORT> Eg: 127.0.0.1:8465
private static final int PORT = Optional.ofNullable(System.getenv("BIGTABLE_EMULATOR_HOST"))
.filter(hostPort->hostPort.split(":").length>1)
.map(hostPort->hostPort.split(":")[1])
.map(Integer::parseInt).orElse(0);

private int port;
private ManagedChannel dataChannel;
private ManagedChannel adminChannel;
Expand Down Expand Up @@ -262,7 +270,7 @@ private static String getBundledResourcePath() {

/** Gets a random open port number. */
private static int getAvailablePort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
return serverSocket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("Failed to find open port");
Expand Down