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.fail;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.testclassification.MasterTests;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028import org.mockito.Mockito;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Test the {@link TimeoutExceptionInjector} to ensure we fulfill contracts
034 */
035@Category({ MasterTests.class, SmallTests.class })
036public class TestTimeoutExceptionInjector {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestTimeoutExceptionInjector.class);
041
042  private static final Logger LOG = LoggerFactory.getLogger(TestTimeoutExceptionInjector.class);
043
044  /**
045   * Test that a manually triggered timer fires an exception.
046   */
047  @Test
048  public void testTimerTrigger() {
049    final long time = 10000000; // pick a value that is very far in the future
050    ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
051    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
052    timer.start();
053    timer.trigger();
054    Mockito.verify(listener, Mockito.times(1)).receive(Mockito.any());
055  }
056
057  /**
058   * Test that a manually triggered exception with data fires with the data in receiveError.
059   */
060  @Test
061  public void testTimerPassesOnErrorInfo() {
062    final long time = 1000000;
063    ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
064    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
065    timer.start();
066    timer.trigger();
067    Mockito.verify(listener).receive(Mockito.any());
068  }
069
070  /**
071   * Demonstrate TimeoutExceptionInjector semantics -- completion means no more exceptions passed to
072   * error listener.
073   */
074  @Test
075  public void testStartAfterComplete() throws InterruptedException {
076    final long time = 10;
077    ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
078    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
079    timer.complete();
080    try {
081      timer.start();
082      fail("Timer should fail to start after complete.");
083    } catch (IllegalStateException e) {
084      LOG.debug("Correctly failed timer: " + e.getMessage());
085    }
086    Thread.sleep(time + 1);
087    Mockito.verifyZeroInteractions(listener);
088  }
089
090  /**
091   * Demonstrate TimeoutExceptionInjector semantics -- triggering fires exception and completes the
092   * timer.
093   */
094  @Test
095  public void testStartAfterTrigger() throws InterruptedException {
096    final long time = 10;
097    ForeignExceptionListener listener = Mockito.mock(ForeignExceptionListener.class);
098    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
099    timer.trigger();
100    try {
101      timer.start();
102      fail("Timer should fail to start after complete.");
103    } catch (IllegalStateException e) {
104      LOG.debug("Correctly failed timer: " + e.getMessage());
105    }
106    Thread.sleep(time * 2);
107    Mockito.verify(listener, Mockito.times(1)).receive(Mockito.any());
108    Mockito.verifyNoMoreInteractions(listener);
109  }
110}