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 035 * injection 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 = new ForeignException("FORTEST", new IllegalArgumentException("FORTEST")); 050 final ForeignException EXTEXN2 = new ForeignException("FORTEST2", new IllegalArgumentException("FORTEST2")); 051 052 /** 053 * Tests that a dispatcher only dispatches only the first exception, and does not propagate 054 * subsequent exceptions. 055 */ 056 @Test 057 public void testErrorPropagation() { 058 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class); 059 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class); 060 ForeignExceptionDispatcher dispatcher = new ForeignExceptionDispatcher(); 061 062 // add the listeners 063 dispatcher.addListener(listener1); 064 dispatcher.addListener(listener2); 065 066 // create an artificial error 067 dispatcher.receive(EXTEXN); 068 069 // make sure the listeners got the error 070 Mockito.verify(listener1, Mockito.times(1)).receive(EXTEXN); 071 Mockito.verify(listener2, Mockito.times(1)).receive(EXTEXN); 072 073 // make sure that we get an exception 074 try { 075 dispatcher.rethrowException(); 076 fail("Monitor should have thrown an exception after getting error."); 077 } catch (ForeignException ex) { 078 assertTrue("Got an unexpected exception:" + ex, ex.getCause() == EXTEXN.getCause()); 079 LOG.debug("Got the testing exception!"); 080 } 081 082 // push another error, which should be not be passed to listeners 083 dispatcher.receive(EXTEXN2); 084 Mockito.verify(listener1, Mockito.never()).receive(EXTEXN2); 085 Mockito.verify(listener2, Mockito.never()).receive(EXTEXN2); 086 } 087 088 @Test 089 public void testSingleDispatcherWithTimer() { 090 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class); 091 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class); 092 093 ForeignExceptionDispatcher monitor = new ForeignExceptionDispatcher(); 094 095 // add the listeners 096 monitor.addListener(listener1); 097 monitor.addListener(listener2); 098 099 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(monitor, 1000); 100 timer.start(); 101 timer.trigger(); 102 103 assertTrue("Monitor didn't get timeout", monitor.hasException()); 104 105 // verify that that we propagated the error 106 Mockito.verify(listener1).receive(Mockito.any()); 107 Mockito.verify(listener2).receive(Mockito.any()); 108 } 109 110 /** 111 * Test that the dispatcher can receive an error via the timer mechanism. 112 */ 113 @Test 114 public void testAttemptTimer() { 115 ForeignExceptionListener listener1 = Mockito.mock(ForeignExceptionListener.class); 116 ForeignExceptionListener listener2 = Mockito.mock(ForeignExceptionListener.class); 117 ForeignExceptionDispatcher orchestrator = new ForeignExceptionDispatcher(); 118 119 // add the listeners 120 orchestrator.addListener(listener1); 121 orchestrator.addListener(listener2); 122 123 // now create a timer and check for that error 124 TimeoutExceptionInjector timer = new TimeoutExceptionInjector(orchestrator, 1000); 125 timer.start(); 126 timer.trigger(); 127 // make sure that we got the timer error 128 Mockito.verify(listener1, Mockito.times(1)).receive(Mockito.any()); 129 Mockito.verify(listener2, Mockito.times(1)).receive(Mockito.any()); 130 } 131}