001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.errorhandling;
019
020import static org.junit.Assert.assertTrue;
021import static org.junit.Assert.fail;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.MasterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029import org.mockito.Mockito;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * Test that we propagate errors through an dispatcher exactly once via different failure injection
035 * mechanisms.
036 */
037@Category({ MasterTests.class, SmallTests.class })
038public class TestForeignExceptionDispatcher {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestForeignExceptionDispatcher.class);
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestForeignExceptionDispatcher.class);
045
046  /**
047   * Exception thrown from the test
048   */
049  final ForeignException EXTEXN =
050    new ForeignException("FORTEST", new IllegalArgumentException("FORTEST"));
051  final ForeignException EXTEXN2 =
052    new ForeignException("FORTEST2", new IllegalArgumentException("FORTEST2"));
053
054  /**
055   * Tests that a dispatcher only dispatches only the first exception, and does not propagate
056   * subsequent exceptions.
057   */
058  @Test
059  public void testErrorPropagation() {
060    ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
061    ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
062    ForeignExceptionDispatcher dispatcher = new ForeignExceptionDispatcher();
063
064    // add the listeners
065    dispatcher.addListener(listener1);
066    dispatcher.addListener(listener2);
067
068    // create an artificial error
069    dispatcher.receive(EXTEXN);
070
071    // make sure the listeners got the error
072    Mockito.verify(listener1, Mockito.times(1)).receive(EXTEXN);
073    Mockito.verify(listener2, Mockito.times(1)).receive(EXTEXN);
074
075    // make sure that we get an exception
076    try {
077      dispatcher.rethrowException();
078      fail("Monitor should have thrown an exception after getting error.");
079    } catch (ForeignException ex) {
080      assertTrue("Got an unexpected exception:" + ex, ex.getCause() == EXTEXN.getCause());
081      LOG.debug("Got the testing exception!");
082    }
083
084    // push another error, which should be not be passed to listeners
085    dispatcher.receive(EXTEXN2);
086    Mockito.verify(listener1, Mockito.never()).receive(EXTEXN2);
087    Mockito.verify(listener2, Mockito.never()).receive(EXTEXN2);
088  }
089
090  @Test
091  public void testSingleDispatcherWithTimer() {
092    ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
093    ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
094
095    ForeignExceptionDispatcher monitor = new ForeignExceptionDispatcher();
096
097    // add the listeners
098    monitor.addListener(listener1);
099    monitor.addListener(listener2);
100
101    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(monitor, 1000);
102    timer.start();
103    timer.trigger();
104
105    assertTrue("Monitor didn't get timeout", monitor.hasException());
106
107    // verify that that we propagated the error
108    Mockito.verify(listener1).receive(Mockito.any());
109    Mockito.verify(listener2).receive(Mockito.any());
110  }
111
112  /**
113   * Test that the dispatcher can receive an error via the timer mechanism.
114   */
115  @Test
116  public void testAttemptTimer() {
117    ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class);
118    ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class);
119    ForeignExceptionDispatcher orchestrator = new ForeignExceptionDispatcher();
120
121    // add the listeners
122    orchestrator.addListener(listener1);
123    orchestrator.addListener(listener2);
124
125    // now create a timer and check for that error
126    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(orchestrator, 1000);
127    timer.start();
128    timer.trigger();
129    // make sure that we got the timer error
130    Mockito.verify(listener1, Mockito.times(1)).receive(Mockito.any());
131    Mockito.verify(listener2, Mockito.times(1)).receive(Mockito.any());
132  }
133}