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.jupiter.api.Assertions.assertEquals;
024import static org.junit.jupiter.api.Assertions.assertThrows;
025import static org.junit.jupiter.api.Assertions.assertTrue;
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.HBaseIOException;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeEach;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
041
042@Tag(MiscTests.TAG)
043@Tag(SmallTests.TAG)
044public class TestFutureUtils {
045
046  private ExecutorService executor;
047
048  @BeforeEach
049  public void setUp() {
050    executor = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).build());
051  }
052
053  @AfterEach
054  public void tearDown() {
055    executor.shutdownNow();
056  }
057
058  @Test
059  public void testRecordStackTrace() throws IOException {
060    CompletableFuture<Void> future = new CompletableFuture<>();
061    executor.execute(() -> future.completeExceptionally(new HBaseIOException("Inject error!")));
062    HBaseIOException e = assertThrows(HBaseIOException.class, () -> FutureUtils.get(future),
063      "The future should have been completed exceptionally");
064    assertEquals("Inject error!", e.getMessage());
065    StackTraceElement[] elements = e.getStackTrace();
066    assertThat(elements[0].toString(), containsString("java.lang.Thread.getStackTrace"));
067    assertThat(elements[1].toString(),
068      startsWith("org.apache.hadoop.hbase.util.FutureUtils.setStackTrace"));
069    assertThat(elements[2].toString(),
070      startsWith("org.apache.hadoop.hbase.util.FutureUtils.rethrow"));
071    assertThat(elements[3].toString(), startsWith("org.apache.hadoop.hbase.util.FutureUtils.get"));
072    assertThat(elements[4].toString(),
073      startsWith("org.apache.hadoop.hbase.util.TestFutureUtils.lambda"));
074    int i = 5;
075    for (;;) {
076      if (!elements[i].toString().contains("assertThrows")) {
077        break;
078      }
079      i++;
080    }
081    assertThat(elements[i].toString(),
082      startsWith("org.apache.hadoop.hbase.util.TestFutureUtils.testRecordStackTrace"));
083    assertTrue(Stream.of(elements)
084      .anyMatch(element -> element.toString().contains("--------Future.get--------")));
085  }
086}