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 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 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 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 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(); }
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();
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"); } }
private static <T> T timeLimited(T target, Class<T> clazz, Duration timeout, ExecutorService executor, int maxThreads) { executor = new ExecutorServiceAdapter(new BoundedExecutor(executor, maxThreads)); TimeLimiter limiter = SimpleTimeLimiter.create(executor); return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS); }
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 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(); }
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 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)); }
private static <T> T timeLimited(T target, Class<T> clazz, Duration timeout, ExecutorService executor, int maxThreads) { executor = new ExecutorServiceAdapter(new BoundedExecutor(executor, maxThreads)); TimeLimiter limiter = SimpleTimeLimiter.create(executor); return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS); }
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(); }