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.jupiter.api.Assertions.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.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035/**
036 * Test the {@link TimeoutExceptionInjector} to ensure we fulfill contracts
037 */
038@Tag(MasterTests.TAG)
039@Tag(SmallTests.TAG)
040public class TestTimeoutExceptionInjector {
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 = mock(ForeignExceptionListener.class);
051    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
052    timer.start();
053    timer.trigger();
054    verify(listener, times(1)).receive(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 = mock(ForeignExceptionListener.class);
064    TimeoutExceptionInjector timer = new TimeoutExceptionInjector(listener, time);
065    timer.start();
066    timer.trigger();
067    verify(listener).receive(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 = 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    verifyNoInteractions(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 = 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    verify(listener, times(1)).receive(any());
108    verifyNoMoreInteractions(listener);
109  }
110}