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