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 TestRoundRobinPoolMap extends PoolMapTestBase { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestRoundRobinPoolMap.class); 041 042 @Override 043 protected PoolType getPoolType() { 044 return PoolType.RoundRobin; 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 the pool is not full, we'll get null back. 053 // This forces the user to create new values that can be used to populate 054 // the pool. 055 runThread(randomKey, randomValue, null); 056 assertEquals(1, poolMap.size(randomKey)); 057 } 058 059 @Test 060 public void testMultiThreadedClients() throws InterruptedException, ExecutionException { 061 Random rand = ThreadLocalRandom.current(); 062 for (int i = 0; i < POOL_SIZE; i++) { 063 String randomKey = String.valueOf(rand.nextInt()); 064 String randomValue = String.valueOf(rand.nextInt()); 065 // As long as the pool is not full, we'll get null back 066 runThread(randomKey, randomValue, null); 067 // As long as we use distinct keys, each pool will have one value 068 assertEquals(1, poolMap.size(randomKey)); 069 } 070 poolMap.clear(); 071 String randomKey = String.valueOf(rand.nextInt()); 072 for (int i = 0; i < POOL_SIZE - 1; i++) { 073 String randomValue = String.valueOf(rand.nextInt()); 074 // As long as the pool is not full, we'll get null back 075 runThread(randomKey, randomValue, null); 076 // since we use the same key, the pool size should grow 077 assertEquals(i + 1, poolMap.size(randomKey)); 078 } 079 // at the end of the day, there should be as many values as we put 080 assertEquals(POOL_SIZE - 1, poolMap.size(randomKey)); 081 } 082 083 @Test 084 public void testPoolCap() throws InterruptedException, ExecutionException { 085 Random rand = ThreadLocalRandom.current(); 086 String randomKey = String.valueOf(rand.nextInt()); 087 List<String> randomValues = new ArrayList<>(); 088 for (int i = 0; i < POOL_SIZE * 2; i++) { 089 String randomValue = String.valueOf(rand.nextInt()); 090 randomValues.add(randomValue); 091 if (i < POOL_SIZE - 1) { 092 // As long as the pool is not full, we'll get null back 093 runThread(randomKey, randomValue, null); 094 } else { 095 // when the pool becomes full, we expect the value we get back to be 096 // what we put earlier, in round-robin order 097 runThread(randomKey, randomValue, randomValues.get((i - POOL_SIZE + 1) % POOL_SIZE)); 098 } 099 } 100 assertEquals(POOL_SIZE, poolMap.size(randomKey)); 101 } 102}