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.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.SmallTests;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28 import org.mockito.Mockito;
29
30
31
32
33
34 @Category(SmallTests.class)
35 public class TestForeignExceptionDispatcher {
36 private static final Log LOG = LogFactory.getLog(TestForeignExceptionDispatcher.class);
37
38
39
40
41 final ForeignException EXTEXN = new ForeignException("FORTEST", new IllegalArgumentException("FORTEST"));
42 final ForeignException EXTEXN2 = new ForeignException("FORTEST2", new IllegalArgumentException("FORTEST2"));
43
44
45
46
47
48 @Test
49 public void testErrorPropagation() {
50 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
51 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
52 ForeignExceptionDispatcher dispatcher = new ForeignExceptionDispatcher();
53
54
55 dispatcher.addListener(listener1);
56 dispatcher.addListener(listener2);
57
58
59 dispatcher.receive(EXTEXN);
60
61
62 Mockito.verify(listener1, Mockito.times(1)).receive(EXTEXN);
63 Mockito.verify(listener2, Mockito.times(1)).receive(EXTEXN);
64
65
66 try {
67 dispatcher.rethrowException();
68 fail("Monitor should have thrown an exception after getting error.");
69 } catch (ForeignException ex) {
70 assertTrue("Got an unexpected exception:" + ex, ex.getCause() == EXTEXN.getCause());
71 LOG.debug("Got the testing exception!");
72 }
73
74
75 dispatcher.receive(EXTEXN2);
76 Mockito.verify(listener1, Mockito.never()).receive(EXTEXN2);
77 Mockito.verify(listener2, Mockito.never()).receive(EXTEXN2);
78 }
79
80 @Test
81 public void testSingleDispatcherWithTimer() {
82 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
83 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
84
85 ForeignExceptionDispatcher monitor = new ForeignExceptionDispatcher();
86
87
88 monitor.addListener(listener1);
89 monitor.addListener(listener2);
90
91 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(monitor, 1000);
92 timer.start();
93 timer.trigger();
94
95 assertTrue("Monitor didn't get timeout", monitor.hasException());
96
97
98 Mockito.verify(listener1).receive(Mockito.any(ForeignException.class));
99 Mockito.verify(listener2).receive(Mockito.any(ForeignException.class));
100 }
101
102
103
104
105 @Test
106 public void testAttemptTimer() {
107 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
108 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
109 ForeignExceptionDispatcher orchestrator = new ForeignExceptionDispatcher();
110
111
112 orchestrator.addListener(listener1);
113 orchestrator.addListener(listener2);
114
115
116 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(orchestrator, 1000);
117 timer.start();
118 timer.trigger();
119
120 Mockito.verify(listener1, Mockito.times(1)).receive(Mockito.any(ForeignException.class));
121 Mockito.verify(listener2, Mockito.times(1)).receive(Mockito.any(ForeignException.class));
122 }
123 }