/** * Waits the given amount of seconds. * <p> * If the given value is 0 or negative, the method returns immediately. If an <tt>InterruptedException</tt> * is thrown while waiting, the method will return immediately but ignore the exception. * * @param secondsToWait the number of seconds to wait */ public static void seconds(double secondsToWait) { millis((int) Math.round(1000 * secondsToWait)); }
/** * Waits the given amount of time. * <p> * If the given value is <tt>null</tt>, the method returns immediately. If an <tt>InterruptedException</tt> * is thrown while waiting, the method will return immediately but ignore the exception. * * @param durationToWait the duration to wait */ public static void duration(@Nullable Duration durationToWait) { if (durationToWait != null) { seconds(durationToWait.getSeconds()); } }
/** * Waits for a random amount of seconds within the given bounds. * <p> * See {@link #randomMillis(int, int)} for a detailed description on using a negative lower bound. * * @param minWaitSeconds the minimal time to wait in seconds. * @param maxWaitSeconds the maximal time to wait in seconds. This must be >= minWaitSeconds and also > 0 * or the * method will always return immediately */ public static void randomSeconds(double minWaitSeconds, double maxWaitSeconds) { randomMillis((int) Math.round(1000 * minWaitSeconds), (int) Math.round(1000 * maxWaitSeconds)); } }
Wait.millis((2 - retries) * 500); Wait.randomMillis(-500, 500); } catch (HandledException e) { throw e;
/** * Manually blocks the current thread for n seconds, to make e.g. a bulk write visible in ES. * <p> * Consider using {@link #callAfterUpdate(Runnable)} which does not block system resources. Only use this method * if absolutely necessary. * * @param seconds the number of seconds to block */ public void blockThreadForUpdate(int seconds) { blocks.inc(); Wait.seconds(seconds); }
/** * Waits for an random amount of millisecond within the given bounds. * <p> * Note that minWaitMillis may be negative. This can be used to only block the thread in a given percentage of * all calls. So if the thread should wait between 0 and 500ms in 50% of all calls * {@code randomMillis(-500, 500)} can be invoked. Using this bounds, the expected wait time will be * between -500ms and 0ms in 50% of all calls (on average). As negative delays are ignored, the method * will return immediately. * * @param minWaitMillis the minimal time to wait in millis. * @param maxWaitMillis the maximal time to wait in millis. This must be >= minWaitMillis and also > 0 or the * method will always return immediately */ public static void randomMillis(int minWaitMillis, int maxWaitMillis) { if (minWaitMillis > maxWaitMillis || maxWaitMillis < 0) { return; } int pauseInMillis = minWaitMillis + rnd.nextInt(maxWaitMillis - minWaitMillis); millis(pauseInMillis); }
private void awaitContainerStart(Container container) { LOG.INFO("Waiting for '%s' to become ready...", container.getContainerName()); try { int retries = MAX_WAIT_SECONDS; while (container.areAllPortsOpen().failed()) { Wait.seconds(1); if (retries-- <= 0) { LOG.WARN("Failed to start '%s' - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); return; } } LOG.INFO("Container '%s' is ONLINE - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); } catch (Exception e) { LOG.SEVERE(e); } }
Wait.millis(waitInMillis); waitInMillis = Math.min(1500, waitInMillis + 500); } while (System.currentTimeMillis() < timeout);
private void waitForElasticsearchToBecomReady() { int retries = 15; while (retries-- > 0) { try { if (client.getRestClient().performRequest("GET", "/_cat/indices").getStatusLine().getStatusCode() == 200) { return; } } catch (Exception e) { Exceptions.ignore(e); } Elastic.LOG.INFO("Sleeping two seconds to wait until Elasticsearch is ready..."); Wait.seconds(2); } Elastic.LOG.WARN("Elasticsearch was not ready after waiting 30s..."); }
private void subscribe(Subscriber subscriber, JedisPubSub subscription) { while (subscriptionsActive.get()) { try (Jedis redis = getConnection()) { LOG.INFO("Starting subscription for: %s", subscriber.getTopic()); redis.subscribe(subscription, subscriber.getTopic()); if (subscriptionsActive.get()) { Wait.seconds(5); } } catch (Exception e) { Exceptions.handle() .to(LOG) .error(e) .withSystemErrorMessage("Failed to subscribe to a topic: %s (%s)") .handle(); Wait.seconds(1); } } LOG.INFO("Terminated subscription for: %s", subscriber.getTopic()); }
/** * When executing several scenarios via Docker, we observed, that especially MySQL isn't entirely ready, * when then port 3306 is open. Therefore we try to establish a real connection (with up to 5 retries * in a one...five second interval). */ private void waitForDatabaseToBecomeReady(String realm, String initSql) { if (Strings.isEmpty(initSql)) { return; } int retries = 5; int waitInSeconds = 1; while (retries-- > 0) { Database database = getDatabase(realm); try (Connection connection = database.getHostConnection()) { executeInitialStatement(database, initSql, connection); return; } catch (SQLException e) { Exceptions.ignore(e); Wait.seconds(waitInSeconds++); } } }