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