TimeoutStrategy implements InvocationStrategy { @Override public <T> Invocation<T> apply(final Invocation<T> invocation) { assertNotNull(invocation, "Invocation must not null"); return new TimeoutInvocation<T>(timeout, invocation.getRequestMethod(), invocation.getParameters()); }  @Override Invocation<T> apply(final Invocation<T> invocation);  }
@Test public void testTimeout() throws Exception { final TimeoutStrategy strategy = new TimeoutStrategy(Time.of(1000L, TimeUnit.MILLISECONDS)); final RequestMethod<Integer> method = new RequestMethod<Integer>() { @Override public String getName() { return null; } @Override protected Integer runInternal(List<Object> parameters) throws Exception { return 0; } }; final Invocation<Integer> invocation = strategy.apply(method.toInvocation()); invocation.invoke(); }
@Test public void shouldThrowExceptionOnTimedOut() { try { final TimeoutStrategy strategy = new TimeoutStrategy(Time.of(100L, TimeUnit.MILLISECONDS)); final RequestMethod<Integer> method = new RequestMethod<Integer>() { @Override public String getName() { return null; } @Override protected Integer runInternal(List<Object> parameters) throws Exception { ThreadUtils.trySleep(1000L); return 0; } }; final Invocation<Integer> invocation = strategy.apply(method.toInvocation()); invocation.invoke(); fail(); } catch (TimeoutException e) { } catch (Exception e) { fail(e.getMessage()); } }
@Test public void shouldThrowExceptionOnError() { final Exception expected = new UnsupportedOperationException(); try { final TimeoutStrategy strategy = new TimeoutStrategy(Time.of(100L, TimeUnit.MILLISECONDS)); final RequestMethod<Integer> method = new RequestMethod<Integer>() { @Override public String getName() { return null; } @Override protected Integer runInternal(List<Object> parameters) throws Exception { throw expected; } }; final Invocation<Integer> invocation = strategy.apply(method.toInvocation()); invocation.invoke(); fail(); } catch (Exception actual) { assertEquals(expected, actual); } }