public void waitForState(String stateRegex, long timeout, long timeIntervalToProvokeRetry) {
        Pattern p = Pattern.compile(stateRegex);
        long startTime = System.currentTimeMillis();
        final long endTime = startTime + timeout;
        int iteration = 0;
        while (true) {
            ClusterState currentClusterState;
            synchronized(timer) {
                currentClusterState = current;

                if (currentClusterState != null) {
                    Matcher m = p.matcher(currentClusterState.toString());

                    if (m.matches()) {
                        return;
                    }
                }
                try {
                    if (timeIntervalToProvokeRetry == 0) {
                        var waitTime = Math.max(1, endTime - startTime);
                        timer.wait(waitTime);
                    } else {
                        if (++iteration % 10 == 0) {
                            timer.advanceTime(timeIntervalToProvokeRetry);
                        }
                        timer.wait(10);
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            if (System.currentTimeMillis() >= endTime) {
                throw new IllegalStateException("Timeout. Did not find a state matching " + stateRegex + " within timeout of " + timeout + " milliseconds. Current state is " + currentClusterState);
            }
        }
    }
WARNING: If timeIntervalToProvokeRetry is set != 0 that means time will be set far into the future
and thus hit various unintended timeout periods. Only auto-step time if this is a non-issue.
clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/testutils/StateWaiter.java