HBASE-26474 Implement connection-level attributes (addendum)
[hbase.git] / hbase-client / src / test / java / org / apache / hadoop / hbase / client / TestConnectionRegistryLeak.java
blob561b1f5715fdf36bff4fa7a6496a9f1027f46b56
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.ExecutionException;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseClassTestRule;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.testclassification.ClientTests;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.util.FutureUtils;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
41 @Category({ ClientTests.class, SmallTests.class })
42 public class TestConnectionRegistryLeak {
44 @ClassRule
45 public static final HBaseClassTestRule CLASS_RULE =
46 HBaseClassTestRule.forClass(TestConnectionRegistryLeak.class);
48 public static final class ConnectionRegistryForTest extends DoNothingConnectionRegistry {
50 private boolean closed = false;
52 public ConnectionRegistryForTest(Configuration conf) {
53 super(conf);
54 CREATED.add(this);
57 @Override
58 public CompletableFuture<String> getClusterId() {
59 return FutureUtils.failedFuture(new IOException("inject error"));
62 @Override
63 public void close() {
64 closed = true;
68 private static final List<ConnectionRegistryForTest> CREATED = new ArrayList<>();
70 private static Configuration CONF = HBaseConfiguration.create();
72 @BeforeClass
73 public static void setUp() {
74 CONF.setClass(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
75 ConnectionRegistryForTest.class, ConnectionRegistry.class);
78 @Test
79 public void test() throws InterruptedException {
80 for (int i = 0; i < 10; i++) {
81 try {
82 ConnectionFactory.createAsyncConnection(CONF).get();
83 fail();
84 } catch (ExecutionException e) {
85 // expected
88 assertEquals(10, CREATED.size());
89 CREATED.forEach(r -> assertTrue(r.closed));