### Question:
Invoker { public T invoke() throws InvokerException { Throwable cause = null; for (int tried = 0; tried < trials; tried++) { try { T result = tryToInvoke(); return result; } catch (Exception e) { cause = e; pause(); } } throw new InvokerException(cause); } Invoker(String taskName, Callable<T> task, int trials, int trialTimeout, int pauseBetweenTrials, TimeUnit timeUnit); T invoke(); static boolean SAVE_HISTORY; }### Answer:
@Test public void shouldInvoke() throws InvokerException { Callable<String> task = new AlwaysWork(); int trials = 1; int timeout = 1; Invoker<String> invoker = new Invoker<String>("AlwaysWork", task, trials, timeout, pauseBetweenTrials, TimeUnit.SECONDS); String result = invoker.invoke(); assertEquals("0", result); }
@Test public void invocationShouldWorkInTheSecondTrial() throws InvokerException { Callable<String> task = new WorksInTheSecond(); int trials = 2; int timeout = 1; Invoker<String> invoker = new Invoker<String>("WorksInTheSecond", task, trials, timeout, pauseBetweenTrials, TimeUnit.SECONDS); String result = invoker.invoke(); assertEquals("0", result); }
@Test(expected = InvokerException.class) public void invocationShouldNotWork() throws InvokerException { Callable<String> task = new WorksInTheSecond(); int trials = 1; int timeout = 1; Invoker<String> invoker = new Invoker<String>("WorksInTheSecond", task, trials, timeout, pauseBetweenTrials, TimeUnit.SECONDS); invoker.invoke(); }
@Test public void shouldInvokeAndNotTimeout() throws InvokerException { Callable<String> task = new TakesHundredMilliSecToWork(); int trials = 1; int timeout = 500; Invoker<String> invoker = new Invoker<String>("TakesHundredMilliSecToWork", task, trials, timeout, pauseBetweenTrials, TimeUnit.MILLISECONDS); String result = invoker.invoke(); assertEquals("0", result); }
@Test(expected = InvokerException.class) public void shouldInvokeAndTimeout() throws InvokerException { Callable<String> task = new TakesHundredMilliSecToWork(); int trials = 1; int timeout = 5; Invoker<String> invoker = new Invoker<String>("TakesHundredMilliSecToWork", task, trials, timeout, pauseBetweenTrials, TimeUnit.MILLISECONDS); invoker.invoke(); }
@Test public void shouldExecuteVoidTask() throws InvokerException { VoidTask task = new VoidTask(); int trials = 1; int timeout = 100; Invoker<Void> invoker = new Invoker<Void>("VoidTask", task, trials, timeout, pauseBetweenTrials, TimeUnit.MILLISECONDS); invoker.invoke(); boolean done = task.done; assertEquals(true, done); }