public void testCallWithTimeout_goodCallableWithNotEnoughTime() throws Exception { try { service.callWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (TimeoutException expected) { } }
public void testCallWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
@Override public void train(Dataset dataset) { params.svm_type = getLibsvmType(); svm_problem problem = toSvmProblem(dataset); ExecutorService service = newCachedThreadPool(threadsNamed("libsvm-trainer-" + System.identityHashCode(this) + "-%s")); try { TimeLimiter limiter = SimpleTimeLimiter.create(service); //TODO: this time limit should be configurable model = limiter.callWithTimeout(getTrainingFunction(problem, params), 1, TimeUnit.HOURS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause != null) { throwIfUnchecked(cause); throw new RuntimeException(cause); } } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } finally { service.shutdownNow(); } }
@Nullable private Stream matchWithTimeOut(final Message message, long timeout, TimeUnit unit) { Stream matchedStream = null; try (final Timer.Context ignored = streamMetrics.getExecutionTimer(streamId, streamRuleId).time()) { matchedStream = timeLimiter.callWithTimeout(new Callable<Stream>() { @Override @Nullable public Stream call() throws Exception { return match(message); } }, timeout, unit); } catch (UncheckedTimeoutException e) { streamFaultManager.registerFailure(stream); } catch (Exception e) { LOG.warn("Unexpected error during stream matching", e); streamMetrics.markExceptionMeter(streamId); } return matchedStream; }
public void testCallWithTimeout_badCallableWithEnoughTime() throws Exception { try { service.callWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleException.class); } }
public void testCallWithTimeout_propagatesReturnValue() throws Exception { String result = timeLimiter.callWithTimeout( Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS); assertThat(result).isEqualTo(RETURN_VALUE); }
public void testCallWithTimeout_wrapsUncheckedException() throws Exception { Exception exception = new RuntimeException("test"); try { timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS); fail("Expected UncheckedExecutionException"); } catch (UncheckedExecutionException e) { assertThat(e.getCause()).isEqualTo(exception); } }
public void testCallWithTimeout_wrapsCheckedException() throws Exception { Exception exception = new SampleCheckedException(); try { timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertThat(e.getCause()).isEqualTo(exception); } }
@Before public void setUp() throws Exception { when(timingOutLimiter.callWithTimeout(any(), anyLong(), any())).thenThrow(new TimeoutException()); when(interruptingLimiter.callWithTimeout(any(), anyLong(), any())).thenThrow(new InterruptedException()); when(throwingLimiter.callWithTimeout(any(), anyLong(), any())).thenThrow(new IllegalStateException()); }
private <T> T callWithTimeLimit(Callable<T> callable, LockRequestSpecification specification) throws InterruptedException { try { return timeLimiter.callWithTimeout(callable, blockingTimeLimitMillis, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { // In this case, the thread was interrupted for some other reason, perhaps because we lost leadership. log.info("Lock service was interrupted when servicing {} for client \"{}\"; request was {}", SafeArg.of("method", specification.method()), SafeArg.of("client", specification.client()), UnsafeArg.of("lockRequest", specification.lockRequest()), e); throw e; } catch (TimeoutException e) { // This is the legitimate timeout case we're trying to catch. throw logAndHandleTimeout(specification); } catch (Exception e) { // We don't know, and would prefer not to throw checked exceptions apart from InterruptedException. throw Throwables.propagate(e); } }
@Override public V call(Callable<V> callable) throws Exception { return timeLimiter.callWithTimeout(callable, duration, timeUnit, true); } }
@Override public void poll() { logger.info("polling entries from queue " + pollRequest.getQueue()); try { timeLimiter.callWithTimeout(new ProcessingFunction(), timeout, timeunit); } catch (Exception e) { logger.log(Level.WARNING, "[" + name + "] Error executing poll request " + pollRequest, e); } }
TimeLimiter limiter = new SimpleTimeLimiter(); String result = limiter.callWithTimeout(new Callable<String>() { public String call() { return getStaticJsonResponse("build", "post"); } }, 3, TimeUnit.SECONDS, false);
public void testCallWithTimeout_goodCallableWithNotEnoughTime() throws Exception { try { service.callWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (TimeoutException expected) { } }
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000L)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config); invokeHttpMethod.apply(get); }
public void testTimeLimitedRunsFallbackCreateOrPropagate() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(config.getTimeoutNanos(asyncGet)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andThrow(exception); expect(fallback.createOrPropagate(exception)).andReturn(fallbackResponse); replay(http, timeLimiter, fallback, config, future); assertEquals(invokeHttpMethod.apply(get), fallbackResponse); }
public void testCallWithTimeout_propagatesReturnValue() throws Exception { String result = timeLimiter.callWithTimeout( Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS); assertThat(result).isEqualTo(RETURN_VALUE); }
public void testTimeLimitedRunsFallbackCreateOrPropagate() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andThrow(exception); expect(fallback.createOrPropagate(exception)).andReturn(fallbackResponse); replay(http, timeLimiter, fallback, config, future); assertEquals(invokeHttpMethod.apply(get), fallbackResponse); } }