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; 021import static org.junit.Assert.assertFalse; 022 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.Iterator; 027import java.util.List; 028import java.util.Random; 029import java.util.concurrent.CompletableFuture; 030import java.util.concurrent.CompletionException; 031import java.util.concurrent.ExecutionException; 032import java.util.concurrent.ThreadLocalRandom; 033import java.util.concurrent.atomic.AtomicInteger; 034import org.apache.hadoop.hbase.HBaseClassTestRule; 035import org.apache.hadoop.hbase.testclassification.MiscTests; 036import org.apache.hadoop.hbase.testclassification.SmallTests; 037import org.apache.hadoop.hbase.util.PoolMap.PoolType; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042@Category({ MiscTests.class, SmallTests.class }) 043public class TestRoundRobinPoolMap extends PoolMapTestBase { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestRoundRobinPoolMap.class); 048 049 @Override 050 protected PoolType getPoolType() { 051 return PoolType.RoundRobin; 052 } 053 054 @Test 055 public void testGetOrCreate() throws IOException { 056 String key = "key"; 057 String value = "value"; 058 String result = poolMap.getOrCreate(key, () -> value); 059 060 assertEquals(value, result); 061 assertEquals(1, poolMap.values().size()); 062 } 063 064 @Test 065 public void testMultipleKeys() throws IOException { 066 for (int i = 0; i < KEY_COUNT; i++) { 067 String key = Integer.toString(i); 068 String value = Integer.toString(2 * i); 069 String result = poolMap.getOrCreate(key, () -> value); 070 071 assertEquals(value, result); 072 } 073 074 assertEquals(KEY_COUNT, poolMap.values().size()); 075 } 076 077 @Test 078 public void testMultipleValues() throws IOException { 079 String key = "key"; 080 081 for (int i = 0; i < POOL_SIZE; i++) { 082 String value = Integer.toString(i); 083 String result = poolMap.getOrCreate(key, () -> value); 084 085 assertEquals(value, result); 086 } 087 088 assertEquals(POOL_SIZE, poolMap.values().size()); 089 } 090 091 @Test 092 public void testRoundRobin() throws IOException { 093 String key = "key"; 094 095 for (int i = 0; i < POOL_SIZE; i++) { 096 String value = Integer.toString(i); 097 poolMap.getOrCreate(key, () -> value); 098 } 099 100 assertEquals(POOL_SIZE, poolMap.values().size()); 101 102 /* pool is filled, get() should return elements round robin order */ 103 for (int i = 0; i < 2 * POOL_SIZE; i++) { 104 String expected = Integer.toString(i % POOL_SIZE); 105 assertEquals(expected, poolMap.getOrCreate(key, () -> { 106 throw new IOException("must not call me"); 107 })); 108 } 109 110 assertEquals(POOL_SIZE, poolMap.values().size()); 111 } 112 113 @Test 114 public void testMultiThreadedRoundRobin() throws ExecutionException, InterruptedException { 115 String key = "key"; 116 AtomicInteger id = new AtomicInteger(); 117 List<String> results = Collections.synchronizedList(new ArrayList<>()); 118 119 Runnable runnable = () -> { 120 try { 121 for (int i = 0; i < POOL_SIZE; i++) { 122 String value = Integer.toString(id.getAndIncrement()); 123 String result = poolMap.getOrCreate(key, () -> value); 124 results.add(result); 125 126 Thread.yield(); 127 } 128 } catch (IOException e) { 129 throw new CompletionException(e); 130 } 131 }; 132 133 CompletableFuture<Void> future1 = CompletableFuture.runAsync(runnable); 134 CompletableFuture<Void> future2 = CompletableFuture.runAsync(runnable); 135 136 /* test for successful completion */ 137 future1.get(); 138 future2.get(); 139 140 assertEquals(POOL_SIZE, poolMap.values().size()); 141 142 /* check every elements occur twice */ 143 Collections.sort(results); 144 Iterator<String> iterator = results.iterator(); 145 146 for (int i = 0; i < POOL_SIZE; i++) { 147 String next1 = iterator.next(); 148 String next2 = iterator.next(); 149 assertEquals(next1, next2); 150 } 151 152 assertFalse(iterator.hasNext()); 153 } 154}