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.function.Supplier;
23  
24  import static org.hamcrest.Matchers.equalTo;
25  import static org.junit.Assert.assertThat;
26  import static org.mockito.Mockito.times;
27  import static org.mockito.Mockito.verify;
28  import static org.mockito.Mockito.verifyNoMoreInteractions;
29  import static org.mockito.Mockito.verifyZeroInteractions;
30  import static org.mockito.Mockito.when;
31  import static org.mockito.MockitoAnnotations.initMocks;
32  
33  public class RetrySupplierTest {
34    private static final int ATTEMPTS = 4;
35    public static final String RESULT = "result";
36  
37    @Mock private Supplier<String> supplier;
38    @Mock private ExceptionHandler exceptionHandler;
39    @Mock private RuntimeException runtimeException;
40  
41    @Before public void setUp() {
42      initMocks(this);
43    }
44  
45    @Test public void basicSupplier() {
46      when(supplier.get()).thenReturn(RESULT);
47      final String result = new RetrySupplier<>(supplier, ATTEMPTS).get();
48  
49      verify(supplier).get();
50      assertThat(result, equalTo(RESULT));
51    }
52  
53    @Test(expected = IllegalArgumentException.class) public void basicSupplierRequiresPositiveTries() {
54      new RetrySupplier<>(supplier, 0).get();
55    }
56  
57    @Test(expected = RuntimeException.class) public void basicSupplierRetry() {
58      when(supplier.get()).thenThrow(runtimeException);
59  
60      try {
61        new RetrySupplier<>(supplier, ATTEMPTS).get();
62      } finally {
63        verify(supplier, times(ATTEMPTS)).get();
64      }
65    }
66  
67    @Test public void supplierWithExceptionHandler() {
68      when(supplier.get()).thenReturn(RESULT);
69      final String result = new RetrySupplier<>(supplier, ATTEMPTS, exceptionHandler).get();
70  
71      verify(supplier).get();
72      assertThat(result, equalTo(RESULT));
73      verifyZeroInteractions(exceptionHandler);
74    }
75  
76    @Test(expected = RuntimeException.class) public void supplierRetryWithExceptions() {
77      when(supplier.get()).thenThrow(runtimeException);
78  
79      try {
80        new RetrySupplier<>(supplier, ATTEMPTS, exceptionHandler).get();
81      } finally {
82        verify(supplier, times(ATTEMPTS)).get();
83        verify(exceptionHandler, times(ATTEMPTS)).handle(runtimeException);
84      }
85    }
86  
87    @Test public void supplierEarlyExit() {
88      when(supplier.get()).thenThrow(new RuntimeException("First attempt")).thenReturn(RESULT).thenThrow(new RuntimeException("Third attempt"))
89        .thenThrow(new RuntimeException("Fourth attempt"));
90  
91      final String result = new RetrySupplier<>(supplier, ATTEMPTS).get();
92      assertThat(result, equalTo(RESULT));
93      verify(supplier, times(2)).get();
94      verifyNoMoreInteractions(supplier);
95    }
96  }