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.io.IOException;
023import java.util.Random;
024import java.util.concurrent.CompletableFuture;
025import java.util.concurrent.CompletionException;
026import java.util.concurrent.ExecutionException;
027import java.util.concurrent.ThreadLocalRandom;
028import java.util.concurrent.atomic.AtomicInteger;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.PoolMap.PoolType;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ MiscTests.class, SmallTests.class })
038public class TestThreadLocalPoolMap extends PoolMapTestBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestThreadLocalPoolMap.class);
043
044  @Override
045  protected PoolType getPoolType() {
046    return PoolType.ThreadLocal;
047  }
048
049  @Test
050  public void testGetOrCreate() throws IOException {
051    String key = "key";
052    String value = "value";
053    String result = poolMap.getOrCreate(key, () -> value);
054
055    assertEquals(value, result);
056    assertEquals(1, poolMap.values().size());
057  }
058
059  @Test
060  public void testMultipleKeys() throws IOException {
061    for (int i = 0; i < KEY_COUNT; i++) {
062      String key = Integer.toString(i);
063      String value = Integer.toString(2 * i);
064      String result = poolMap.getOrCreate(key, () -> value);
065
066      assertEquals(value, result);
067    }
068
069    assertEquals(KEY_COUNT, poolMap.values().size());
070  }
071
072  @Test
073  public void testFull() throws IOException {
074    String key = "key";
075    String value = "value";
076
077    String result = poolMap.getOrCreate(key, () -> value);
078    assertEquals(value, result);
079
080    String result2 = poolMap.getOrCreate(key, () -> {
081      throw new IOException("must not call me");
082    });
083
084    assertEquals(value, result2);
085    assertEquals(1, poolMap.values().size());
086  }
087
088  @Test
089  public void testLocality() throws ExecutionException, InterruptedException {
090    String key = "key";
091    AtomicInteger id = new AtomicInteger();
092
093    Runnable runnable = () -> {
094      try {
095        String myId = Integer.toString(id.getAndIncrement());
096
097        for (int i = 0; i < 3; i++) {
098          String result = poolMap.getOrCreate(key, () -> myId);
099          assertEquals(myId, result);
100
101          Thread.yield();
102        }
103      } catch (IOException e) {
104        throw new CompletionException(e);
105      }
106    };
107
108    CompletableFuture<Void> future1 = CompletableFuture.runAsync(runnable);
109    CompletableFuture<Void> future2 = CompletableFuture.runAsync(runnable);
110
111    /* test for successful completion */
112    future1.get();
113    future2.get();
114
115    assertEquals(2, poolMap.values().size());
116  }
117}