1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Simon Delisle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.temp
.request
;
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
;
21 import java
.util
.ArrayList
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
24 import java
.util
.concurrent
.TimeUnit
;
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
;
47 * Test suite for the scheduler.
49 public class TmfSchedulerTest
{
51 /** Time-out tests after 60 seconds */
53 public TestRule globalTimeout
= new Timeout(1, TimeUnit
.MINUTES
);
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
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;
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 private CtfTmfTrace fixture
;
69 private long fStartTime
;
70 private long fEndTime
;
71 private TmfTimeRange fForegroundTimeRange
;
73 private final List
<String
> fOrderList
= new ArrayList
<>();
74 private int fForegroundId
= 0;
75 private int fBackgroundId
= 0;
78 * Perform pre-test initialization.
80 * @throws TmfTraceException
81 * If the test trace is not found
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();
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
));
99 * Perform post-test clean-up.
102 public void tearDown() {
103 if (fixture
!= null) {
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 * Test one background request
116 public void backgroundRequest() {
117 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
118 fixture
.sendRequest(background
);
120 background
.waitForCompletion();
121 } catch (InterruptedException e
) {
124 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
128 * Test one foreground request
131 public void foregroundRequest() {
132 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
133 fixture
.sendRequest(foreground
);
135 foreground
.waitForCompletion();
136 } catch (InterruptedException e
) {
139 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
143 * Test one foreground and one background request for the entire trace at
147 public void TestMultiRequest1() {
148 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
149 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
151 fixture
.sendRequest(background
);
152 fixture
.sendRequest(foreground
);
154 background
.waitForCompletion();
155 foreground
.waitForCompletion();
156 } catch (InterruptedException e
) {
160 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
161 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
165 * Test one background request for the entire trace and one foreground
166 * request for smaller time range
169 public void TestMultiRequest2() {
170 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
171 ForegroundRequest foreground2
= new ForegroundRequest(fForegroundTimeRange
);
173 fixture
.sendRequest(background2
);
174 fixture
.sendRequest(foreground2
);
176 background2
.waitForCompletion();
177 foreground2
.waitForCompletion();
178 } catch (InterruptedException e
) {
182 assertEquals(NB_EVENTS_TRACE
, background2
.getNbEvents());
183 assertEquals(NB_EVENTS_TIME_RANGE
, foreground2
.getNbEvents());
187 * Test two foreground request, one to select a time range and one to select
188 * an event in this time range
191 public void TestMultiRequest3() {
192 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
193 fixture
.sendRequest(foreground3
);
195 TmfSelectionRangeUpdatedSignal signal3
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fForegroundTimeRange
.getStartTime()));
196 fixture
.broadcast(signal3
);
199 foreground3
.waitForCompletion();
200 } catch (InterruptedException e
) {
204 assertEquals(NB_EVENTS_TRACE
, foreground3
.getNbEvents());
208 * Test two foreground request, one to select a time range and one to select
209 * an event before this time range
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
);
219 foreground4
.waitForCompletion();
220 } catch (InterruptedException e
) {
224 assertEquals(NB_EVENTS_TIME_RANGE
, foreground4
.getNbEvents());
228 * Test two foreground request, one to select a time range and one to select
229 * an event after this time range
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
);
239 foreground5
.waitForCompletion();
240 } catch (InterruptedException e
) {
244 assertEquals(NB_EVENTS_TIME_RANGE
, foreground5
.getNbEvents());
248 * Test one background and one foreground request for the entire trace and
249 * one foreground request to select an event
252 public void TestMultiRequest6() {
253 BackgroundRequest background6
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
254 ForegroundRequest foreground6
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
256 fixture
.sendRequest(background6
);
257 fixture
.sendRequest(foreground6
);
259 TmfSelectionRangeUpdatedSignal signal6
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
260 fixture
.broadcast(signal6
);
263 background6
.waitForCompletion();
264 foreground6
.waitForCompletion();
265 } catch (InterruptedException e
) {
269 assertEquals(NB_EVENTS_TRACE
, background6
.getNbEvents());
270 assertEquals(NB_EVENTS_TRACE
, foreground6
.getNbEvents());
274 * Four request, two foreground and two background
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
);
287 foreground7
.waitForCompletion();
288 foreground8
.waitForCompletion();
289 background7
.waitForCompletion();
290 background8
.waitForCompletion();
291 } catch (InterruptedException e
) {
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());
301 * One long foreground request and one short foreground request, the short
302 * one should finish first
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
);
312 foreground9
.waitForStart();
313 } catch (InterruptedException e
) {
316 fixture
.sendRequest(shortForeground
);
318 shortForeground
.waitForCompletion();
319 } catch (InterruptedException e
) {
322 assertFalse(foreground9
.isCompleted());
326 * One long background request and one short foreground request, the
327 * foreground request should finish first
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
);
336 foreground10
.waitForCompletion();
337 } catch (InterruptedException e
) {
340 assertTrue(foreground10
.isCompleted());
341 assertFalse(background9
.isCompleted());
345 * Test if the scheduler is working as expected
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");
366 BackgroundRequest background1
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
367 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
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
);
374 fixture
.sendRequest(foreground1
);
375 fixture
.sendRequest(foreground2
);
376 fixture
.sendRequest(foreground3
);
377 fixture
.sendRequest(foreground4
);
378 fixture
.sendRequest(background1
);
379 fixture
.sendRequest(background2
);
381 foreground1
.waitForCompletion();
382 foreground2
.waitForCompletion();
383 foreground3
.waitForCompletion();
384 foreground4
.waitForCompletion();
385 background1
.waitForCompletion();
386 background2
.waitForCompletion();
387 } catch (InterruptedException e
) {
390 assertEquals(expectedOrder
, fOrderList
.subList(0, expectedOrder
.size()));
393 // ------------------------------------------------------------------------
395 // ------------------------------------------------------------------------
397 private class BackgroundRequest
extends TmfEventRequest
{
398 private int nbEvents
= 0;
399 private String backgroundName
;
401 BackgroundRequest(TmfTimeRange timeRange
) {
402 super(fixture
.getEventType(),
405 ITmfEventRequest
.ALL_DATA
,
406 ExecutionType
.BACKGROUND
);
407 backgroundName
= getExecType().toString() + ++fBackgroundId
;
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
);
421 public int getNbEvents() {
426 private class ForegroundRequest
extends TmfEventRequest
{
427 private int nbEvents
= 0;
428 private String foregroundName
;
430 ForegroundRequest(TmfTimeRange timeRange
) {
431 super(fixture
.getEventType(),
434 ITmfEventRequest
.ALL_DATA
,
435 ExecutionType
.FOREGROUND
);
436 foregroundName
= getExecType().toString() + ++fForegroundId
;
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
);
450 public int getNbEvents() {