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