View Javadoc
1   /*
2      Copyright 2010 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue.retry;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.mockito.Mock;
21  
22  import java.util.concurrent.atomic.AtomicReference;
23  
24  import static org.hamcrest.Matchers.equalTo;
25  import static org.junit.Assert.assertThat;
26  import static org.mockito.Mockito.doThrow;
27  import static org.mockito.Mockito.times;
28  import static org.mockito.Mockito.verify;
29  import static org.mockito.Mockito.verifyZeroInteractions;
30  import static org.mockito.MockitoAnnotations.initMocks;
31  
32  public class RetryTaskTest {
33    private static final int ATTEMPTS = 4;
34  
35    @Mock private Runnable task;
36    @Mock private ExceptionHandler exceptionHandler;
37    @Mock private RuntimeException runtimeException;
38  
39    @Before public void setUp() {
40      initMocks(this);
41    }
42  
43    @Test public void basicTask() {
44      new RetryTask(task, ATTEMPTS).run();
45      verify(task).run();
46    }
47  
48    @Test(expected = RuntimeException.class) public void basicTaskRetry() {
49      doThrow(runtimeException).when(task).run();
50  
51      try {
52        new RetryTask(task, ATTEMPTS).run();
53      } finally {
54        verify(task, times(ATTEMPTS)).run();
55      }
56    }
57  
58    @Test public void taskWithExceptionHandler() {
59      new RetryTask(task, ATTEMPTS, exceptionHandler).run();
60      verify(task).run();
61      verifyZeroInteractions(exceptionHandler);
62    }
63  
64    @Test(expected = RuntimeException.class) public void taskRetryWithExceptions() {
65      doThrow(runtimeException).when(task).run();
66  
67      try {
68        new RetryTask(task, ATTEMPTS, exceptionHandler).run();
69      } finally {
70        verify(task, times(ATTEMPTS)).run();
71        verify(exceptionHandler, times(ATTEMPTS)).handle(runtimeException);
72      }
73    }
74  
75    @Test public void taskEarlyExit() {
76      final AtomicReference<Integer> failcount = new AtomicReference<>(0);
77      Runnable localTask = () -> {
78        failcount.set(failcount.get() + 1);
79        switch (failcount.get()) {
80          case 1:
81            throw new RuntimeException("First attempt");
82          case 2:
83            return;
84          default:
85            throw new RuntimeException("Third runthrough (fail)");
86        }
87      };
88  
89      new RetryTask(localTask, ATTEMPTS).run();
90      assertThat(failcount.get(), equalTo(2));
91    }
92  }