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