Migrate to JUnit5
This commit is contained in:
		| @@ -34,8 +34,13 @@ | ||||
|   <artifactId>singleton</artifactId> | ||||
|   <dependencies> | ||||
|     <dependency> | ||||
|       <groupId>junit</groupId> | ||||
|       <artifactId>junit</artifactId> | ||||
|       <groupId>org.junit.jupiter</groupId> | ||||
|       <artifactId>junit-jupiter-api</artifactId> | ||||
|       <scope>test</scope> | ||||
|     </dependency> | ||||
|     <dependency> | ||||
|       <groupId>org.junit.jupiter</groupId> | ||||
|       <artifactId>junit-jupiter-engine</artifactId> | ||||
|       <scope>test</scope> | ||||
|     </dependency> | ||||
|   </dependencies> | ||||
|   | ||||
| @@ -22,7 +22,7 @@ | ||||
|  */ | ||||
| package com.iluwatar.singleton; | ||||
|  | ||||
| import org.junit.Test; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|   | ||||
| @@ -22,8 +22,8 @@ | ||||
|  */ | ||||
| package com.iluwatar.singleton; | ||||
|  | ||||
| import static org.junit.Assert.assertNotNull; | ||||
| import static org.junit.Assert.assertSame; | ||||
| import org.junit.jupiter.api.Disabled; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| @@ -32,7 +32,11 @@ import java.util.concurrent.ExecutorService; | ||||
| import java.util.concurrent.Executors; | ||||
| import java.util.concurrent.Future; | ||||
| import java.util.function.Supplier; | ||||
| import org.junit.Test; | ||||
|  | ||||
| import static java.time.Duration.ofMillis; | ||||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||||
| import static org.junit.jupiter.api.Assertions.assertTimeout; | ||||
|  | ||||
| /** | ||||
|  * This class provides several test case that test singleton construction. | ||||
| @@ -80,29 +84,30 @@ public abstract class SingletonTest<S> { | ||||
|   /** | ||||
|    * Test singleton instance in a concurrent setting | ||||
|    */ | ||||
|   @Test(timeout = 10000) | ||||
|   @Test | ||||
|   public void testMultipleCallsReturnTheSameObjectInDifferentThreads() throws Exception { | ||||
|     assertTimeout(ofMillis(10000), () -> { | ||||
|       // Create 10000 tasks and inside each callable instantiate the singleton class | ||||
|       final List<Callable<S>> tasks = new ArrayList<>(); | ||||
|       for (int i = 0; i < 10000; i++) { | ||||
|         tasks.add(this.singletonInstanceMethod::get); | ||||
|       } | ||||
|  | ||||
|     // Create 10000 tasks and inside each callable instantiate the singleton class | ||||
|     final List<Callable<S>> tasks = new ArrayList<>(); | ||||
|     for (int i = 0; i < 10000; i++) { | ||||
|       tasks.add(this.singletonInstanceMethod::get); | ||||
|     } | ||||
|       // Use up to 8 concurrent threads to handle the tasks | ||||
|       final ExecutorService executorService = Executors.newFixedThreadPool(8); | ||||
|       final List<Future<S>> results = executorService.invokeAll(tasks); | ||||
|  | ||||
|     // Use up to 8 concurrent threads to handle the tasks | ||||
|     final ExecutorService executorService = Executors.newFixedThreadPool(8); | ||||
|     final List<Future<S>> results = executorService.invokeAll(tasks); | ||||
|       // wait for all of the threads to complete | ||||
|       final S expectedInstance = this.singletonInstanceMethod.get(); | ||||
|       for (Future<S> res : results) { | ||||
|         final S instance = res.get(); | ||||
|         assertNotNull(instance); | ||||
|         assertSame(expectedInstance, instance); | ||||
|       } | ||||
|  | ||||
|     // wait for all of the threads to complete | ||||
|     final S expectedInstance = this.singletonInstanceMethod.get(); | ||||
|     for (Future<S> res : results) { | ||||
|       final S instance = res.get(); | ||||
|       assertNotNull(instance); | ||||
|       assertSame(expectedInstance, instance); | ||||
|     } | ||||
|  | ||||
|     // tidy up the executor | ||||
|     executorService.shutdown(); | ||||
|       // tidy up the executor | ||||
|       executorService.shutdown(); | ||||
|     }); | ||||
|  | ||||
|   } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user