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

Delete unused methods #13189

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package io.opentelemetry.instrumentation.test.utils;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

public final class PortUtils {

public static final int UNUSABLE_PORT = 61;
Expand All @@ -25,59 +21,5 @@ public static int findOpenPort() {
return portAllocator.getPort();
}

@SuppressWarnings("AddressSelection")
private static boolean isPortOpen(int port) {
try (Socket socket = new Socket((String) null, port)) {
return true;
} catch (IOException e) {
return false;
}
}

public static void waitForPortToOpen(int port, long timeout, TimeUnit unit) {
long waitUntil = System.currentTimeMillis() + unit.toMillis(timeout);

while (System.currentTimeMillis() < waitUntil) {
if (isPortOpen(port)) {
return;
}

try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while waiting for " + port + " to be opened");
}
}

throw new IllegalStateException("Timed out waiting for port " + port + " to be opened");
}

public static void waitForPortToOpen(int port, long timeout, TimeUnit unit, Process process) {
long waitUntil = System.currentTimeMillis() + unit.toMillis(timeout);

while (System.currentTimeMillis() < waitUntil) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted while waiting for " + port + " to be opened");
}

// Note: we should have used `process.isAlive()` here but it is java8 only
try {
process.exitValue();
throw new IllegalStateException("Process died before port " + port + " was opened");
} catch (IllegalThreadStateException e) {
// process is still alive, things are good.
}

if (isPortOpen(port)) {
return;
}
}

throw new IllegalStateException("Timed out waiting for port " + port + " to be opened");
}

private PortUtils() {}
}