2525import java .net .URL ;
2626import java .time .Duration ;
2727
28- import static org .eclipse .jkube .kit .common .util .AsyncUtil .await ;
29- import static org .eclipse .jkube .kit .common .util .AsyncUtil .get ;
30-
3128@ Getter
3229public class TestOciServer implements Closeable {
3330
34- private static final Duration TIMEOUT = Duration .ofSeconds (5 );
31+ private static final Duration TIMEOUT = Duration .ofSeconds (10 );
32+ private static final Duration RETRY_INTERVAL = Duration .ofMillis (100 );
3533 private static final String DEFAULT_USER = "oci-user" ;
3634 private static final String DEFAULT_PASSWORD = "oci-password" ;
3735
@@ -46,7 +44,7 @@ private static HelmLib getHelmLib() {
4644
4745 private final String user ;
4846 private final String password ;
49- private final String url ;
47+ private String url ;
5048
5149 public TestOciServer () {
5250 this (DEFAULT_USER , DEFAULT_PASSWORD );
@@ -55,28 +53,69 @@ public TestOciServer() {
5553 public TestOciServer (String user , String password ) {
5654 this .user = user ;
5755 this .password = password ;
58- url = get (await (this ::startServer ).apply (this ::waitForServer ), TIMEOUT );
56+ }
57+
58+ /**
59+ * Starts the OCI server and waits until it is ready to accept connections.
60+ * This method blocks until the server is ready or the timeout is reached.
61+ *
62+ * @throws IllegalStateException if the server fails to start within the timeout period
63+ */
64+ public void start () {
65+ if (url != null ) {
66+ throw new IllegalStateException ("Server is already started" );
67+ }
68+
69+ url = getHelmLib ().RepoOciServerStart (
70+ new RepoServerOptions (null , user , password )
71+ ).out ;
72+
73+ waitForServerReady ();
5974 }
6075
6176 @ Override
6277 public void close () throws IOException {
63- // No effect yet https://github.com/manusa/helm-java/blob/f44a88ed1ad351b2b5a00b5e735deb5cb35b32f7/native/internal/helm/repotest.go#L138
64- getHelmLib ().RepoServerStop (url );
78+ if (url != null ) {
79+ // No effect yet https://github.com/manusa/helm-java/blob/f44a88ed1ad351b2b5a00b5e735deb5cb35b32f7/native/internal/helm/repotest.go#L138
80+ getHelmLib ().RepoServerStop (url );
81+ url = null ;
82+ }
6583 }
6684
67- private String startServer () {
68- return getHelmLib ().RepoOciServerStart (
69- new RepoServerOptions (null , user , password )
70- ).out ;
85+ private void waitForServerReady () {
86+ final long startTime = System .currentTimeMillis ();
87+ final long timeoutMillis = TIMEOUT .toMillis ();
88+ final long retryIntervalMillis = RETRY_INTERVAL .toMillis ();
89+
90+ while (System .currentTimeMillis () - startTime < timeoutMillis ) {
91+ if (isServerReady ()) {
92+ return ;
93+ }
94+
95+ try {
96+ Thread .sleep (retryIntervalMillis );
97+ } catch (InterruptedException e ) {
98+ Thread .currentThread ().interrupt ();
99+ throw new IllegalStateException ("Interrupted while waiting for OCI server to start" , e );
100+ }
101+ }
102+
103+ throw new IllegalStateException (
104+ String .format ("OCI server at %s failed to become ready within %d milliseconds" , url , timeoutMillis )
105+ );
71106 }
72107
73- private boolean waitForServer ( String serverUrl ) {
108+ private boolean isServerReady ( ) {
74109 try {
75- final HttpURLConnection connection = (HttpURLConnection ) new URL ("http://" + serverUrl + "/v2/" )
110+ final HttpURLConnection connection = (HttpURLConnection ) new URL ("http://" + url + "/v2/" )
76111 .openConnection ();
77112 connection .setRequestProperty ("Authorization" , "Basic " + Base64Util .encodeToString (String .join (":" , user , password )));
113+ connection .setConnectTimeout ((int ) RETRY_INTERVAL .toMillis ());
114+ connection .setReadTimeout ((int ) RETRY_INTERVAL .toMillis ());
78115 connection .connect ();
79- return connection .getResponseCode () == HttpURLConnection .HTTP_OK ;
116+ final int responseCode = connection .getResponseCode ();
117+ connection .disconnect ();
118+ return responseCode == HttpURLConnection .HTTP_OK ;
80119 } catch (IOException e ) {
81120 return false ;
82121 }
0 commit comments