Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2009, 2010, 2012, 2013 Ericsson |
96b353c5 | 3 | * |
d18dd09b ASL |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
96b353c5 | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.request; |
d18dd09b | 15 | |
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertFalse; | |
18 | import static org.junit.Assert.assertTrue; | |
d18dd09b | 19 | |
2f185a65 | 20 | import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread; |
8fd82db5 | 21 | import org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor; |
2f185a65 FC |
22 | import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider; |
23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; | |
5419a136 AM |
25 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
26 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType; | |
27 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; | |
2f185a65 FC |
28 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignal; |
29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; | |
5c00c0b7 | 31 | |
6e1886bc AM |
32 | import org.junit.After; |
33 | import org.junit.Before; | |
34 | import org.junit.Test; | |
35 | ||
d18dd09b | 36 | /** |
d18dd09b ASL |
37 | * Test suite for the TmfRequestExecutor class. |
38 | */ | |
5419a136 | 39 | @SuppressWarnings({ "nls" }) |
6e1886bc | 40 | public class TmfRequestExecutorTest { |
d18dd09b | 41 | |
96b353c5 FC |
42 | // ------------------------------------------------------------------------ |
43 | // Variables | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
46 | private TmfRequestExecutor fExecutor; | |
5c00c0b7 | 47 | |
6e1886bc AM |
48 | // ------------------------------------------------------------------------ |
49 | // Housekeeping | |
50 | // ------------------------------------------------------------------------ | |
d18dd09b | 51 | |
6e1886bc AM |
52 | /** |
53 | * Setup | |
54 | */ | |
55 | @Before | |
56 | public void setUp() { | |
96b353c5 | 57 | fExecutor = new TmfRequestExecutor(); |
96b353c5 FC |
58 | } |
59 | ||
6e1886bc AM |
60 | /** |
61 | * Cleanup | |
62 | */ | |
63 | @After | |
64 | public void tearDown() { | |
96b353c5 FC |
65 | fExecutor.stop(); |
66 | } | |
d18dd09b | 67 | |
6e1886bc AM |
68 | // ------------------------------------------------------------------------ |
69 | // Constructors | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
72 | /** | |
73 | * Test method for | |
74 | * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#TmfRequestExecutor()} | |
75 | */ | |
76 | @Test | |
77 | public void testTmfRequestExecutor() { | |
78 | TmfRequestExecutor executor = new TmfRequestExecutor(); | |
79 | assertFalse("isShutdown", executor.isShutdown()); | |
80 | assertFalse("isTerminated", executor.isTerminated()); | |
81 | } | |
82 | ||
83 | /** | |
84 | * Test method for | |
85 | * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#stop()} | |
86 | */ | |
87 | @Test | |
88 | public void testStop() { | |
89 | TmfRequestExecutor executor = new TmfRequestExecutor(); | |
90 | executor.stop(); | |
91 | assertTrue("isShutdown", executor.isShutdown()); | |
92 | assertTrue("isTerminated", executor.isTerminated()); | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // execute | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
99 | // Dummy context | |
100 | private static class MyContext implements ITmfContext { | |
101 | private long fNbRequested; | |
2f185a65 FC |
102 | private long fRank; |
103 | ||
104 | public MyContext(long requested) { | |
105 | fNbRequested = requested; | |
106 | fRank = 0; | |
107 | } | |
6e1886bc | 108 | |
2f185a65 FC |
109 | @Override |
110 | public long getRank() { | |
111 | return (fRank <= fNbRequested) ? fRank : -1; | |
112 | } | |
6e1886bc | 113 | |
2f185a65 FC |
114 | @Override |
115 | public ITmfLocation getLocation() { | |
116 | return null; | |
117 | } | |
6e1886bc | 118 | |
2f185a65 FC |
119 | @Override |
120 | public boolean hasValidRank() { | |
121 | return true; | |
122 | } | |
6e1886bc | 123 | |
2f185a65 FC |
124 | @Override |
125 | public void setLocation(ITmfLocation location) { | |
126 | } | |
6e1886bc | 127 | |
2f185a65 FC |
128 | @Override |
129 | public void setRank(long rank) { | |
130 | fRank = rank; | |
131 | } | |
6e1886bc | 132 | |
2f185a65 FC |
133 | @Override |
134 | public void increaseRank() { | |
135 | fRank++; | |
136 | } | |
6e1886bc | 137 | |
2f185a65 FC |
138 | @Override |
139 | public void dispose() { | |
140 | } | |
6e1886bc | 141 | |
2f185a65 FC |
142 | @Override |
143 | public MyContext clone() { | |
144 | return this; | |
145 | } | |
6e1886bc | 146 | } |
2f185a65 | 147 | |
6e1886bc AM |
148 | // Dummy provider |
149 | private static class MyProvider extends TmfDataProvider { | |
150 | private ITmfEvent fEvent = new TmfEvent(); | |
2f185a65 FC |
151 | |
152 | @Override | |
153 | public String getName() { | |
154 | return null; | |
155 | } | |
6e1886bc | 156 | |
2f185a65 FC |
157 | @Override |
158 | public void dispose() { | |
159 | } | |
6e1886bc | 160 | |
2f185a65 FC |
161 | @Override |
162 | public void broadcast(TmfSignal signal) { | |
163 | } | |
6e1886bc | 164 | |
2f185a65 | 165 | @Override |
5419a136 | 166 | public void sendRequest(ITmfDataRequest request) { |
2f185a65 | 167 | } |
6e1886bc | 168 | |
2f185a65 FC |
169 | @Override |
170 | public void fireRequest() { | |
171 | } | |
6e1886bc | 172 | |
2f185a65 FC |
173 | @Override |
174 | public void notifyPendingRequest(boolean isIncrement) { | |
175 | } | |
6e1886bc | 176 | |
2f185a65 FC |
177 | @Override |
178 | public ITmfEvent getNext(ITmfContext context) { | |
179 | context.increaseRank(); | |
180 | return context.getRank() >= 0 ? fEvent : null; | |
181 | } | |
6e1886bc | 182 | |
2f185a65 | 183 | @Override |
5419a136 | 184 | public ITmfContext armRequest(ITmfDataRequest request) { |
2f185a65 FC |
185 | return new MyContext(request.getNbRequested()); |
186 | } | |
6e1886bc | 187 | } |
2f185a65 | 188 | |
6e1886bc | 189 | // Dummy request |
5419a136 AM |
190 | private static class MyRequest extends TmfDataRequest { |
191 | public MyRequest(ExecutionType priority, int requested) { | |
192 | super(ITmfEvent.class, 0, requested, priority); | |
2f185a65 | 193 | } |
6e1886bc | 194 | |
2f185a65 | 195 | @Override |
5419a136 | 196 | public void done() { |
2f185a65 FC |
197 | synchronized (monitor) { |
198 | monitor.notifyAll(); | |
199 | } | |
200 | } | |
201 | } | |
202 | ||
203 | // Dummy thread | |
204 | private static class MyThread extends TmfEventThread { | |
5419a136 | 205 | public MyThread(TmfDataProvider provider, ITmfDataRequest request) { |
2f185a65 | 206 | super(provider, request); |
2f185a65 FC |
207 | } |
208 | } | |
209 | ||
5419a136 | 210 | private final static Object monitor = new Object(); |
2f185a65 FC |
211 | |
212 | /** | |
6e1886bc AM |
213 | * Test method for |
214 | * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#execute(java.lang.Runnable)} | |
215 | */ | |
216 | @Test | |
217 | public void testExecute() { | |
2f185a65 | 218 | MyProvider provider = new MyProvider(); |
6e1886bc AM |
219 | MyRequest request1 = new MyRequest(ExecutionType.BACKGROUND, Integer.MAX_VALUE / 5); |
220 | MyThread thread1 = new MyThread(provider, request1); | |
221 | MyRequest request2 = new MyRequest(ExecutionType.FOREGROUND, Integer.MAX_VALUE / 10); | |
222 | MyThread thread2 = new MyThread(provider, request2); | |
223 | MyRequest request3 = new MyRequest(ExecutionType.FOREGROUND, Integer.MAX_VALUE / 10); | |
224 | MyThread thread3 = new MyThread(provider, request3); | |
2f185a65 FC |
225 | |
226 | // Start thread1 | |
5419a136 AM |
227 | fExecutor.execute(thread1); |
228 | try { | |
229 | Thread.sleep(1000); | |
230 | } catch (InterruptedException e) { | |
2f185a65 | 231 | } |
6e1886bc | 232 | assertTrue("isRunning", thread1.isRunning()); |
2f185a65 FC |
233 | |
234 | // Start higher priority thread2 | |
5419a136 AM |
235 | fExecutor.execute(thread2); |
236 | try { | |
237 | Thread.sleep(1000); | |
238 | } catch (InterruptedException e) { | |
2f185a65 | 239 | } |
5419a136 AM |
240 | assertFalse("isRunning", thread1.isRunning()); |
241 | assertTrue("isRunning", thread2.isRunning()); | |
2f185a65 FC |
242 | |
243 | // Wait for end of thread2 | |
244 | try { | |
5419a136 AM |
245 | synchronized (monitor) { |
246 | monitor.wait(); | |
247 | Thread.sleep(1000); | |
2f185a65 FC |
248 | } |
249 | } catch (InterruptedException e) { | |
250 | } | |
5419a136 AM |
251 | assertTrue("isCompleted", thread2.isCompleted()); |
252 | assertTrue("isRunning", thread1.isRunning()); | |
2f185a65 FC |
253 | |
254 | // Start higher priority thread3 | |
5419a136 AM |
255 | fExecutor.execute(thread3); |
256 | try { | |
257 | Thread.sleep(500); | |
258 | } catch (InterruptedException e) { | |
2f185a65 | 259 | } |
5419a136 AM |
260 | assertFalse("isRunning", thread1.isRunning()); |
261 | assertTrue("isRunning", thread3.isRunning()); | |
2f185a65 FC |
262 | |
263 | // Wait for end of thread3 | |
264 | try { | |
5419a136 AM |
265 | synchronized (monitor) { |
266 | monitor.wait(); | |
267 | Thread.sleep(500); | |
2f185a65 FC |
268 | } |
269 | } catch (InterruptedException e) { | |
270 | } | |
5419a136 AM |
271 | assertTrue("isCompleted", thread3.isCompleted()); |
272 | assertTrue("isRunning", thread1.isRunning()); | |
2f185a65 FC |
273 | |
274 | // Wait for thread1 completion | |
275 | try { | |
5419a136 AM |
276 | synchronized (monitor) { |
277 | monitor.wait(); | |
2f185a65 FC |
278 | } |
279 | } catch (InterruptedException e) { | |
280 | } | |
5419a136 | 281 | assertTrue("isCompleted", thread1.isCompleted()); |
2f185a65 | 282 | } |
ff4ed569 | 283 | |
6e1886bc AM |
284 | // ------------------------------------------------------------------------ |
285 | // toString | |
286 | // ------------------------------------------------------------------------ | |
ff4ed569 | 287 | |
6e1886bc AM |
288 | /** |
289 | * Test method for | |
290 | * {@link org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor#toString()} | |
291 | */ | |
292 | @Test | |
293 | public void testToString() { | |
96b353c5 FC |
294 | TmfRequestExecutor executor = new TmfRequestExecutor(); |
295 | String expected = "[TmfRequestExecutor(ThreadPoolExecutor)]"; | |
296 | assertEquals("toString", expected, executor.toString()); | |
6e1886bc | 297 | } |
d18dd09b ASL |
298 | |
299 | } |