package com.centurylink.cloud.sdk.common.management.services.domain.queue.future.job.waiting;

import com.google.common.base.Throwables;

import java.util.function.Consumer;
import java.util.function.Supplier;

import static java.lang.Boolean.TRUE;

/**
 * @author Ilya Drabenia
 */
public class SingleWaitingLoop implements WaitingLoop {
    public static final long STATUS_POLLING_DELAY = 400L;

    private final Consumer<Void> onIterationStarted;
    private final Supplier<Boolean> checkStatus;

    public SingleWaitingLoop(Supplier<Boolean> checkStatus) {
        this(checkStatus, e -> { });
    }

    private SingleWaitingLoop(Supplier<Boolean> checkStatus, Consumer<Void> onIterationStarted) {
        this.checkStatus = checkStatus;
        this.onIterationStarted = onIterationStarted;
    }

    @Override
    public WaitingLoop onIterationStarted(Consumer<Void> listener) {
        return new SingleWaitingLoop(checkStatus, onIterationStarted.andThen(listener));
    }

    @Override
    public Void get() {
        for (;;) {
            onIterationStarted.accept(null);

            if (TRUE.equals(checkStatus.get())) {
                sleep();
                return null;
            }

            sleep();
        }
    }

    private void sleep() {
        try {
            Thread.sleep(STATUS_POLLING_DELAY);
        } catch (InterruptedException ex) {
            throw Throwables.propagate(ex);
        }
    }

}

@@ -1139,37 +1139,64 @@
                 
-sleep
+waitingForBackendConsistentState
 ();%0A            
@@ -1257,32 +1257,144 @@
        %7D%0A    %7D%0A%0A
+    private void waitingForBackendConsistentState() %7B%0A        sleep();%0A        sleep();%0A        sleep();%0A    %7D%0A%0A
     private void

Implement REST client retries possibilities #10