View Javadoc
1   package net.secodo.jcircuitbreaker.breakhandler.impl;
2   
3   import static org.hamcrest.CoreMatchers.equalTo;
4   
5   import static org.junit.Assert.assertThat;
6   
7   import static org.mockito.Mockito.mock;
8   
9   import java.lang.reflect.Field;
10  
11  import java.util.concurrent.Callable;
12  import java.util.concurrent.ConcurrentHashMap;
13  
14  import net.secodo.jcircuitbreaker.breaker.ContextAwareCircuitBreaker;
15  import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
16  import net.secodo.jcircuitbreaker.breaker.execution.impl.DefaultExecutionContextImpl;
17  import net.secodo.jcircuitbreaker.breakhandler.BreakHandler;
18  import net.secodo.jcircuitbreaker.breakstrategy.BreakStrategy;
19  
20  import net.secodo.jcircuitbreaker.task.Task;
21  import org.junit.Test;
22  
23  
24  @SuppressWarnings("unchecked")
25  public class ReusableRetryHandlerTest {
26    @Test
27    public void shouldCreateNewRetryHandlerForEachInvocation() throws Exception {
28      final ReusableRetryHandler<Integer> factory = new ReusableRetryHandler<>(5);
29  
30      ExecutionContext<Integer> executionContext1 = new DefaultExecutionContextImpl<>(new ConcurrentHashMap<>(),
31        null);
32  
33      final BreakHandler<Integer> handler1 = factory.makeHandler(mock(Task.class), executionContext1);
34      handler1.onBreak(mock(ContextAwareCircuitBreaker.class),
35        mock(Task.class),
36        mock(BreakStrategy.class),
37        executionContext1);
38  
39      ExecutionContext<Integer> executionContext2 = new DefaultExecutionContextImpl<>(new ConcurrentHashMap<>(),
40        null);
41  
42      final BreakHandler<Integer> handler2 = factory.makeHandler(mock(Task.class), executionContext2);
43      handler2.onBreak(mock(ContextAwareCircuitBreaker.class),
44        mock(Task.class),
45        mock(BreakStrategy.class),
46        executionContext2);
47  
48      handler2.onBreak(mock(ContextAwareCircuitBreaker.class),
49        mock(Task.class),
50        mock(BreakStrategy.class),
51        executionContext2);
52  
53      handler2.onBreak(mock(ContextAwareCircuitBreaker.class),
54        mock(Task.class),
55        mock(BreakStrategy.class),
56        executionContext2);
57  
58      assertThat(getNumberOfRetryAttempts((StatefulRetryHandler) handler1), equalTo(1)); // only one call to this handler
59      assertThat(getNumberOfRetryAttempts((StatefulRetryHandler) handler2), equalTo(3)); // three calls to this handler
60  
61      // NOTE: different state of the handler proves that these are different instances
62  
63  
64    }
65  
66    private int getNumberOfRetryAttempts(StatefulRetryHandler retryHandler) throws NoSuchFieldException,
67                                                                                   IllegalAccessException {
68      final Field field = StatefulRetryHandler.class.getDeclaredField("currentRetryAttempt");
69      field.setAccessible(true);
70      return (int) field.get(retryHandler);
71    }
72  
73  }