Add additional unit tests to show that singletons can be created in single thread environment and multithread environment. Also add a test to demonstrate a whole with Singleton when instantiating using reflection
This commit is contained in:
		@@ -2,73 +2,109 @@ package com.iluwatar.singleton;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.lang.reflect.Constructor;
 | 
				
			||||||
 | 
					import java.lang.reflect.Field;
 | 
				
			||||||
 | 
					import java.lang.reflect.InvocationTargetException;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.concurrent.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertEquals;
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertNull;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 | 
					 * This class provides several test case that test singleton construction.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * This test case demonstrates thread safety issues of lazy loaded Singleton implementation.
 | 
					 * The first proves that multiple calls to the singleton getInstance object are the same when called in the SAME thread.
 | 
				
			||||||
 * <p>
 | 
					 * The second proves that multiple calls to the singleton getInstance object are the same when called in the DIFFERENT thread.
 | 
				
			||||||
 * Out of the box you should see the test output something like the following:
 | 
					 * The third proves that there is a hole if the singleton is created reflectively
 | 
				
			||||||
 * <p>
 | 
					 | 
				
			||||||
 * Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * Thread=Thread-2 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * Thread=Thread-0 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@60f330b0
 | 
					 | 
				
			||||||
 * Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
 | 
					 | 
				
			||||||
 * <p>
 | 
					 | 
				
			||||||
 * By changing the method signature of LazyLoadedIvoryTower#getInstance from
 | 
					 | 
				
			||||||
 * <p><blockquote><pre>
 | 
					 | 
				
			||||||
 * 	 public static LazyLoadedIvoryTower getInstance()
 | 
					 | 
				
			||||||
 * </pre></blockquote><p>
 | 
					 | 
				
			||||||
 * into
 | 
					 | 
				
			||||||
 * <p><blockquote><pre>
 | 
					 | 
				
			||||||
 *   public synchronized static LazyLoadedIvoryTower getInstance()
 | 
					 | 
				
			||||||
 * </pre></blockquote><p>
 | 
					 | 
				
			||||||
 * you should see the test output change to something like the following:
 | 
					 | 
				
			||||||
 * <p>
 | 
					 | 
				
			||||||
 * Thread=Thread-4 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 * Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 * Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 * Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 * Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 * Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
 | 
					 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class LazyLoadedSingletonThreadSafetyTest {
 | 
					public class LazyLoadedSingletonThreadSafetyTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final int NUM_THREADS = 5;
 | 
					    private static final int NUM_THREADS = 5;
 | 
				
			||||||
 | 
					    private List<ThreadSafeLazyLoadedIvoryTower> threadObjects = new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Test
 | 
					    //NullObject class so Callable has to return something
 | 
				
			||||||
	public void test() {
 | 
					    private class NullObject{private NullObject(){}}
 | 
				
			||||||
		SingletonThread runnable = new SingletonThread();
 | 
					 | 
				
			||||||
		for (int j=0; j<NUM_THREADS; j++) {
 | 
					 | 
				
			||||||
			Thread thread = new Thread(runnable);
 | 
					 | 
				
			||||||
			thread.start();
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static class SingletonThread implements Runnable {
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void test_MultipleCallsReturnTheSameObjectInSameThread() {
 | 
				
			||||||
 | 
					        //Create several instances in the same calling thread
 | 
				
			||||||
 | 
					        ThreadSafeLazyLoadedIvoryTower instance1 = ThreadSafeLazyLoadedIvoryTower.getInstance();
 | 
				
			||||||
 | 
					        ThreadSafeLazyLoadedIvoryTower instance2 = ThreadSafeLazyLoadedIvoryTower.getInstance();
 | 
				
			||||||
 | 
					        ThreadSafeLazyLoadedIvoryTower instance3 = ThreadSafeLazyLoadedIvoryTower.getInstance();
 | 
				
			||||||
 | 
					        //now check they are equal
 | 
				
			||||||
 | 
					        assertEquals(instance1, instance1);
 | 
				
			||||||
 | 
					        assertEquals(instance1, instance2);
 | 
				
			||||||
 | 
					        assertEquals(instance2, instance3);
 | 
				
			||||||
 | 
					        assertEquals(instance1, instance3);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Override
 | 
					    @Test
 | 
				
			||||||
		public void run() {
 | 
					    public void test_MultipleCallsReturnTheSameObjectInDifferentThreads() throws InterruptedException, ExecutionException {
 | 
				
			||||||
			LazyLoadedIvoryTower instance = LazyLoadedIvoryTower.getInstance();
 | 
					        {//create several threads and inside each callable instantiate the singleton class
 | 
				
			||||||
			System.out.println("Thread=" + Thread.currentThread().getName() + " got instance=" + instance);
 | 
					            ExecutorService executorService = Executors.newSingleThreadExecutor();
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static class LazyLoadedIvoryTower {
 | 
					            List<Callable<NullObject>> threadList = new ArrayList<>();
 | 
				
			||||||
 | 
					            for (int i = 0; i < NUM_THREADS; i++) {
 | 
				
			||||||
 | 
					                threadList.add(new SingletonCreatingThread());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private static LazyLoadedIvoryTower instance = null;
 | 
					            ExecutorService service = Executors.newCachedThreadPool();
 | 
				
			||||||
 | 
					            List<Future<NullObject>> results = service.invokeAll(threadList);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private LazyLoadedIvoryTower() {
 | 
					            //wait for all of the threads to complete
 | 
				
			||||||
		}
 | 
					            for (Future res : results) {
 | 
				
			||||||
 | 
					                res.get();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public static LazyLoadedIvoryTower getInstance() {
 | 
					            //tidy up the executor
 | 
				
			||||||
			if (instance == null) {
 | 
					            executorService.shutdown();
 | 
				
			||||||
				instance = new LazyLoadedIvoryTower();
 | 
					        }
 | 
				
			||||||
				System.out.println("Thread=" + Thread.currentThread().getName() + " creating instance=" + instance);
 | 
					        {//now check the contents that were added to threadObjects by each thread
 | 
				
			||||||
			}
 | 
					            assertEquals(NUM_THREADS, threadObjects.size());
 | 
				
			||||||
			return instance;
 | 
					            assertEquals(threadObjects.get(0), threadObjects.get(1));
 | 
				
			||||||
		}
 | 
					            assertEquals(threadObjects.get(1), threadObjects.get(2));
 | 
				
			||||||
	}
 | 
					            assertEquals(threadObjects.get(2), threadObjects.get(3));
 | 
				
			||||||
 | 
					            assertEquals(threadObjects.get(3), threadObjects.get(4));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    @SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
					    public void test_HoleInSingletonCreationIfUsingReflection() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
 | 
				
			||||||
 | 
					        Field[] f = ThreadSafeLazyLoadedIvoryTower.class.getDeclaredFields();
 | 
				
			||||||
 | 
					        assertEquals("One field only in ThreadSafeLazyLoadedIvoryTower", 1, f.length);
 | 
				
			||||||
 | 
					        f[0].setAccessible(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        {//reflectively create an object - the singleton field is null
 | 
				
			||||||
 | 
					            Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower");
 | 
				
			||||||
 | 
					            Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
 | 
				
			||||||
 | 
					            constructor.setAccessible(true);
 | 
				
			||||||
 | 
					            ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
 | 
				
			||||||
 | 
					            assertNull(f[0].get(instance));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //instantiate the singleton but when we do the below code we are creating a new object where it is set to null still
 | 
				
			||||||
 | 
					        IvoryTower.getInstance();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        {//reflectively create an object - the singleton field is null as a new object is created
 | 
				
			||||||
 | 
					            Class lazyIvoryTowerClazz = Class.forName("com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower");
 | 
				
			||||||
 | 
					            Constructor<ThreadSafeLazyLoadedIvoryTower> constructor = lazyIvoryTowerClazz.getDeclaredConstructor();
 | 
				
			||||||
 | 
					            constructor.setAccessible(true);
 | 
				
			||||||
 | 
					            ThreadSafeLazyLoadedIvoryTower instance = constructor.newInstance();
 | 
				
			||||||
 | 
					            assertNull(f[0].get(instance));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private class SingletonCreatingThread implements Callable<NullObject> {
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public NullObject call() {
 | 
				
			||||||
 | 
					            //instantiate the thread safety class and add to list to test afterwards
 | 
				
			||||||
 | 
					            ThreadSafeLazyLoadedIvoryTower instance = ThreadSafeLazyLoadedIvoryTower.getInstance();
 | 
				
			||||||
 | 
					            threadObjects.add(instance);
 | 
				
			||||||
 | 
					            return new NullObject();//return null object (cannot return Void)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user