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