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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotEquals;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.concurrent.Callable;
026import java.util.concurrent.ExecutorService;
027import java.util.concurrent.Executors;
028import java.util.concurrent.Future;
029import org.apache.hadoop.hbase.testclassification.MetricsTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034@Tag(MetricsTests.TAG)
035@Tag(SmallTests.TAG)
036public class TestCompatibilitySingletonFactory {
037
038  private static final int ITERATIONS = 100000;
039
040  private static class TestCompatibilitySingletonFactoryCallable implements Callable<String> {
041
042    @Override
043    public String call() throws Exception {
044      // XXX: Why is this sleep here?
045      Thread.sleep(10);
046      RandomStringGenerator instance =
047        CompatibilitySingletonFactory.getInstance(RandomStringGenerator.class);
048      return instance.getRandString();
049    }
050  }
051
052  @Test
053  public void testGetInstance() throws Exception {
054    List<TestCompatibilitySingletonFactoryCallable> callables = new ArrayList<>(ITERATIONS);
055    List<String> resultStrings = new ArrayList<>(ITERATIONS);
056
057    // Create the callables.
058    for (int i = 0; i < ITERATIONS; i++) {
059      callables.add(new TestCompatibilitySingletonFactoryCallable());
060    }
061
062    // Now run the callables.
063    ExecutorService executorService = Executors.newFixedThreadPool(100);
064    List<Future<String>> futures = executorService.invokeAll(callables);
065
066    // Wait for them all to finish.
067    for (Future<String> f : futures) {
068      resultStrings.add(f.get());
069    }
070
071    // Get the first string.
072    String firstString = resultStrings.get(0);
073
074    // Assert that all the strings are equal to the fist.
075    for (String s : resultStrings) {
076      assertEquals(firstString, s);
077    }
078
079    // an assert to make sure that RandomStringGeneratorImpl is generating random strings.
080    assertNotEquals(new RandomStringGeneratorImpl().getRandString(), firstString);
081  }
082}