BAEL 1639: added singleton examples and test. (#3977)

This commit is contained in:
Donato Rimenti
2018-04-21 18:51:14 +02:00
committed by Grzegorz Piwowarek
parent 5b5386636b
commit 5f83980363
6 changed files with 279 additions and 0 deletions
@@ -0,0 +1,119 @@
package com.baeldung.designpatterns.singleton.synchronization;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit tests for the singleton synchronization package with the same name.
*
* @author Donato Rimenti
*
*/
public class SingletonSynchronizationUnitTest {
/**
* Size of the thread pools used.
*/
private static final int POOL_SIZE = 1_000;
/**
* Number of tasks to submit.
*/
private static final int TASKS_TO_SUBMIT = 1_000_000;
/**
* Tests the thread-safety of {@link DraconianSingleton}.
*/
@Test
public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<DraconianSingleton>());
// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
executor.submit(() -> resultSet.add(DraconianSingleton.getInstance()));
}
// Since the instance of the object we inserted into the set is always
// the same, the size should be one.
Assert.assertEquals(1, resultSet.size());
}
/**
* Tests the thread-safety of {@link DclSingleton}.
*/
@Test
public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<DclSingleton>());
// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
executor.submit(() -> resultSet.add(DclSingleton.getInstance()));
}
// Since the instance of the object we inserted into the set is always
// the same, the size should be one.
Assert.assertEquals(1, resultSet.size());
}
/**
* Tests the thread-safety of {@link EarlyInitSingleton}.
*/
@Test
public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<EarlyInitSingleton>());
// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
executor.submit(() -> resultSet.add(EarlyInitSingleton.getInstance()));
}
// Since the instance of the object we inserted into the set is always
// the same, the size should be one.
Assert.assertEquals(1, resultSet.size());
}
/**
* Tests the thread-safety of {@link InitOnDemandSingleton}.
*/
@Test
public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<InitOnDemandSingleton>());
// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
executor.submit(() -> resultSet.add(InitOnDemandSingleton.getInstance()));
}
// Since the instance of the object we inserted into the set is always
// the same, the size should be one.
Assert.assertEquals(1, resultSet.size());
}
/**
* Tests the thread-safety of {@link EnumSingleton}.
*/
@Test
public void givenEnumSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<EnumSingleton>());
// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
executor.submit(() -> resultSet.add(EnumSingleton.INSTANCE));
}
// Since the instance of the object we inserted into the set is always
// the same, the size should be one.
Assert.assertEquals(1, resultSet.size());
}
}