Skip to content

Commit 8eff279

Browse files
committed
Merge pull request iluwatar#263 from iamrichardjones/master
Update to singleton unit tests and lazy loading unit test
2 parents b961c57 + 7ab799c commit 8eff279

File tree

3 files changed

+110
-61
lines changed

3 files changed

+110
-61
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.iluwatar.lazy.loading;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Field;
6+
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.assertNull;
9+
10+
/**
11+
* Using reflection this test shows that the heavy field is not instantiated until the method getHeavy is called
12+
*
13+
* Created by jones on 11/10/2015.
14+
*/
15+
public class HolderThreadSafeTest {
16+
17+
@Test
18+
public void test() throws IllegalAccessException {
19+
HolderThreadSafe hts = new HolderThreadSafe();
20+
21+
{//first call is null
22+
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
23+
for (Field f: ff) {
24+
f.setAccessible(true);
25+
}
26+
27+
assertNull(ff[0].get(hts));
28+
}
29+
30+
// now it is lazily loaded
31+
hts.getHeavy();
32+
33+
{//now it is not null - call via reflection so that the test is the same before and after
34+
Field[] ff = HolderThreadSafe.class.getDeclaredFields();
35+
for (Field f: ff) {
36+
f.setAccessible(true);
37+
}
38+
39+
assertNotNull(ff[0].get(hts));
40+
}
41+
}
42+
}

singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Thread-safe Singleton class.
55
* The instance is lazily initialized and thus needs synchronization
66
* mechanism.
7+
*
8+
* Note: if created by reflection then a singleton will not be created but multiple options in the same classloader
79
*/
810
public class ThreadSafeLazyLoadedIvoryTower {
911

singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,78 @@
22

33
import org.junit.Test;
44

5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.concurrent.*;
9+
10+
import static org.junit.Assert.assertEquals;
11+
512
/**
6-
*
7-
* This test case demonstrates thread safety issues of lazy loaded Singleton implementation.
8-
* <p>
9-
* Out of the box you should see the test output something like the following:
10-
* <p>
11-
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
12-
* Thread=Thread-2 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
13-
* Thread=Thread-0 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
14-
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
15-
* Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
16-
* Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@60f330b0
17-
* Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
18-
* <p>
19-
* By changing the method signature of LazyLoadedIvoryTower#getInstance from
20-
* <p><blockquote><pre>
21-
* public static LazyLoadedIvoryTower getInstance()
22-
* </pre></blockquote><p>
23-
* into
24-
* <p><blockquote><pre>
25-
* public synchronized static LazyLoadedIvoryTower getInstance()
26-
* </pre></blockquote><p>
27-
* you should see the test output change to something like the following:
28-
* <p>
29-
* Thread=Thread-4 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
30-
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
31-
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
32-
* Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
33-
* Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
34-
* Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
13+
* This class provides several test case that test singleton construction.
14+
*
15+
* The first proves that multiple calls to the singleton getInstance object are the same when called in the SAME thread.
16+
* The second proves that multiple calls to the singleton getInstance object are the same when called in the DIFFERENT thread.
3517
*
3618
*/
3719
public class LazyLoadedSingletonThreadSafetyTest {
3820

39-
private static final int NUM_THREADS = 5;
40-
41-
@Test
42-
public void test() {
43-
SingletonThread runnable = new SingletonThread();
44-
for (int j=0; j<NUM_THREADS; j++) {
45-
Thread thread = new Thread(runnable);
46-
thread.start();
47-
}
48-
}
49-
50-
private static class SingletonThread implements Runnable {
21+
private static final int NUM_THREADS = 5;
22+
private List<ThreadSafeLazyLoadedIvoryTower> threadObjects = Collections.synchronizedList(new ArrayList<>());
23+
24+
//NullObject class so Callable has to return something
25+
private class NullObject{private NullObject(){}}
26+
27+
@Test
28+
public void test_MultipleCallsReturnTheSameObjectInSameThread() {
29+
//Create several instances in the same calling thread
30+
ThreadSafeLazyLoadedIvoryTower instance1 = ThreadSafeLazyLoadedIvoryTower.getInstance();
31+
ThreadSafeLazyLoadedIvoryTower instance2 = ThreadSafeLazyLoadedIvoryTower.getInstance();
32+
ThreadSafeLazyLoadedIvoryTower instance3 = ThreadSafeLazyLoadedIvoryTower.getInstance();
33+
//now check they are equal
34+
assertEquals(instance1, instance1);
35+
assertEquals(instance1, instance2);
36+
assertEquals(instance2, instance3);
37+
assertEquals(instance1, instance3);
38+
}
39+
40+
@Test
41+
public void test_MultipleCallsReturnTheSameObjectInDifferentThreads() throws InterruptedException, ExecutionException {
42+
{//create several threads and inside each callable instantiate the singleton class
43+
ExecutorService executorService = Executors.newSingleThreadExecutor();
44+
45+
List<Callable<NullObject>> threadList = new ArrayList<>();
46+
for (int i = 0; i < NUM_THREADS; i++) {
47+
threadList.add(new SingletonCreatingThread());
48+
}
49+
50+
ExecutorService service = Executors.newCachedThreadPool();
51+
List<Future<NullObject>> results = service.invokeAll(threadList);
5152

52-
@Override
53-
public void run() {
54-
LazyLoadedIvoryTower instance = LazyLoadedIvoryTower.getInstance();
55-
System.out.println("Thread=" + Thread.currentThread().getName() + " got instance=" + instance);
56-
}
57-
}
58-
59-
private static class LazyLoadedIvoryTower {
53+
//wait for all of the threads to complete
54+
for (Future res : results) {
55+
res.get();
56+
}
6057

61-
private static LazyLoadedIvoryTower instance = null;
62-
63-
private LazyLoadedIvoryTower() {
64-
}
58+
//tidy up the executor
59+
executorService.shutdown();
60+
}
61+
{//now check the contents that were added to threadObjects by each thread
62+
assertEquals(NUM_THREADS, threadObjects.size());
63+
assertEquals(threadObjects.get(0), threadObjects.get(1));
64+
assertEquals(threadObjects.get(1), threadObjects.get(2));
65+
assertEquals(threadObjects.get(2), threadObjects.get(3));
66+
assertEquals(threadObjects.get(3), threadObjects.get(4));
67+
}
68+
}
6569

66-
public static LazyLoadedIvoryTower getInstance() {
67-
if (instance == null) {
68-
instance = new LazyLoadedIvoryTower();
69-
System.out.println("Thread=" + Thread.currentThread().getName() + " creating instance=" + instance);
70-
}
71-
return instance;
72-
}
73-
}
70+
private class SingletonCreatingThread implements Callable<NullObject> {
71+
@Override
72+
public NullObject call() {
73+
//instantiate the thread safety class and add to list to test afterwards
74+
ThreadSafeLazyLoadedIvoryTower instance = ThreadSafeLazyLoadedIvoryTower.getInstance();
75+
threadObjects.add(instance);
76+
return new NullObject();//return null object (cannot return Void)
77+
}
78+
}
7479
}

0 commit comments

Comments
 (0)