tmf: Switch tmf.core.tests to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / request / TmfSchedulerTest.java
CommitLineData
306586b1
SD
1/*******************************************************************************
2 * Copyright (c) 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 * Simon Delisle - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.tests.request;
14
ab346a2b
BH
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertTrue;
18import static org.junit.Assert.fail;
306586b1
SD
19import static org.junit.Assume.assumeTrue;
20
21import java.util.ArrayList;
306586b1
SD
22import java.util.LinkedList;
23import java.util.List;
24
25import org.eclipse.core.resources.IResource;
26import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
27import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
28import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
2740e05c 30import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
306586b1
SD
31import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
32import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
9ac63b5b 33import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace;
306586b1
SD
34import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
35import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
36import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
37import org.junit.After;
38import org.junit.Before;
b1b999ea 39import org.junit.Ignore;
50d6b86f 40import org.junit.Rule;
306586b1 41import org.junit.Test;
50d6b86f
AM
42import org.junit.rules.TestRule;
43import org.junit.rules.Timeout;
306586b1
SD
44
45/**
46 * Test suite for the scheduler.
47 */
48public class TmfSchedulerTest {
49
50d6b86f
AM
50 /** Time-out tests after 60 seconds */
51 @Rule
52 public TestRule globalTimeout= new Timeout(60000);
53
306586b1
SD
54 // ------------------------------------------------------------------------
55 // Constants
56 // ------------------------------------------------------------------------
57
9ac63b5b 58 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
306586b1
SD
59 private static final int NB_EVENTS_TRACE = 695319;
60 private static final int NB_EVENTS_TIME_RANGE = 155133;
61
62 // ------------------------------------------------------------------------
63 // Attributes
64 // ------------------------------------------------------------------------
65
66 private CtfTmfTrace fixture;
67
68 private long fStartTime;
69 private long fEndTime;
70 private TmfTimeRange fForegroundTimeRange;
71
ccf2bbb4 72 private final List<String> fOrderList = new ArrayList<>();
306586b1
SD
73 private int fForegroundId = 0;
74 private int fBackgroundId = 0;
75
76 /**
77 * Perform pre-test initialization.
78 *
79 * @throws TmfTraceException
80 * If the test trace is not found
81 */
82 @Before
83 public void setUp() throws TmfTraceException {
9ac63b5b 84 assumeTrue(testTrace.exists());
306586b1 85 fixture = new CtfTmfTrace();
9ac63b5b 86 fixture.initTrace((IResource) null, testTrace.getPath(), CtfTmfEvent.class);
306586b1
SD
87 fixture.indexTrace(true);
88 fStartTime = fixture.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
89 fEndTime = fixture.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
90
91 long foregroundStartTime = fStartTime + ((fEndTime - fStartTime) / 4);
92 long foregroundEndTime = fStartTime + ((fEndTime - fStartTime) / 2);
93 fForegroundTimeRange = new TmfTimeRange(new TmfTimestamp(foregroundStartTime, ITmfTimestamp.NANOSECOND_SCALE, 0), new TmfTimestamp(foregroundEndTime, ITmfTimestamp.NANOSECOND_SCALE, 0));
94 }
95
96 /**
97 * Perform post-test clean-up.
98 */
99 @After
100 public void tearDown() {
101 if (fixture != null) {
102 fixture.dispose();
103 }
104 }
105
106 // ------------------------------------------------------------------------
107 // Tests cases
108 // ------------------------------------------------------------------------
109
110 /**
111 * Test one background request
112 */
113 @Test
114 public void backgroundRequest() {
115 BackgroundRequest background = new BackgroundRequest(TmfTimeRange.ETERNITY);
116 fixture.sendRequest(background);
117 try {
118 background.waitForCompletion();
119 } catch (InterruptedException e) {
120 fail();
121 }
122 assertEquals(NB_EVENTS_TRACE, background.getNbEvents());
123 }
124
125 /**
126 * Test one foreground request
127 */
128 @Test
129 public void foregroundRequest() {
130 ForegroundRequest foreground = new ForegroundRequest(TmfTimeRange.ETERNITY);
131 fixture.sendRequest(foreground);
132 try {
133 foreground.waitForCompletion();
134 } catch (InterruptedException e) {
135 fail();
136 }
137 assertEquals(NB_EVENTS_TRACE, foreground.getNbEvents());
138 }
139
140 /**
141 * Test one foreground and one background request for the entire trace at
142 * the same time
143 */
144 @Test
145 public void TestMultiRequest1() {
146 BackgroundRequest background = new BackgroundRequest(TmfTimeRange.ETERNITY);
147 ForegroundRequest foreground = new ForegroundRequest(TmfTimeRange.ETERNITY);
148
149 fixture.sendRequest(background);
150 fixture.sendRequest(foreground);
151 try {
152 background.waitForCompletion();
153 foreground.waitForCompletion();
154 } catch (InterruptedException e) {
155 fail();
156 }
157
158 assertEquals(NB_EVENTS_TRACE, background.getNbEvents());
159 assertEquals(NB_EVENTS_TRACE, foreground.getNbEvents());
160 }
161
162 /**
163 * Test one background request for the entire trace and one foreground
164 * request for smaller time range
165 */
166 @Test
167 public void TestMultiRequest2() {
168 BackgroundRequest background2 = new BackgroundRequest(TmfTimeRange.ETERNITY);
169 ForegroundRequest foreground2 = new ForegroundRequest(fForegroundTimeRange);
170
171 fixture.sendRequest(background2);
172 fixture.sendRequest(foreground2);
173 try {
174 background2.waitForCompletion();
175 foreground2.waitForCompletion();
176 } catch (InterruptedException e) {
177 fail();
178 }
179
180 assertEquals(NB_EVENTS_TRACE, background2.getNbEvents());
181 assertEquals(NB_EVENTS_TIME_RANGE, foreground2.getNbEvents());
182 }
183
184 /**
185 * Test two foreground request, one to select a time range and one to select
186 * an event in this time range
187 */
188 @Test
189 public void TestMultiRequest3() {
190 ForegroundRequest foreground3 = new ForegroundRequest(TmfTimeRange.ETERNITY);
191 fixture.sendRequest(foreground3);
192
193 TmfTimeSynchSignal signal3 = new TmfTimeSynchSignal(this, new TmfTimestamp(fForegroundTimeRange.getStartTime()));
194 fixture.broadcast(signal3);
195
196 try {
197 foreground3.waitForCompletion();
198 } catch (InterruptedException e) {
199 fail();
200 }
201
202 assertEquals(NB_EVENTS_TRACE, foreground3.getNbEvents());
203 }
204
205 /**
206 * Test two foreground request, one to select a time range and one to select
207 * an event before this time range
208 */
209 @Test
210 public void TestMultiRequest4() {
211 ForegroundRequest foreground4 = new ForegroundRequest(fForegroundTimeRange);
212 fixture.sendRequest(foreground4);
213 TmfTimeSynchSignal signal4 = new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 8)));
214 fixture.broadcast(signal4);
215
216 try {
217 foreground4.waitForCompletion();
218 } catch (InterruptedException e) {
219 fail();
220 }
221
222 assertEquals(NB_EVENTS_TIME_RANGE, foreground4.getNbEvents());
223 }
224
225 /**
226 * Test two foreground request, one to select a time range and one to select
227 * an event after this time range
228 */
229 @Test
230 public void TestMultiRequest5() {
231 ForegroundRequest foreground5 = new ForegroundRequest(fForegroundTimeRange);
232 fixture.sendRequest(foreground5);
233 TmfTimeSynchSignal signal5 = new TmfTimeSynchSignal(this, new TmfTimestamp(fEndTime - ((fEndTime - fStartTime) / 4)));
234 fixture.broadcast(signal5);
235
236 try {
237 foreground5.waitForCompletion();
238 } catch (InterruptedException e) {
239 fail();
240 }
241
242 assertEquals(NB_EVENTS_TIME_RANGE, foreground5.getNbEvents());
243 }
244
245 /**
246 * Test one background and one foreground request for the entire trace and
247 * one foreground request to select an event
248 */
249 @Test
250 public void TestMultiRequest6() {
251 BackgroundRequest background6 = new BackgroundRequest(TmfTimeRange.ETERNITY);
252 ForegroundRequest foreground6 = new ForegroundRequest(TmfTimeRange.ETERNITY);
253
254 fixture.sendRequest(background6);
255 fixture.sendRequest(foreground6);
256
257 TmfTimeSynchSignal signal6 = new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 8)));
258 fixture.broadcast(signal6);
259
260 try {
261 background6.waitForCompletion();
262 foreground6.waitForCompletion();
263 } catch (InterruptedException e) {
264 fail();
265 }
266
267 assertEquals(NB_EVENTS_TRACE, background6.getNbEvents());
268 assertEquals(NB_EVENTS_TRACE, foreground6.getNbEvents());
269 }
270
271 /**
272 * Four request, two foreground and two background
273 */
274 @Test
275 public void TestMultiRequest7() {
276 ForegroundRequest foreground7 = new ForegroundRequest(TmfTimeRange.ETERNITY);
277 ForegroundRequest foreground8 = new ForegroundRequest(fForegroundTimeRange);
278 BackgroundRequest background7 = new BackgroundRequest(TmfTimeRange.ETERNITY);
279 BackgroundRequest background8 = new BackgroundRequest(TmfTimeRange.ETERNITY);
280 fixture.sendRequest(foreground7);
281 fixture.sendRequest(foreground8);
282 fixture.sendRequest(background7);
283 fixture.sendRequest(background8);
284 try {
285 foreground7.waitForCompletion();
286 foreground8.waitForCompletion();
287 background7.waitForCompletion();
288 background8.waitForCompletion();
289 } catch (InterruptedException e) {
290 fail();
291 }
292 assertEquals(NB_EVENTS_TRACE, foreground7.getNbEvents());
293 assertEquals(NB_EVENTS_TIME_RANGE, foreground8.getNbEvents());
294 assertEquals(NB_EVENTS_TRACE, background7.getNbEvents());
295 assertEquals(NB_EVENTS_TRACE, background8.getNbEvents());
296 }
297
298 /**
299 * One long foreground request and one short foreground request, the short
300 * one should finish first
301 */
302 @Test
303 public void preemptedForegroundRequest() {
304 ForegroundRequest foreground9 = new ForegroundRequest(TmfTimeRange.ETERNITY);
305 TmfTimeRange shortTimeRange = new TmfTimeRange(new TmfTimestamp(fStartTime, ITmfTimestamp.NANOSECOND_SCALE, 0), new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 16), ITmfTimestamp.NANOSECOND_SCALE, 0));
306 ForegroundRequest shortForeground = new ForegroundRequest(shortTimeRange);
307 fixture.sendRequest(foreground9);
308 fixture.sendRequest(shortForeground);
309 try {
310 shortForeground.waitForCompletion();
311 } catch (InterruptedException e) {
312 fail();
313 }
314 assertFalse(foreground9.isCompleted());
315 }
316
317 /**
318 * One long background request and one short foreground request, the
319 * foreground request should finish first
320 */
321 @Test
322 public void preemptedBackgroundRequest() {
323 BackgroundRequest background9 = new BackgroundRequest(TmfTimeRange.ETERNITY);
324 ForegroundRequest foreground10 = new ForegroundRequest(fForegroundTimeRange);
325 fixture.sendRequest(background9);
326 fixture.sendRequest(foreground10);
327 try {
328 foreground10.waitForCompletion();
329 } catch (InterruptedException e) {
330 fail();
331 }
332 assertTrue(foreground10.isCompleted());
333 assertFalse(background9.isCompleted());
334 }
335
336 /**
337 * Test if the scheduler is working as expected
338 */
b1b999ea 339 @Ignore
306586b1
SD
340 @Test
341 public void executionOrder() {
ccf2bbb4 342 List<String> expectedOrder = new LinkedList<>();
306586b1
SD
343 expectedOrder.add("FOREGROUND1");
344 expectedOrder.add("FOREGROUND2");
345 expectedOrder.add("FOREGROUND3");
346 expectedOrder.add("FOREGROUND4");
347 expectedOrder.add("BACKGROUND1");
348 expectedOrder.add("FOREGROUND1");
349 expectedOrder.add("FOREGROUND2");
350 expectedOrder.add("FOREGROUND3");
351 expectedOrder.add("FOREGROUND4");
352 expectedOrder.add("BACKGROUND2");
353
354 fOrderList.clear();
355 fForegroundId = 0;
356 fBackgroundId = 0;
357
358 BackgroundRequest background1 = new BackgroundRequest(TmfTimeRange.ETERNITY);
359 BackgroundRequest background2 = new BackgroundRequest(TmfTimeRange.ETERNITY);
360
361 ForegroundRequest foreground1 = new ForegroundRequest(TmfTimeRange.ETERNITY);
362 ForegroundRequest foreground2 = new ForegroundRequest(TmfTimeRange.ETERNITY);
363 ForegroundRequest foreground3 = new ForegroundRequest(TmfTimeRange.ETERNITY);
364 ForegroundRequest foreground4 = new ForegroundRequest(TmfTimeRange.ETERNITY);
365
366 fixture.sendRequest(foreground1);
367 fixture.sendRequest(foreground2);
368 fixture.sendRequest(foreground3);
369 fixture.sendRequest(foreground4);
370 fixture.sendRequest(background1);
371 fixture.sendRequest(background2);
372 try {
373 foreground1.waitForCompletion();
374 foreground2.waitForCompletion();
375 foreground3.waitForCompletion();
376 foreground4.waitForCompletion();
377 background1.waitForCompletion();
378 background2.waitForCompletion();
379 } catch (InterruptedException e) {
380 fail();
381 }
382 assertEquals(expectedOrder, fOrderList.subList(0, expectedOrder.size()));
383 }
384
385 // ------------------------------------------------------------------------
386 // Helper methods
387 // ------------------------------------------------------------------------
388
389 private class BackgroundRequest extends TmfEventRequest {
306586b1
SD
390 private int nbEvents = 0;
391 private String backgroundName;
392
393 BackgroundRequest(TmfTimeRange timeRange) {
7184fc40
AM
394 super(fixture.getEventType(),
395 timeRange,
396 0,
2740e05c 397 ITmfEventRequest.ALL_DATA,
306586b1
SD
398 ExecutionType.BACKGROUND);
399 backgroundName = getExecType().toString() + ++fBackgroundId;
400 }
401
402 @Override
403 public void handleData(final ITmfEvent event) {
404 super.handleData(event);
ab346a2b
BH
405 synchronized (fOrderList) {
406 if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(backgroundName)) {
407 fOrderList.add(backgroundName);
408 }
306586b1
SD
409 }
410 ++nbEvents;
411 }
412
413 public int getNbEvents() {
414 return nbEvents;
415 }
416 }
417
418 private class ForegroundRequest extends TmfEventRequest {
306586b1
SD
419 private int nbEvents = 0;
420 private String foregroundName;
421
422 ForegroundRequest(TmfTimeRange timeRange) {
7184fc40
AM
423 super(fixture.getEventType(),
424 timeRange,
425 0,
2740e05c 426 ITmfEventRequest.ALL_DATA,
306586b1
SD
427 ExecutionType.FOREGROUND);
428 foregroundName = getExecType().toString() + ++fForegroundId;
429 }
430
431 @Override
432 public void handleData(final ITmfEvent event) {
433 super.handleData(event);
ab346a2b
BH
434 synchronized (fOrderList) {
435 if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(foregroundName)) {
436 fOrderList.add(foregroundName);
437 }
306586b1
SD
438 }
439 ++nbEvents;
440 }
441
442 public int getNbEvents() {
443 return nbEvents;
444 }
445 }
9d63098b 446}
This page took 0.045928 seconds and 5 git commands to generate.