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.client; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022import static org.junit.Assert.fail; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.List; 027import java.util.concurrent.CompletableFuture; 028import java.util.concurrent.ExecutionException; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseConfiguration; 032import org.apache.hadoop.hbase.testclassification.ClientTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.apache.hadoop.hbase.util.FutureUtils; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039 040@Category({ ClientTests.class, SmallTests.class }) 041public class TestAsyncRegistryLeak { 042 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestAsyncRegistryLeak.class); 046 047 public static final class AsyncRegistryForTest extends DoNothingAsyncRegistry { 048 049 private boolean closed = false; 050 051 public AsyncRegistryForTest(Configuration conf) { 052 super(conf); 053 CREATED.add(this); 054 } 055 056 @Override 057 public CompletableFuture<String> getClusterId() { 058 return FutureUtils.failedFuture(new IOException("inject error")); 059 } 060 061 @Override 062 public void close() { 063 closed = true; 064 } 065 } 066 067 private static final List<AsyncRegistryForTest> CREATED = new ArrayList<>(); 068 069 private static Configuration CONF = HBaseConfiguration.create(); 070 071 @BeforeClass 072 public static void setUp() { 073 CONF.setClass(AsyncRegistryFactory.REGISTRY_IMPL_CONF_KEY, AsyncRegistryForTest.class, 074 AsyncRegistry.class); 075 } 076 077 @Test 078 public void test() throws InterruptedException { 079 for (int i = 0; i < 10; i++) { 080 try { 081 ConnectionFactory.createAsyncConnection(CONF).get(); 082 fail(); 083 } catch (ExecutionException e) { 084 // expected 085 } 086 } 087 assertEquals(10, CREATED.size()); 088 CREATED.forEach(r -> assertTrue(r.closed)); 089 } 090}