Skip to content

Commit df3a6c5

Browse files
committed
ThreadServiceTest: test the other methods, too
1 parent 901154b commit df3a6c5

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/test/java/org/scijava/thread/ThreadServiceTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@
3131

3232
package org.scijava.thread;
3333

34+
import static org.junit.Assert.assertFalse;
35+
import static org.junit.Assert.assertNotSame;
3436
import static org.junit.Assert.assertSame;
37+
import static org.junit.Assert.assertTrue;
3538

39+
import java.lang.reflect.InvocationTargetException;
3640
import java.util.concurrent.Callable;
41+
import java.util.concurrent.ExecutionException;
3742

3843
import org.junit.After;
3944
import org.junit.Before;
@@ -61,6 +66,80 @@ public void tearDown() {
6166
context.dispose();
6267
}
6368

69+
/** Tests {@link ThreadService#run(Callable)}. */
70+
@Test
71+
public void testRunCallable() throws InterruptedException, ExecutionException
72+
{
73+
final Thread result = threadService.run(new Callable<Thread>() {
74+
75+
@Override
76+
public Thread call() {
77+
return Thread.currentThread();
78+
}
79+
}).get();
80+
assertNotSame(Thread.currentThread(), result);
81+
}
82+
83+
/** Tests {@link ThreadService#run(Runnable)}. */
84+
@Test
85+
public void testRunRunnable() throws InterruptedException, ExecutionException
86+
{
87+
final Thread[] results = new Thread[1];
88+
threadService.run(new Runnable() {
89+
90+
@Override
91+
public void run() {
92+
results[0] = Thread.currentThread();
93+
}
94+
}).get();
95+
assertNotSame(Thread.currentThread(), results[0]);
96+
}
97+
98+
/**
99+
* Tests {@link ThreadService#invoke(Runnable)} and
100+
* {@link ThreadService#isDispatchThread()}.
101+
*/
102+
@Test
103+
public void testInvoke() throws InterruptedException,
104+
InvocationTargetException
105+
{
106+
final boolean[] results = new boolean[1];
107+
threadService.invoke(new Runnable() {
108+
109+
@Override
110+
public void run() {
111+
results[0] = threadService.isDispatchThread();
112+
}
113+
});
114+
assertTrue(results[0]);
115+
assertFalse(threadService.isDispatchThread());
116+
}
117+
118+
/**
119+
* Tests {@link ThreadService#queue(Runnable)} and
120+
* {@link ThreadService#isDispatchThread()}.
121+
*/
122+
@Test
123+
public void testQueue() throws InterruptedException {
124+
final Object sync = new Object();
125+
final boolean[] results = new boolean[1];
126+
synchronized (sync) {
127+
threadService.queue(new Runnable() {
128+
129+
@Override
130+
public void run() {
131+
results[0] = threadService.isDispatchThread();
132+
synchronized (sync) {
133+
sync.notifyAll();
134+
}
135+
}
136+
});
137+
sync.wait();
138+
}
139+
assertTrue(results[0]);
140+
assertFalse(threadService.isDispatchThread());
141+
}
142+
64143
/**
65144
* Tests {@link ThreadService#getParent(Thread)} when called after
66145
* {@link ThreadService#invoke(Runnable)}.

0 commit comments

Comments
 (0)