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