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