package org.conf4j.consul;

import com.orbitz.consul.Consul;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;

public class StandaloneConsul {

    private static final Logger logger = LoggerFactory.getLogger(StandaloneConsul.class);

    private final GenericContainer consulContainer;
    private Consul consul;

    public StandaloneConsul() {
        consulContainer = new GenericContainer("consul:0.9.0")
                .withCommand("agent -dev -client 0.0.0.0")
                .withExposedPorts(8500);
    }

    public void start() {
        consulContainer.start();
        consulContainer.followOutput(new Slf4jLogConsumer(logger).withPrefix("consul"));
        consul = Consul.builder()
                .withUrl(getConsulUrl())
                .build();
    }

    public void stop() {
        consul = null;
        consulContainer.stop();
    }

    public Consul getConsul() {
        if (consul == null) {
            throw new IllegalStateException("Standalone consul must be started before calling getConsul");
        }

        return consul;
    }

    public String getConsulUrl() {
        return String.format("http://localhost:%s", consulContainer.getMappedPort(8500));
    }

}

@@ -229,16 +229,155 @@
 sumer;%0A%0A
+import java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.atomic.AtomicReference;%0A%0Aimport static org.awaitility.Awaitility.await;%0A%0A
 public c
@@ -560,21 +560,64 @@
 ate 
+AtomicReference%3C
 Consul
+%3E
  consul
+ = new AtomicReference%3C%3E()
 ;%0A%0A 
@@ -977,50 +977,187 @@
     
-consul = Consul.builder()%0A                
+await(%22Consul is ready%22)%0A                .atMost(10, TimeUnit.SECONDS)%0A                .until(() -%3E %7B%0A                    try %7B%0A                        consul.set(Consul.builder()
 .wit
@@ -1176,16 +1176,26 @@
 ulUrl())
+.build());
 %0A       
@@ -1203,23 +1203,266 @@
         
-.build(
+        return true;%0A                    %7D catch (Exception e) %7B%0A                        logger.info(%22Failed to create consul client, probably consul is not ready yet%22, e);%0A                        return false;%0A                    %7D%0A                %7D
 );%0A    %7D
@@ -1600,16 +1600,22 @@
  (consul
+.get()
  == null
@@ -1732,24 +1732,24 @@
 %0A        %7D%0A%0A
-
         retu
@@ -1757,16 +1757,22 @@
 n consul
+.get()
 ;%0A    %7D%0A

Add wait until consul is ready to standalone consul