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