ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionServiceTest.java
Revision: 1.3
Committed: Tue May 3 16:02:00 2005 UTC (19 years ago) by dl
Branch: MAIN
Changes since 1.2: +3 -3 lines
Log Message:
Fix some asserts and awaitUninterruptibly tests

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
13 import java.math.BigInteger;
14 import java.security.*;
15
16 public class ExecutorCompletionServiceTest extends JSR166TestCase{
17 public static void main(String[] args) {
18 junit.textui.TestRunner.run (suite());
19 }
20 public static Test suite() {
21 return new TestSuite(ExecutorCompletionServiceTest.class);
22 }
23
24
25 /**
26 * Creating a new ECS with null Executor throw NPE
27 */
28 public void testConstructorNPE() {
29 try {
30 ExecutorCompletionService ecs = new ExecutorCompletionService(null);
31 shouldThrow();
32 } catch (NullPointerException success) {
33 }
34 }
35
36 /**
37 * Creating a new ECS with null queue throw NPE
38 */
39 public void testConstructorNPE2() {
40 try {
41 ExecutorService e = Executors.newCachedThreadPool();
42 ExecutorCompletionService ecs = new ExecutorCompletionService(e, null);
43 shouldThrow();
44 } catch (NullPointerException success) {
45 }
46 }
47
48 /**
49 * Submitting a null callable throws NPE
50 */
51 public void testSubmitNPE() {
52 ExecutorService e = Executors.newCachedThreadPool();
53 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
54 try {
55 Callable c = null;
56 ecs.submit(c);
57 shouldThrow();
58 } catch (NullPointerException success) {
59 } finally {
60 joinPool(e);
61 }
62 }
63
64 /**
65 * Submitting a null runnable throws NPE
66 */
67 public void testSubmitNPE2() {
68 ExecutorService e = Executors.newCachedThreadPool();
69 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
70 try {
71 Runnable r = null;
72 ecs.submit(r, Boolean.TRUE);
73 shouldThrow();
74 } catch (NullPointerException success) {
75 } finally {
76 joinPool(e);
77 }
78 }
79
80 /**
81 * A taken submitted task is completed
82 */
83 public void testTake() {
84 ExecutorService e = Executors.newCachedThreadPool();
85 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
86 try {
87 Callable c = new StringTask();
88 ecs.submit(c);
89 Future f = ecs.take();
90 assertTrue(f.isDone());
91 } catch (Exception ex) {
92 unexpectedException();
93 } finally {
94 joinPool(e);
95 }
96 }
97
98 /**
99 * Take returns the same future object returned by submit
100 */
101 public void testTake2() {
102 ExecutorService e = Executors.newCachedThreadPool();
103 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
104 try {
105 Callable c = new StringTask();
106 Future f1 = ecs.submit(c);
107 Future f2 = ecs.take();
108 assertSame(f1, f2);
109 } catch (Exception ex) {
110 unexpectedException();
111 } finally {
112 joinPool(e);
113 }
114 }
115
116 /**
117 * If poll returns non-null, the returned task is completed
118 */
119 public void testPoll1() {
120 ExecutorService e = Executors.newCachedThreadPool();
121 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
122 try {
123 assertNull(ecs.poll());
124 Callable c = new StringTask();
125 ecs.submit(c);
126 Thread.sleep(SHORT_DELAY_MS);
127 for (;;) {
128 Future f = ecs.poll();
129 if (f != null) {
130 assertTrue(f.isDone());
131 break;
132 }
133 }
134 } catch (Exception ex) {
135 unexpectedException();
136 } finally {
137 joinPool(e);
138 }
139 }
140
141 /**
142 * If timed poll returns non-null, the returned task is completed
143 */
144 public void testPoll2() {
145 ExecutorService e = Executors.newCachedThreadPool();
146 ExecutorCompletionService ecs = new ExecutorCompletionService(e);
147 try {
148 assertNull(ecs.poll());
149 Callable c = new StringTask();
150 ecs.submit(c);
151 Future f = ecs.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
152 if (f != null)
153 assertTrue(f.isDone());
154 } catch (Exception ex) {
155 unexpectedException();
156 } finally {
157 joinPool(e);
158 }
159 }
160
161 }