|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * <p> |
| 4 | + * Copyright (c) 2017 James |
| 5 | + * <p> |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * <p> |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * <p> |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package me.zbl.singleton; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.*; |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +import static org.junit.Assert.assertNotNull; |
| 34 | +import static org.junit.Assert.assertSame; |
| 35 | + |
| 36 | +/** |
| 37 | + * 测试单例模式 |
| 38 | + * <p> |
| 39 | + * 提供测试单例结构的测试用例 |
| 40 | + * <p> |
| 41 | + * 第一个验证,在同一个线程中,对 getInstance() 的多次调用结果是相同的 |
| 42 | + * 第二个验证,在不同的线程中,对 getInstance() 的多次调用结果也是一样的 |
| 43 | + */ |
| 44 | +public abstract class SingletonTest<S> { |
| 45 | + |
| 46 | + /** |
| 47 | + * 获取单例实例的方法 |
| 48 | + */ |
| 49 | + private final Supplier<S> singletonInstanceMethod; |
| 50 | + |
| 51 | + /** |
| 52 | + * 创建一个测试用例 |
| 53 | + * |
| 54 | + * @param singletonInstanceMethod 实例类中获取实例的方法 |
| 55 | + */ |
| 56 | + public SingletonTest(final Supplier<S> singletonInstanceMethod) { |
| 57 | + this.singletonInstanceMethod = singletonInstanceMethod; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * 测试在同一线程中的多次调用返回结果是相同的 |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void testMultipleCallsReturnSameObjectInSameThread() { |
| 65 | + // 在同一线程中创建多个实例 |
| 66 | + S instance1 = singletonInstanceMethod.get(); |
| 67 | + S instance2 = singletonInstanceMethod.get(); |
| 68 | + S instance3 = singletonInstanceMethod.get(); |
| 69 | + |
| 70 | + // 分别验证是否相同 |
| 71 | + assertSame(instance1, instance2); |
| 72 | + assertSame(instance2, instance3); |
| 73 | + assertSame(instance3, instance1); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 测试在不同线程中的多次调用返回的结果也是相同的 |
| 78 | + */ |
| 79 | + @Test(timeout = 5000L) |
| 80 | + public void testMultipleCallsReturnSameObjectInMultipleThread() throws InterruptedException, ExecutionException { |
| 81 | + |
| 82 | + // 创建 5000 个任务,并在每个任务中实例化单例类 |
| 83 | + List<Callable<S>> tasks = new ArrayList<>(); |
| 84 | + for (int i = 0; i < 5000; i++) { |
| 85 | + tasks.add(singletonInstanceMethod::get); |
| 86 | + } |
| 87 | + |
| 88 | + // 使用 10 个不同的线程来分别执行这 5000 个任务 |
| 89 | + final ExecutorService pool = Executors.newFixedThreadPool(10); |
| 90 | + final List<Future<S>> results = pool.invokeAll(tasks); |
| 91 | + |
| 92 | + // 预期结果 |
| 93 | + S expectedResult = singletonInstanceMethod.get(); |
| 94 | + |
| 95 | + // 分别验证是否是同一个对象 |
| 96 | + for (Future<S> res : results) { |
| 97 | + final S result = res.get(); |
| 98 | + assertNotNull(result); |
| 99 | + assertSame(expectedResult, result); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments