public void testCallWithTimeout_goodCallableWithNotEnoughTime() throws Exception { try { service.callWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (TimeoutException expected) { } }
private AtopProcess(Process process, Duration readTimeout, ExecutorService executor) { this.process = requireNonNull(process, "process is null"); underlyingReader = new BufferedReader(new InputStreamReader(process.getInputStream())); TimeLimiter limiter = SimpleTimeLimiter.create(executor); this.reader = limiter.newProxy(underlyingReader::readLine, LineReader.class, readTimeout.toMillis(), MILLISECONDS); try { // Ignore the first two lines, as they are an event since boot (RESET followed by event line) this.reader.readLine(); this.reader.readLine(); // Read the first real line line = this.reader.readLine(); } catch (IOException e) { line = null; } catch (UncheckedTimeoutException e) { throw new PrestoException(ATOP_READ_TIMEOUT, "Timeout reading from atop process"); } }
public void testCallUninterruptiblyWithTimeout_goodCallableWithNotEnoughTime() throws Exception { try { service.callUninterruptiblyWithTimeout(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)); }
public void testNewProxy_badMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); }
public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callUninterruptiblyWithTimeout(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(); } }
public void testNewProxy_goodMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); String result = proxy.sleepThenReturnInput("x"); assertThat(result).isEqualTo("x"); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); assertThat(target.finished).isTrue(); }
public void testCallUninterruptiblyWithTimeout_badCallableWithEnoughTime() throws Exception { try { service.callUninterruptiblyWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleException.class); } }
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 testNewProxy_badMethodWithEnoughTime() throws Exception { SampleImpl target = new SampleImpl(DELAY_MS); Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenThrowException(); fail("no exception thrown"); } catch (SampleException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
public void testCallUninterruptiblyWithTimeout_propagatesReturnValue() throws Exception { String result = timeLimiter.callUninterruptiblyWithTimeout( Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS); assertThat(result).isEqualTo(RETURN_VALUE); }
@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 testNewProxy_goodMethodWithNotEnoughTime() throws Exception { SampleImpl target = new SampleImpl(9999); Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS); Stopwatch stopwatch = Stopwatch.createStarted(); try { proxy.sleepThenReturnInput("x"); fail("no exception thrown"); } catch (UncheckedTimeoutException expected) { } assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2)); // Is it still computing away anyway? assertThat(target.finished).isFalse(); MILLISECONDS.sleep(ENOUGH_MS); assertThat(target.finished).isFalse(); }
public void testCallUninterruptiblyWithTimeout_goodCallableWithNotEnoughTime() throws Exception { try { service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (TimeoutException expected) { } }
public void testCallWithTimeout_propagatesReturnValue() throws Exception { String result = timeLimiter.callWithTimeout( Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS); assertThat(result).isEqualTo(RETURN_VALUE); }
ExecutorService executor = newSingleThreadExecutor(); TimeLimiter limiter = SimpleTimeLimiter.create(executor); java.sql.Statement limitedStatement = limiter.newProxy(jdbcStatement, java.sql.Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS); try (ResultSet resultSet = limitedStatement.executeQuery(formatSql(zeroRowsQuery, Optional.empty()))) { ResultSetMetaData metaData = resultSet.getMetaData();
public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception { Stopwatch stopwatch = Stopwatch.createStarted(); String result = service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS); assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT); assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS)); }
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); } }