1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.errorhandling;
19
20 import static org.junit.Assert.fail;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.testclassification.SmallTests;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27 import org.mockito.Mockito;
28
29
30
31
32 @Category(SmallTests.class)
33 public class TestTimeoutExceptionInjector {
34
35 private static final Log LOG = LogFactory.getLog(TestTimeoutExceptionInjector.class);
36
37
38
39
40 @Test(timeout = 60000)
41 public void testTimerTrigger() {
42 final long time = 10000000;
43 ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
44 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
45 timer.start();
46 timer.trigger();
47 Mockito.verify(listener, Mockito.times(1)).receive(Mockito.any(ForeignException.class));
48 }
49
50
51
52
53 @Test
54 public void testTimerPassesOnErrorInfo() {
55 final long time = 1000000;
56 ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
57 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
58 timer.start();
59 timer.trigger();
60 Mockito.verify(listener).receive(Mockito.any(ForeignException.class));
61 }
62
63
64
65
66
67 @Test(timeout = 60000)
68 public void testStartAfterComplete() throws InterruptedException {
69 final long time = 10;
70 ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
71 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
72 timer.complete();
73 try {
74 timer.start();
75 fail("Timer should fail to start after complete.");
76 } catch (IllegalStateException e) {
77 LOG.debug("Correctly failed timer: " + e.getMessage());
78 }
79 Thread.sleep(time + 1);
80 Mockito.verifyZeroInteractions(listener);
81 }
82
83
84
85
86
87 @Test(timeout = 60000)
88 public void testStartAfterTrigger() throws InterruptedException {
89 final long time = 10;
90 ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
91 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
92 timer.trigger();
93 try {
94 timer.start();
95 fail("Timer should fail to start after complete.");
96 } catch (IllegalStateException e) {
97 LOG.debug("Correctly failed timer: " + e.getMessage());
98 }
99 Thread.sleep(time * 2);
100 Mockito.verify(listener, Mockito.times(1)).receive(Mockito.any(ForeignException.class));
101 Mockito.verifyNoMoreInteractions(listener);
102 }
103 }