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