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
.CtfTmfTestTrace
;
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 CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
49 private static final int NB_EVENTS_TRACE
= 695319;
50 private static final int NB_EVENTS_TIME_RANGE
= 155133;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 private CtfTmfTrace fixture
;
58 private long fStartTime
;
59 private long fEndTime
;
60 private TmfTimeRange fForegroundTimeRange
;
62 private final List
<String
> fOrderList
= Collections
.synchronizedList(new ArrayList
<String
>());
63 private int fForegroundId
= 0;
64 private int fBackgroundId
= 0;
67 * Perform pre-test initialization.
69 * @throws TmfTraceException
70 * If the test trace is not found
73 public void setUp() throws TmfTraceException
{
74 assumeTrue(testTrace
.exists());
75 fixture
= new CtfTmfTrace();
76 fixture
.initTrace((IResource
) null, testTrace
.getPath(), CtfTmfEvent
.class);
77 fixture
.indexTrace(true);
78 fStartTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
79 fEndTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
81 long foregroundStartTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 4);
82 long foregroundEndTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 2);
83 fForegroundTimeRange
= new TmfTimeRange(new TmfTimestamp(foregroundStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0), new TmfTimestamp(foregroundEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0));
87 * Perform post-test clean-up.
90 public void tearDown() {
91 if (fixture
!= null) {
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * Test one background request
104 public void backgroundRequest() {
105 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
106 fixture
.sendRequest(background
);
108 background
.waitForCompletion();
109 } catch (InterruptedException e
) {
112 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
116 * Test one foreground request
119 public void foregroundRequest() {
120 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
121 fixture
.sendRequest(foreground
);
123 foreground
.waitForCompletion();
124 } catch (InterruptedException e
) {
127 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
131 * Test one foreground and one background request for the entire trace at
135 public void TestMultiRequest1() {
136 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
137 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
139 fixture
.sendRequest(background
);
140 fixture
.sendRequest(foreground
);
142 background
.waitForCompletion();
143 foreground
.waitForCompletion();
144 } catch (InterruptedException e
) {
148 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
149 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
153 * Test one background request for the entire trace and one foreground
154 * request for smaller time range
157 public void TestMultiRequest2() {
158 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
159 ForegroundRequest foreground2
= new ForegroundRequest(fForegroundTimeRange
);
161 fixture
.sendRequest(background2
);
162 fixture
.sendRequest(foreground2
);
164 background2
.waitForCompletion();
165 foreground2
.waitForCompletion();
166 } catch (InterruptedException e
) {
170 assertEquals(NB_EVENTS_TRACE
, background2
.getNbEvents());
171 assertEquals(NB_EVENTS_TIME_RANGE
, foreground2
.getNbEvents());
175 * Test two foreground request, one to select a time range and one to select
176 * an event in this time range
179 public void TestMultiRequest3() {
180 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
181 fixture
.sendRequest(foreground3
);
183 TmfTimeSynchSignal signal3
= new TmfTimeSynchSignal(this, new TmfTimestamp(fForegroundTimeRange
.getStartTime()));
184 fixture
.broadcast(signal3
);
187 foreground3
.waitForCompletion();
188 } catch (InterruptedException e
) {
192 assertEquals(NB_EVENTS_TRACE
, foreground3
.getNbEvents());
196 * Test two foreground request, one to select a time range and one to select
197 * an event before this time range
200 public void TestMultiRequest4() {
201 ForegroundRequest foreground4
= new ForegroundRequest(fForegroundTimeRange
);
202 fixture
.sendRequest(foreground4
);
203 TmfTimeSynchSignal signal4
= new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
204 fixture
.broadcast(signal4
);
207 foreground4
.waitForCompletion();
208 } catch (InterruptedException e
) {
212 assertEquals(NB_EVENTS_TIME_RANGE
, foreground4
.getNbEvents());
216 * Test two foreground request, one to select a time range and one to select
217 * an event after this time range
220 public void TestMultiRequest5() {
221 ForegroundRequest foreground5
= new ForegroundRequest(fForegroundTimeRange
);
222 fixture
.sendRequest(foreground5
);
223 TmfTimeSynchSignal signal5
= new TmfTimeSynchSignal(this, new TmfTimestamp(fEndTime
- ((fEndTime
- fStartTime
) / 4)));
224 fixture
.broadcast(signal5
);
227 foreground5
.waitForCompletion();
228 } catch (InterruptedException e
) {
232 assertEquals(NB_EVENTS_TIME_RANGE
, foreground5
.getNbEvents());
236 * Test one background and one foreground request for the entire trace and
237 * one foreground request to select an event
240 public void TestMultiRequest6() {
241 BackgroundRequest background6
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
242 ForegroundRequest foreground6
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
244 fixture
.sendRequest(background6
);
245 fixture
.sendRequest(foreground6
);
247 TmfTimeSynchSignal signal6
= new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
248 fixture
.broadcast(signal6
);
251 background6
.waitForCompletion();
252 foreground6
.waitForCompletion();
253 } catch (InterruptedException e
) {
257 assertEquals(NB_EVENTS_TRACE
, background6
.getNbEvents());
258 assertEquals(NB_EVENTS_TRACE
, foreground6
.getNbEvents());
262 * Four request, two foreground and two background
265 public void TestMultiRequest7() {
266 ForegroundRequest foreground7
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
267 ForegroundRequest foreground8
= new ForegroundRequest(fForegroundTimeRange
);
268 BackgroundRequest background7
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
269 BackgroundRequest background8
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
270 fixture
.sendRequest(foreground7
);
271 fixture
.sendRequest(foreground8
);
272 fixture
.sendRequest(background7
);
273 fixture
.sendRequest(background8
);
275 foreground7
.waitForCompletion();
276 foreground8
.waitForCompletion();
277 background7
.waitForCompletion();
278 background8
.waitForCompletion();
279 } catch (InterruptedException e
) {
282 assertEquals(NB_EVENTS_TRACE
, foreground7
.getNbEvents());
283 assertEquals(NB_EVENTS_TIME_RANGE
, foreground8
.getNbEvents());
284 assertEquals(NB_EVENTS_TRACE
, background7
.getNbEvents());
285 assertEquals(NB_EVENTS_TRACE
, background8
.getNbEvents());
289 * One long foreground request and one short foreground request, the short
290 * one should finish first
293 public void preemptedForegroundRequest() {
294 ForegroundRequest foreground9
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
295 TmfTimeRange shortTimeRange
= new TmfTimeRange(new TmfTimestamp(fStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
, 0), new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 16), ITmfTimestamp
.NANOSECOND_SCALE
, 0));
296 ForegroundRequest shortForeground
= new ForegroundRequest(shortTimeRange
);
297 fixture
.sendRequest(foreground9
);
298 fixture
.sendRequest(shortForeground
);
300 shortForeground
.waitForCompletion();
301 } catch (InterruptedException e
) {
304 assertFalse(foreground9
.isCompleted());
308 * One long background request and one short foreground request, the
309 * foreground request should finish first
312 public void preemptedBackgroundRequest() {
313 BackgroundRequest background9
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
314 ForegroundRequest foreground10
= new ForegroundRequest(fForegroundTimeRange
);
315 fixture
.sendRequest(background9
);
316 fixture
.sendRequest(foreground10
);
318 foreground10
.waitForCompletion();
319 } catch (InterruptedException e
) {
322 assertTrue(foreground10
.isCompleted());
323 assertFalse(background9
.isCompleted());
327 * Test if the scheduler is working as expected
330 public void executionOrder() {
331 List
<String
> expectedOrder
= new LinkedList
<String
>();
332 expectedOrder
.add("FOREGROUND1");
333 expectedOrder
.add("FOREGROUND2");
334 expectedOrder
.add("FOREGROUND3");
335 expectedOrder
.add("FOREGROUND4");
336 expectedOrder
.add("BACKGROUND1");
337 expectedOrder
.add("FOREGROUND1");
338 expectedOrder
.add("FOREGROUND2");
339 expectedOrder
.add("FOREGROUND3");
340 expectedOrder
.add("FOREGROUND4");
341 expectedOrder
.add("BACKGROUND2");
347 BackgroundRequest background1
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
348 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
350 ForegroundRequest foreground1
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
351 ForegroundRequest foreground2
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
352 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
353 ForegroundRequest foreground4
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
355 fixture
.sendRequest(foreground1
);
356 fixture
.sendRequest(foreground2
);
357 fixture
.sendRequest(foreground3
);
358 fixture
.sendRequest(foreground4
);
359 fixture
.sendRequest(background1
);
360 fixture
.sendRequest(background2
);
362 foreground1
.waitForCompletion();
363 foreground2
.waitForCompletion();
364 foreground3
.waitForCompletion();
365 foreground4
.waitForCompletion();
366 background1
.waitForCompletion();
367 background2
.waitForCompletion();
368 } catch (InterruptedException e
) {
371 assertEquals(expectedOrder
, fOrderList
.subList(0, expectedOrder
.size()));
374 // ------------------------------------------------------------------------
376 // ------------------------------------------------------------------------
378 private class BackgroundRequest
extends TmfEventRequest
{
379 private int nbEvents
= 0;
380 private String backgroundName
;
382 BackgroundRequest(TmfTimeRange timeRange
) {
383 super(fixture
.getEventType(),
386 TmfDataRequest
.ALL_DATA
,
387 ExecutionType
.BACKGROUND
);
388 backgroundName
= getExecType().toString() + ++fBackgroundId
;
392 public void handleData(final ITmfEvent event
) {
393 super.handleData(event
);
394 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(backgroundName
)) {
395 fOrderList
.add(backgroundName
);
400 public int getNbEvents() {
405 private class ForegroundRequest
extends TmfEventRequest
{
406 private int nbEvents
= 0;
407 private String foregroundName
;
409 ForegroundRequest(TmfTimeRange timeRange
) {
410 super(fixture
.getEventType(),
413 TmfDataRequest
.ALL_DATA
,
414 ExecutionType
.FOREGROUND
);
415 foregroundName
= getExecType().toString() + ++fForegroundId
;
419 public void handleData(final ITmfEvent event
) {
420 super.handleData(event
);
421 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(foregroundName
)) {
422 fOrderList
.add(foregroundName
);
427 public int getNbEvents() {