1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59 for (int i = 0; i < ITERATIONS; i++) {
60 callables.add(new TestCompatibilitySingletonFactoryCallable());
61 }
62
63
64 ExecutorService executorService = Executors.newFixedThreadPool(100);
65 List<Future<String>> futures = executorService.invokeAll(callables);
66
67
68 for (Future<String> f : futures) {
69 resultStrings.add(f.get());
70 }
71
72
73 String firstString = resultStrings.get(0);
74
75
76
77 for (String s : resultStrings) {
78 assertEquals(firstString, s);
79 }
80
81
82 assertNotEquals(new RandomStringGeneratorImpl().getRandString(), firstString);
83 }
84 }