### Question:
LimitedRetryRestartStrategy extends BaseRestartStrategy<T> { @Override public RestartStrategyInstance<T> newInstance(T restartable) { LimitedRetryRestartStrategyInstance<T> instance = new LimitedRetryRestartStrategyInstance<T>(restartable, this, numberRetries, sampleDelay, timeDurationSuccessfulRestart, spaceEnvironment.getTimeProvider(), spaceEnvironment.getExecutorService()); instance.startRestartAttempts(); return instance; } LimitedRetryRestartStrategy(int numberRetries, long sampleDelay, long timeDurationSuccessfulRestart,
      InteractiveSpacesEnvironment spaceEnvironment); @Override RestartStrategyInstance<T> newInstance(T restartable);  }### Answer:
@Test public void testSuccess() throws Exception{ LimitedRetryRestartStrategy strategy = new LimitedRetryRestartStrategy(1, 20, 40, spaceEnvironment); strategy.addRestartStrategyListener(restartListener); Mockito.when(restartable.isRestarted()).thenReturn(true); Mockito.when(restartListener.onRestartAttempt(strategy, restartable, true)).thenReturn(true); Mockito.when(timeProvider.getCurrentTime()).thenReturn(0l, 100l); RestartStrategyInstance instance = strategy.newInstance(restartable); Thread.sleep(1000); Mockito.verify(restartable, Mockito.times(1)).attemptRestart(); Mockito.verify(restartable, Mockito.times(1)).restartComplete(true); Mockito.verify(restartListener, Mockito.times(0)).onRestartFailure(strategy, restartable); Mockito.verify(restartListener, Mockito.times(1)).onRestartAttempt(strategy, restartable, true); Mockito.verify(restartListener, Mockito.times(1)).onRestartSuccess(strategy, restartable); }
@Test public void testFailure() throws Exception { LimitedRetryRestartStrategy strategy = new LimitedRetryRestartStrategy(3, 20, 40, spaceEnvironment); strategy.addRestartStrategyListener(restartListener); Mockito.when(restartable.isRestarted()).thenReturn(false); Mockito.when(restartListener.onRestartAttempt(strategy, restartable, true)).thenReturn(true); Mockito.when(timeProvider.getCurrentTime()).thenReturn(0l, 100l); RestartStrategyInstance instance = strategy.newInstance(restartable); Thread.sleep(1000); Mockito.verify(restartable, Mockito.times(3)).attemptRestart(); Mockito.verify(restartable, Mockito.times(1)).restartComplete(false); Mockito.verify(restartListener, Mockito.times(1)).onRestartFailure(strategy, restartable); Mockito.verify(restartListener, Mockito.times(3)).onRestartAttempt(strategy, restartable, true); Mockito.verify(restartListener, Mockito.times(0)).onRestartSuccess(strategy, restartable); }
@Test public void testFailureFromListener() throws Exception { LimitedRetryRestartStrategy strategy = new LimitedRetryRestartStrategy(3, 20, 40, spaceEnvironment); strategy.addRestartStrategyListener(restartListener); Mockito.when(restartable.isRestarted()).thenReturn(false); Mockito.when(restartListener.onRestartAttempt(strategy, restartable, true)).thenReturn(false); Mockito.when(timeProvider.getCurrentTime()).thenReturn(0l, 100l); RestartStrategyInstance instance = strategy.newInstance(restartable); Thread.sleep(1000); Mockito.verify(restartable, Mockito.times(0)).attemptRestart(); Mockito.verify(restartable, Mockito.times(1)).restartComplete(false); Mockito.verify(restartListener, Mockito.times(1)).onRestartFailure(strategy, restartable); Mockito.verify(restartListener, Mockito.times(1)).onRestartAttempt(strategy, restartable, true); Mockito.verify(restartListener, Mockito.times(0)).onRestartSuccess(strategy, restartable); }