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.util;
019
020import static org.hamcrest.CoreMatchers.containsString;
021import static org.hamcrest.CoreMatchers.startsWith;
022import static org.hamcrest.MatcherAssert.assertThat;
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertTrue;
025import static org.junit.Assert.fail;
026
027import java.io.IOException;
028import java.util.concurrent.CompletableFuture;
029import java.util.concurrent.ExecutorService;
030import java.util.concurrent.Executors;
031import java.util.stream.Stream;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseIOException;
034import org.apache.hadoop.hbase.testclassification.MiscTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
043
044@Category({ MiscTests.class, SmallTests.class })
045public class TestFutureUtils {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestFutureUtils.class);
050
051  private ExecutorService executor;
052
053  @Before
054  public void setUp() {
055    executor = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).build());
056  }
057
058  @After
059  public void tearDown() {
060    executor.shutdownNow();
061  }
062
063  @Test
064  public void testRecordStackTrace() throws IOException {
065    CompletableFuture<Void> future = new CompletableFuture<>();
066    executor.execute(() -> future.completeExceptionally(new HBaseIOException("Inject error!")));
067    try {
068      FutureUtils.get(future);
069      fail("The future should have been completed exceptionally");
070    } catch (HBaseIOException e) {
071      assertEquals("Inject error!", e.getMessage());
072      StackTraceElement[] elements = e.getStackTrace();
073      assertThat(elements[0].toString(), containsString("java.lang.Thread.getStackTrace"));
074      assertThat(elements[1].toString(),
075        startsWith("org.apache.hadoop.hbase.util.FutureUtils.setStackTrace"));
076      assertThat(elements[2].toString(),
077        startsWith("org.apache.hadoop.hbase.util.FutureUtils.rethrow"));
078      assertThat(elements[3].toString(),
079        startsWith("org.apache.hadoop.hbase.util.FutureUtils.get"));
080      assertThat(elements[4].toString(),
081        startsWith("org.apache.hadoop.hbase.util.TestFutureUtils.testRecordStackTrace"));
082      assertTrue(Stream.of(elements)
083        .anyMatch(element -> element.toString().contains("--------Future.get--------")));
084    } catch (Throwable t) {
085      throw new AssertionError("Caught unexpected Throwable", t);
086    }
087  }
088}