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.Random; 023import java.util.concurrent.ExecutionException; 024import java.util.concurrent.ThreadLocalRandom; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.hadoop.hbase.util.PoolMap.PoolType; 029import org.junit.ClassRule; 030import org.junit.Test; 031import org.junit.experimental.categories.Category; 032 033@Category({ MiscTests.class, SmallTests.class }) 034public class TestThreadLocalPoolMap extends PoolMapTestBase { 035 036 @ClassRule 037 public static final HBaseClassTestRule CLASS_RULE = 038 HBaseClassTestRule.forClass(TestThreadLocalPoolMap.class); 039 040 @Override 041 protected PoolType getPoolType() { 042 return PoolType.ThreadLocal; 043 } 044 045 @Test 046 public void testSingleThreadedClient() throws InterruptedException, ExecutionException { 047 Random rand = ThreadLocalRandom.current(); 048 String randomKey = String.valueOf(rand.nextInt()); 049 String randomValue = String.valueOf(rand.nextInt()); 050 // As long as the pool is not full, we should get back what we put 051 runThread(randomKey, randomValue, randomValue); 052 assertEquals(1, poolMap.size(randomKey)); 053 } 054 055 @Test 056 public void testMultiThreadedClients() throws InterruptedException, ExecutionException { 057 Random rand = ThreadLocalRandom.current(); 058 // As long as the pool is not full, we should get back what we put 059 for (int i = 0; i < POOL_SIZE; i++) { 060 String randomKey = String.valueOf(rand.nextInt()); 061 String randomValue = String.valueOf(rand.nextInt()); 062 runThread(randomKey, randomValue, randomValue); 063 assertEquals(1, poolMap.size(randomKey)); 064 } 065 String randomKey = String.valueOf(rand.nextInt()); 066 for (int i = 0; i < POOL_SIZE; i++) { 067 String randomValue = String.valueOf(rand.nextInt()); 068 runThread(randomKey, randomValue, randomValue); 069 assertEquals(i + 1, poolMap.size(randomKey)); 070 } 071 } 072 073 @Test 074 public void testPoolCap() throws InterruptedException, ExecutionException { 075 Random rand = ThreadLocalRandom.current(); 076 String randomKey = String.valueOf(rand.nextInt()); 077 for (int i = 0; i < POOL_SIZE * 2; i++) { 078 String randomValue = String.valueOf(rand.nextInt()); 079 // as of HBASE-4150, pool limit is no longer used with ThreadLocalPool 080 runThread(randomKey, randomValue, randomValue); 081 } 082 assertEquals(POOL_SIZE * 2, poolMap.size(randomKey)); 083 } 084}