View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Random;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  import java.util.concurrent.Future;
28  
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.testclassification.MetricsTests;
31  
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNotEquals;
37  
38  @Category({MetricsTests.class, SmallTests.class})
39  public class TestCompatibilitySingletonFactory {
40  
41    private static final int ITERATIONS = 100000;
42    private static final Random RANDOM = new Random();
43  
44    private class TestCompatibilitySingletonFactoryCallable implements Callable<String> {
45  
46      @Override
47      public String call() throws Exception {
48        Thread.sleep(RANDOM.nextInt(10));
49        RandomStringGenerator
50            instance =
51            CompatibilitySingletonFactory.getInstance(RandomStringGenerator.class);
52        return instance.getRandString();
53      }
54    }
55  
56    @Test
57    public void testGetInstance() throws Exception {
58      List<TestCompatibilitySingletonFactoryCallable> callables =
59          new ArrayList<TestCompatibilitySingletonFactoryCallable>(ITERATIONS);
60      List<String> resultStrings = new ArrayList<String>(ITERATIONS);
61  
62  
63      // Create the callables.
64      for (int i = 0; i < ITERATIONS; i++) {
65        callables.add(new TestCompatibilitySingletonFactoryCallable());
66      }
67  
68      // Now run the callables.
69      ExecutorService executorService = Executors.newFixedThreadPool(100);
70      List<Future<String>> futures = executorService.invokeAll(callables);
71  
72      // Wait for them all to finish.
73      for (Future<String> f : futures) {
74        resultStrings.add(f.get());
75      }
76  
77      // Get the first string.
78      String firstString = resultStrings.get(0);
79  
80  
81      // Assert that all the strings are equal to the fist.
82      for (String s : resultStrings) {
83        assertEquals(firstString, s);
84      }
85  
86      // an assert to make sure that RandomStringGeneratorImpl is generating random strings.
87      assertNotEquals(new RandomStringGeneratorImpl().getRandString(), firstString);
88    }
89  }