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.util; 019 020import static org.junit.Assert.assertEquals; 021 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Random; 025import java.util.concurrent.ExecutionException; 026import java.util.concurrent.ThreadLocalRandom; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.testclassification.MiscTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.apache.hadoop.hbase.util.PoolMap.PoolType; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034 035@Category({ MiscTests.class, SmallTests.class }) 036public class TestReusablePoolMap extends PoolMapTestBase { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestReusablePoolMap.class); 041 042 @Override 043 protected PoolType getPoolType() { 044 return PoolType.Reusable; 045 } 046 047 @Test 048 public void testSingleThreadedClient() throws InterruptedException, ExecutionException { 049 Random rand = ThreadLocalRandom.current(); 050 String randomKey = String.valueOf(rand.nextInt()); 051 String randomValue = String.valueOf(rand.nextInt()); 052 // As long as we poll values we put, the pool size should remain zero 053 runThread(randomKey, randomValue, randomValue); 054 assertEquals(0, poolMap.size(randomKey)); 055 } 056 057 @Test 058 public void testMultiThreadedClients() throws InterruptedException, ExecutionException { 059 Random rand = ThreadLocalRandom.current(); 060 // As long as we poll values we put, the pool size should remain zero 061 for (int i = 0; i < POOL_SIZE; i++) { 062 String randomKey = String.valueOf(rand.nextInt()); 063 String randomValue = String.valueOf(rand.nextInt()); 064 runThread(randomKey, randomValue, randomValue); 065 assertEquals(0, poolMap.size(randomKey)); 066 } 067 poolMap.clear(); 068 String randomKey = String.valueOf(rand.nextInt()); 069 for (int i = 0; i < POOL_SIZE - 1; i++) { 070 String randomValue = String.valueOf(rand.nextInt()); 071 runThread(randomKey, randomValue, randomValue); 072 assertEquals(0, poolMap.size(randomKey)); 073 } 074 assertEquals(0, poolMap.size(randomKey)); 075 } 076 077 @Test 078 public void testPoolCap() throws InterruptedException, ExecutionException { 079 Random rand = ThreadLocalRandom.current(); 080 // As long as we poll values we put, the pool size should remain zero 081 String randomKey = String.valueOf(rand.nextInt()); 082 List<String> randomValues = new ArrayList<>(); 083 for (int i = 0; i < POOL_SIZE * 2; i++) { 084 String randomValue = String.valueOf(rand.nextInt()); 085 randomValues.add(randomValue); 086 runThread(randomKey, randomValue, randomValue); 087 } 088 assertEquals(0, poolMap.size(randomKey)); 089 } 090}