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