55
66package io .opentelemetry .instrumentation .test .utils ;
77
8- import java .io .IOException ;
9- import java .net .Socket ;
10- import java .util .concurrent .TimeUnit ;
11-
128public final class PortUtils {
139
1410 public static final int UNUSABLE_PORT = 61 ;
@@ -25,59 +21,5 @@ public static int findOpenPort() {
2521 return portAllocator .getPort ();
2622 }
2723
28- @ SuppressWarnings ("AddressSelection" )
29- private static boolean isPortOpen (int port ) {
30- try (Socket socket = new Socket ((String ) null , port )) {
31- return true ;
32- } catch (IOException e ) {
33- return false ;
34- }
35- }
36-
37- public static void waitForPortToOpen (int port , long timeout , TimeUnit unit ) {
38- long waitUntil = System .currentTimeMillis () + unit .toMillis (timeout );
39-
40- while (System .currentTimeMillis () < waitUntil ) {
41- if (isPortOpen (port )) {
42- return ;
43- }
44-
45- try {
46- TimeUnit .MILLISECONDS .sleep (100 );
47- } catch (InterruptedException e ) {
48- Thread .currentThread ().interrupt ();
49- throw new IllegalStateException ("Interrupted while waiting for " + port + " to be opened" );
50- }
51- }
52-
53- throw new IllegalStateException ("Timed out waiting for port " + port + " to be opened" );
54- }
55-
56- public static void waitForPortToOpen (int port , long timeout , TimeUnit unit , Process process ) {
57- long waitUntil = System .currentTimeMillis () + unit .toMillis (timeout );
58-
59- while (System .currentTimeMillis () < waitUntil ) {
60- try {
61- Thread .sleep (100 );
62- } catch (InterruptedException e ) {
63- throw new IllegalStateException ("Interrupted while waiting for " + port + " to be opened" );
64- }
65-
66- // Note: we should have used `process.isAlive()` here but it is java8 only
67- try {
68- process .exitValue ();
69- throw new IllegalStateException ("Process died before port " + port + " was opened" );
70- } catch (IllegalThreadStateException e ) {
71- // process is still alive, things are good.
72- }
73-
74- if (isPortOpen (port )) {
75- return ;
76- }
77- }
78-
79- throw new IllegalStateException ("Timed out waiting for port " + port + " to be opened" );
80- }
81-
8224 private PortUtils () {}
8325}
0 commit comments