Revert "BackPort HBASE-11554 Remove Reusable poolmap Rpc client type. (#2208)"
[hbase.git] / hbase-client / src / test / java / org / apache / hadoop / hbase / util / TestReusablePoolMap.java
blob3fcaebb5fe5c187f08da55054e0922a2817ca013
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.util;
20 import static org.junit.Assert.assertEquals;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Random;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ThreadLocalRandom;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.testclassification.MiscTests;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.hadoop.hbase.util.PoolMap.PoolType;
31 import org.junit.ClassRule;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
35 @Category({ MiscTests.class, SmallTests.class })
36 public class TestReusablePoolMap extends PoolMapTestBase {
38 @ClassRule
39 public static final HBaseClassTestRule CLASS_RULE =
40 HBaseClassTestRule.forClass(TestReusablePoolMap.class);
42 @Override
43 protected PoolType getPoolType() {
44 return PoolType.Reusable;
47 @Test
48 public void testSingleThreadedClient() throws InterruptedException, ExecutionException {
49 Random rand = ThreadLocalRandom.current();
50 String randomKey = String.valueOf(rand.nextInt());
51 String randomValue = String.valueOf(rand.nextInt());
52 // As long as we poll values we put, the pool size should remain zero
53 runThread(randomKey, randomValue, randomValue);
54 assertEquals(0, poolMap.size(randomKey));
57 @Test
58 public void testMultiThreadedClients() throws InterruptedException, ExecutionException {
59 Random rand = ThreadLocalRandom.current();
60 // As long as we poll values we put, the pool size should remain zero
61 for (int i = 0; i < POOL_SIZE; i++) {
62 String randomKey = String.valueOf(rand.nextInt());
63 String randomValue = String.valueOf(rand.nextInt());
64 runThread(randomKey, randomValue, randomValue);
65 assertEquals(0, poolMap.size(randomKey));
67 poolMap.clear();
68 String randomKey = String.valueOf(rand.nextInt());
69 for (int i = 0; i < POOL_SIZE - 1; i++) {
70 String randomValue = String.valueOf(rand.nextInt());
71 runThread(randomKey, randomValue, randomValue);
72 assertEquals(0, poolMap.size(randomKey));
74 assertEquals(0, poolMap.size(randomKey));
77 @Test
78 public void testPoolCap() throws InterruptedException, ExecutionException {
79 Random rand = ThreadLocalRandom.current();
80 // As long as we poll values we put, the pool size should remain zero
81 String randomKey = String.valueOf(rand.nextInt());
82 List<String> randomValues = new ArrayList<>();
83 for (int i = 0; i < POOL_SIZE * 2; i++) {
84 String randomValue = String.valueOf(rand.nextInt());
85 randomValues.add(randomValue);
86 runThread(randomKey, randomValue, randomValue);
88 assertEquals(0, poolMap.size(randomKey));