private <T> T callWithRetriesAndTimeout(Callable<T> callable, Optional<Integer> retryCount) throws Exception { RetryerBuilder<T> retryerBuilder = RetryerBuilder.<T>newBuilder() .withAttemptTimeLimiter(AttemptTimeLimiters.<T>fixedTimeLimit(configuration.getDockerClientTimeLimitSeconds(), TimeUnit.SECONDS, executor)); if (retryCount.isPresent()) { retryerBuilder.withStopStrategy(StopStrategies.stopAfterAttempt(retryCount.get())); } return retryerBuilder.build().call(callable); } }
.withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(getFetchOffsetTimeoutSec(), TimeUnit.SECONDS)) .retryIfExceptionOfType(Exception.class)
@Test public void testFixedTimeLimitWithNoExecutorReusesThreads() throws Exception { Set<Long> threadsUsed = Collections.synchronizedSet(Sets.newHashSet()); Callable<Void> callable = () -> { threadsUsed.add(Thread.currentThread().getId()); return null; }; int iterations = 20; for (int i = 0; i < iterations; i++) { AttemptTimeLimiter timeLimiter = AttemptTimeLimiters.fixedTimeLimit(1, TimeUnit.SECONDS); timeLimiter.call(callable); } assertTrue("Should have used less than " + iterations + " threads", threadsUsed.size() < iterations); }