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.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertNotEquals;
33  
34  public class TestCompatibilitySingletonFactory {
35  
36    private static final int ITERATIONS = 100000;
37    private static final Random RANDOM = new Random();
38  
39    private class TestCompatibilitySingletonFactoryCallable implements Callable<String> {
40  
41      @Override
42      public String call() throws Exception {
43        Thread.sleep(RANDOM.nextInt(10));
44        RandomStringGenerator
45            instance =
46            CompatibilitySingletonFactory.getInstance(RandomStringGenerator.class);
47        return instance.getRandString();
48      }
49    }
50  
51    @Test
52    public void testGetInstance() throws Exception {
53      List<TestCompatibilitySingletonFactoryCallable> callables =
54          new ArrayList<TestCompatibilitySingletonFactoryCallable>(ITERATIONS);
55      List<String> resultStrings = new ArrayList<String>(ITERATIONS);
56  
57  
58      // Create the callables.
59      for (int i = 0; i < ITERATIONS; i++) {
60        callables.add(new TestCompatibilitySingletonFactoryCallable());
61      }
62  
63      // Now run the callables.
64      ExecutorService executorService = Executors.newFixedThreadPool(100);
65      List<Future<String>> futures = executorService.invokeAll(callables);
66  
67      // Wait for them all to finish.
68      for (Future<String> f : futures) {
69        resultStrings.add(f.get());
70      }
71  
72      // Get the first string.
73      String firstString = resultStrings.get(0);
74  
75  
76      // Assert that all the strings are equal to the fist.
77      for (String s : resultStrings) {
78        assertEquals(firstString, s);
79      }
80  
81      // an assert to make sure that RandomStringGeneratorImpl is generating random strings.
82      assertNotEquals(new RandomStringGeneratorImpl().getRandString(), firstString);
83    }
84  }