1 /*******************************************************************************
2 * Copyright (c) 2013 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
.linuxtools
.tmf
.core
.tests
.request
;
15 import static org
.junit
.Assert
.*;
16 import static org
.junit
.Assume
.assumeTrue
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collections
;
20 import java
.util
.LinkedList
;
21 import java
.util
.List
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTrace
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTraces
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
35 import org
.junit
.After
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * Test suite for the scheduler.
42 public class TmfSchedulerTest
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private static final int TRACE_INDEX
= 0;
49 private static final String PATH
= CtfTmfTestTraces
.getTestTracePath(TRACE_INDEX
);
50 private static final int NB_EVENTS_TRACE
= 695319;
51 private static final int NB_EVENTS_TIME_RANGE
= 155133;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 private CtfTmfTrace fixture
;
59 private long fStartTime
;
60 private long fEndTime
;
61 private TmfTimeRange fForegroundTimeRange
;
63 private final List
<String
> fOrderList
= Collections
.synchronizedList(new ArrayList
<String
>());
64 private int fForegroundId
= 0;
65 private int fBackgroundId
= 0;
68 * Perform pre-test initialization.
70 * @throws TmfTraceException
71 * If the test trace is not found
74 public void setUp() throws TmfTraceException
{
75 assumeTrue(CtfTmfTestTraces
.tracesExist());
76 fixture
= new CtfTmfTrace();
77 fixture
.initTrace((IResource
) null, PATH
, CtfTmfEvent
.class);
78 fixture
.indexTrace(true);
79 fStartTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
80 fEndTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
82 long foregroundStartTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 4);
83 long foregroundEndTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 2);
84 fForegroundTimeRange
= new TmfTimeRange(new TmfTimestamp(foregroundStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0), new TmfTimestamp(foregroundEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0));
88 * Perform post-test clean-up.
91 public void tearDown() {
92 if (fixture
!= null) {
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 * Test one background request
105 public void backgroundRequest() {
106 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
107 fixture
.sendRequest(background
);
109 background
.waitForCompletion();
110 } catch (InterruptedException e
) {
113 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
117 * Test one foreground request
120 public void foregroundRequest() {
121 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
122 fixture
.sendRequest(foreground
);
124 foreground
.waitForCompletion();
125 } catch (InterruptedException e
) {
128 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
132 * Test one foreground and one background request for the entire trace at
136 public void TestMultiRequest1() {
137 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
138 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
140 fixture
.sendRequest(background
);
141 fixture
.sendRequest(foreground
);
143 background
.waitForCompletion();
144 foreground
.waitForCompletion();
145 } catch (InterruptedException e
) {
149 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
150 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
154 * Test one background request for the entire trace and one foreground
155 * request for smaller time range
158 public void TestMultiRequest2() {
159 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
160 ForegroundRequest foreground2
= new ForegroundRequest(fForegroundTimeRange
);
162 fixture
.sendRequest(background2
);
163 fixture
.sendRequest(foreground2
);
165 background2
.waitForCompletion();
166 foreground2
.waitForCompletion();
167 } catch (InterruptedException e
) {
171 assertEquals(NB_EVENTS_TRACE
, background2
.getNbEvents());
172 assertEquals(NB_EVENTS_TIME_RANGE
, foreground2
.getNbEvents());
176 * Test two foreground request, one to select a time range and one to select
177 * an event in this time range
180 public void TestMultiRequest3() {
181 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
182 fixture
.sendRequest(foreground3
);
184 TmfTimeSynchSignal signal3
= new TmfTimeSynchSignal(this, new TmfTimestamp(fForegroundTimeRange
.getStartTime()));
185 fixture
.broadcast(signal3
);
188 foreground3
.waitForCompletion();
189 } catch (InterruptedException e
) {
193 assertEquals(NB_EVENTS_TRACE
, foreground3
.getNbEvents());
197 * Test two foreground request, one to select a time range and one to select
198 * an event before this time range
201 public void TestMultiRequest4() {
202 ForegroundRequest foreground4
= new ForegroundRequest(fForegroundTimeRange
);
203 fixture
.sendRequest(foreground4
);
204 TmfTimeSynchSignal signal4
= new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
205 fixture
.broadcast(signal4
);
208 foreground4
.waitForCompletion();
209 } catch (InterruptedException e
) {
213 assertEquals(NB_EVENTS_TIME_RANGE
, foreground4
.getNbEvents());
217 * Test two foreground request, one to select a time range and one to select
218 * an event after this time range
221 public void TestMultiRequest5() {
222 ForegroundRequest foreground5
= new ForegroundRequest(fForegroundTimeRange
);
223 fixture
.sendRequest(foreground5
);
224 TmfTimeSynchSignal signal5
= new TmfTimeSynchSignal(this, new TmfTimestamp(fEndTime
- ((fEndTime
- fStartTime
) / 4)));
225 fixture
.broadcast(signal5
);
228 foreground5
.waitForCompletion();
229 } catch (InterruptedException e
) {
233 assertEquals(NB_EVENTS_TIME_RANGE
, foreground5
.getNbEvents());
237 * Test one background and one foreground request for the entire trace and
238 * one foreground request to select an event
241 public void TestMultiRequest6() {
242 BackgroundRequest background6
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
243 ForegroundRequest foreground6
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
245 fixture
.sendRequest(background6
);
246 fixture
.sendRequest(foreground6
);
248 TmfTimeSynchSignal signal6
= new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
249 fixture
.broadcast(signal6
);
252 background6
.waitForCompletion();
253 foreground6
.waitForCompletion();
254 } catch (InterruptedException e
) {
258 assertEquals(NB_EVENTS_TRACE
, background6
.getNbEvents());
259 assertEquals(NB_EVENTS_TRACE
, foreground6
.getNbEvents());
263 * Four request, two foreground and two background
266 public void TestMultiRequest7() {
267 ForegroundRequest foreground7
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
268 ForegroundRequest foreground8
= new ForegroundRequest(fForegroundTimeRange
);
269 BackgroundRequest background7
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
270 BackgroundRequest background8
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
271 fixture
.sendRequest(foreground7
);
272 fixture
.sendRequest(foreground8
);
273 fixture
.sendRequest(background7
);
274 fixture
.sendRequest(background8
);
276 foreground7
.waitForCompletion();
277 foreground8
.waitForCompletion();
278 background7
.waitForCompletion();
279 background8
.waitForCompletion();
280 } catch (InterruptedException e
) {
283 assertEquals(NB_EVENTS_TRACE
, foreground7
.getNbEvents());
284 assertEquals(NB_EVENTS_TIME_RANGE
, foreground8
.getNbEvents());
285 assertEquals(NB_EVENTS_TRACE
, background7
.getNbEvents());
286 assertEquals(NB_EVENTS_TRACE
, background8
.getNbEvents());
290 * One long foreground request and one short foreground request, the short
291 * one should finish first
294 public void preemptedForegroundRequest() {
295 ForegroundRequest foreground9
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
296 TmfTimeRange shortTimeRange
= new TmfTimeRange(new TmfTimestamp(fStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0), new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 16), ITmfTimestamp
.NANOSECOND_SCALE
, 0));
297 ForegroundRequest shortForeground
= new ForegroundRequest(shortTimeRange
);
298 fixture
.sendRequest(foreground9
);
299 fixture
.sendRequest(shortForeground
);
301 shortForeground
.waitForCompletion();
302 } catch (InterruptedException e
) {
305 assertFalse(foreground9
.isCompleted());
309 * One long background request and one short foreground request, the
310 * foreground request should finish first
313 public void preemptedBackgroundRequest() {
314 BackgroundRequest background9
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
315 ForegroundRequest foreground10
= new ForegroundRequest(fForegroundTimeRange
);
316 fixture
.sendRequest(background9
);
317 fixture
.sendRequest(foreground10
);
319 foreground10
.waitForCompletion();
320 } catch (InterruptedException e
) {
323 assertTrue(foreground10
.isCompleted());
324 assertFalse(background9
.isCompleted());
328 * Test if the scheduler is working as expected
331 public void executionOrder() {
332 List
<String
> expectedOrder
= new LinkedList
<String
>();
333 expectedOrder
.add("FOREGROUND1");
334 expectedOrder
.add("FOREGROUND2");
335 expectedOrder
.add("FOREGROUND3");
336 expectedOrder
.add("FOREGROUND4");
337 expectedOrder
.add("BACKGROUND1");
338 expectedOrder
.add("FOREGROUND1");
339 expectedOrder
.add("FOREGROUND2");
340 expectedOrder
.add("FOREGROUND3");
341 expectedOrder
.add("FOREGROUND4");
342 expectedOrder
.add("BACKGROUND2");
348 BackgroundRequest background1
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
349 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
351 ForegroundRequest foreground1
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
352 ForegroundRequest foreground2
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
353 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
354 ForegroundRequest foreground4
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
356 fixture
.sendRequest(foreground1
);
357 fixture
.sendRequest(foreground2
);
358 fixture
.sendRequest(foreground3
);
359 fixture
.sendRequest(foreground4
);
360 fixture
.sendRequest(background1
);
361 fixture
.sendRequest(background2
);
363 foreground1
.waitForCompletion();
364 foreground2
.waitForCompletion();
365 foreground3
.waitForCompletion();
366 foreground4
.waitForCompletion();
367 background1
.waitForCompletion();
368 background2
.waitForCompletion();
369 } catch (InterruptedException e
) {
372 assertEquals(expectedOrder
, fOrderList
.subList(0, expectedOrder
.size()));
375 // ------------------------------------------------------------------------
377 // ------------------------------------------------------------------------
379 private class BackgroundRequest
extends TmfEventRequest
{
380 private int nbEvents
= 0;
381 private String backgroundName
;
383 BackgroundRequest(TmfTimeRange timeRange
) {
384 super(fixture
.getEventType(),
387 TmfDataRequest
.ALL_DATA
,
388 ExecutionType
.BACKGROUND
);
389 backgroundName
= getExecType().toString() + ++fBackgroundId
;
393 public void handleData(final ITmfEvent event
) {
394 super.handleData(event
);
395 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(backgroundName
)) {
396 fOrderList
.add(backgroundName
);
401 public int getNbEvents() {
406 private class ForegroundRequest
extends TmfEventRequest
{
407 private int nbEvents
= 0;
408 private String foregroundName
;
410 ForegroundRequest(TmfTimeRange timeRange
) {
411 super(fixture
.getEventType(),
414 TmfDataRequest
.ALL_DATA
,
415 ExecutionType
.FOREGROUND
);
416 foregroundName
= getExecType().toString() + ++fForegroundId
;
420 public void handleData(final ITmfEvent event
) {
421 super.handleData(event
);
422 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(foregroundName
)) {
423 fOrderList
.add(foregroundName
);
428 public int getNbEvents() {