View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.SmallTests;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  import org.mockito.Mockito;
28  
29  /**
30   * Test the {@link TimeoutExceptionInjector} to ensure we fulfill contracts
31   */
32  @Category(SmallTests.class)
33  public class TestTimeoutExceptionInjector {
34  
35    private static final Log LOG = LogFactory.getLog(TestTimeoutExceptionInjector.class);
36  
37    /**
38     * Test that a manually triggered timer fires an exception.
39     */
40    @Test(timeout = 60000)
41    public void testTimerTrigger() {
42      final long time = 10000000; // pick a value that is very far in the future
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     * Test that a manually triggered exception with data fires with the data in receiveError.
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     * Demonstrate TimeoutExceptionInjector semantics -- completion means no more exceptions passed to
65     * error listener.
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     * Demonstrate TimeoutExceptionInjector semantics -- triggering fires exception and completes
85     * the timer.
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 }