### Question:
Retry { public T runWithDelay() throws Exception { try { return execute(); } catch(Exception e) { if (e.getClass().equals(retryExceptionType)){ tries++; if (MAX_RETRIES == tries) { throw e; } else { Thread.sleep((long) DELAY * tries); return runWithDelay(); } } else { throw e; } } } Retry(Class exceptionClassType); abstract T execute(); T run(); T runWithDelay(); static final int MAX_RETRIES; static final int DELAY; }### Answer:
@Test public void testRetryFailureWithDelay() { Retry<Void> retriable = new Retry<Void>(NullPointerException.class) { @Override public Void execute() { throw new RuntimeException(); } }; try { retriable.runWithDelay(); Assert.fail(); } catch (Exception e) { Assert.assertEquals(RuntimeException.class, e.getClass()); } }
@Test public void testRetrySuccessWithDelay() { Retry<Void> retriable = new Retry<Void>(NullPointerException.class) { private long startTime = System.currentTimeMillis(); @Override public Void execute() { executeWithDelay(startTime); return null; } }; try { retriable.runWithDelay(); } catch (Exception e) { Assert.fail(); } }
@Test public void testRetryFailureWithDelayMoreThanTimeout() { Retry<Void> retriable = new Retry<Void>(NullPointerException.class) { @Override public Void execute() { throw new NullPointerException(); } }; long startTime = System.currentTimeMillis(); try { retriable.runWithDelay(); Assert.fail(); } catch (Exception e) { Assert.assertEquals(NullPointerException.class, e.getClass()); Assert.assertTrue(System.currentTimeMillis() - startTime >= 180 * 1000); } }