|
31 | 31 |
|
32 | 32 | package org.scijava.thread;
|
33 | 33 |
|
| 34 | +import static org.junit.Assert.assertFalse; |
| 35 | +import static org.junit.Assert.assertNotSame; |
34 | 36 | import static org.junit.Assert.assertSame;
|
| 37 | +import static org.junit.Assert.assertTrue; |
35 | 38 |
|
| 39 | +import java.lang.reflect.InvocationTargetException; |
36 | 40 | import java.util.concurrent.Callable;
|
| 41 | +import java.util.concurrent.ExecutionException; |
37 | 42 |
|
38 | 43 | import org.junit.After;
|
39 | 44 | import org.junit.Before;
|
@@ -61,6 +66,80 @@ public void tearDown() {
|
61 | 66 | context.dispose();
|
62 | 67 | }
|
63 | 68 |
|
| 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 | + |
64 | 143 | /**
|
65 | 144 | * Tests {@link ThreadService#getParent(Thread)} when called after
|
66 | 145 | * {@link ThreadService#invoke(Runnable)}.
|
|
0 commit comments