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