Commit | Line | Data |
---|---|---|
306586b1 | 1 | /******************************************************************************* |
8967c8c0 | 2 | * Copyright (c) 2013, 2014 Ericsson |
306586b1 SD |
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 | ||
91e7f946 | 13 | package org.eclipse.linuxtools.tmf.ctf.core.tests.request; |
306586b1 | 14 | |
ab346a2b BH |
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; | |
306586b1 SD |
19 | import static org.junit.Assume.assumeTrue; |
20 | ||
21 | import java.util.ArrayList; | |
306586b1 SD |
22 | import java.util.LinkedList; |
23 | import java.util.List; | |
24 | ||
25 | import org.eclipse.core.resources.IResource; | |
306586b1 SD |
26 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
27 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
2740e05c | 28 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
306586b1 SD |
29 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
30 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; | |
306586b1 SD |
31 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
32 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
33 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
91e7f946 AM |
34 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent; |
35 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace; | |
36 | import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace; | |
306586b1 SD |
37 | import org.junit.After; |
38 | import org.junit.Before; | |
b1b999ea | 39 | import org.junit.Ignore; |
50d6b86f | 40 | import org.junit.Rule; |
306586b1 | 41 | import org.junit.Test; |
50d6b86f AM |
42 | import org.junit.rules.TestRule; |
43 | import org.junit.rules.Timeout; | |
306586b1 SD |
44 | |
45 | /** | |
46 | * Test suite for the scheduler. | |
47 | */ | |
48 | public 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); | |
8967c8c0 BH |
308 | try { |
309 | foreground9.waitForStart(); | |
310 | } catch (InterruptedException e) { | |
311 | fail(); | |
312 | } | |
306586b1 SD |
313 | fixture.sendRequest(shortForeground); |
314 | try { | |
315 | shortForeground.waitForCompletion(); | |
316 | } catch (InterruptedException e) { | |
317 | fail(); | |
318 | } | |
319 | assertFalse(foreground9.isCompleted()); | |
320 | } | |
321 | ||
322 | /** | |
323 | * One long background request and one short foreground request, the | |
324 | * foreground request should finish first | |
325 | */ | |
326 | @Test | |
327 | public void preemptedBackgroundRequest() { | |
328 | BackgroundRequest background9 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
329 | ForegroundRequest foreground10 = new ForegroundRequest(fForegroundTimeRange); | |
330 | fixture.sendRequest(background9); | |
331 | fixture.sendRequest(foreground10); | |
332 | try { | |
333 | foreground10.waitForCompletion(); | |
334 | } catch (InterruptedException e) { | |
335 | fail(); | |
336 | } | |
337 | assertTrue(foreground10.isCompleted()); | |
338 | assertFalse(background9.isCompleted()); | |
339 | } | |
340 | ||
341 | /** | |
342 | * Test if the scheduler is working as expected | |
343 | */ | |
b1b999ea | 344 | @Ignore |
306586b1 SD |
345 | @Test |
346 | public void executionOrder() { | |
ccf2bbb4 | 347 | List<String> expectedOrder = new LinkedList<>(); |
306586b1 SD |
348 | expectedOrder.add("FOREGROUND1"); |
349 | expectedOrder.add("FOREGROUND2"); | |
350 | expectedOrder.add("FOREGROUND3"); | |
351 | expectedOrder.add("FOREGROUND4"); | |
352 | expectedOrder.add("BACKGROUND1"); | |
353 | expectedOrder.add("FOREGROUND1"); | |
354 | expectedOrder.add("FOREGROUND2"); | |
355 | expectedOrder.add("FOREGROUND3"); | |
356 | expectedOrder.add("FOREGROUND4"); | |
357 | expectedOrder.add("BACKGROUND2"); | |
358 | ||
359 | fOrderList.clear(); | |
360 | fForegroundId = 0; | |
361 | fBackgroundId = 0; | |
362 | ||
363 | BackgroundRequest background1 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
364 | BackgroundRequest background2 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
365 | ||
366 | ForegroundRequest foreground1 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
367 | ForegroundRequest foreground2 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
368 | ForegroundRequest foreground3 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
369 | ForegroundRequest foreground4 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
370 | ||
371 | fixture.sendRequest(foreground1); | |
372 | fixture.sendRequest(foreground2); | |
373 | fixture.sendRequest(foreground3); | |
374 | fixture.sendRequest(foreground4); | |
375 | fixture.sendRequest(background1); | |
376 | fixture.sendRequest(background2); | |
377 | try { | |
378 | foreground1.waitForCompletion(); | |
379 | foreground2.waitForCompletion(); | |
380 | foreground3.waitForCompletion(); | |
381 | foreground4.waitForCompletion(); | |
382 | background1.waitForCompletion(); | |
383 | background2.waitForCompletion(); | |
384 | } catch (InterruptedException e) { | |
385 | fail(); | |
386 | } | |
387 | assertEquals(expectedOrder, fOrderList.subList(0, expectedOrder.size())); | |
388 | } | |
389 | ||
390 | // ------------------------------------------------------------------------ | |
391 | // Helper methods | |
392 | // ------------------------------------------------------------------------ | |
393 | ||
394 | private class BackgroundRequest extends TmfEventRequest { | |
306586b1 SD |
395 | private int nbEvents = 0; |
396 | private String backgroundName; | |
397 | ||
398 | BackgroundRequest(TmfTimeRange timeRange) { | |
7184fc40 AM |
399 | super(fixture.getEventType(), |
400 | timeRange, | |
401 | 0, | |
2740e05c | 402 | ITmfEventRequest.ALL_DATA, |
306586b1 SD |
403 | ExecutionType.BACKGROUND); |
404 | backgroundName = getExecType().toString() + ++fBackgroundId; | |
405 | } | |
406 | ||
407 | @Override | |
408 | public void handleData(final ITmfEvent event) { | |
409 | super.handleData(event); | |
ab346a2b BH |
410 | synchronized (fOrderList) { |
411 | if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(backgroundName)) { | |
412 | fOrderList.add(backgroundName); | |
413 | } | |
306586b1 SD |
414 | } |
415 | ++nbEvents; | |
416 | } | |
417 | ||
418 | public int getNbEvents() { | |
419 | return nbEvents; | |
420 | } | |
421 | } | |
422 | ||
423 | private class ForegroundRequest extends TmfEventRequest { | |
306586b1 SD |
424 | private int nbEvents = 0; |
425 | private String foregroundName; | |
426 | ||
427 | ForegroundRequest(TmfTimeRange timeRange) { | |
7184fc40 AM |
428 | super(fixture.getEventType(), |
429 | timeRange, | |
430 | 0, | |
2740e05c | 431 | ITmfEventRequest.ALL_DATA, |
306586b1 SD |
432 | ExecutionType.FOREGROUND); |
433 | foregroundName = getExecType().toString() + ++fForegroundId; | |
434 | } | |
435 | ||
436 | @Override | |
437 | public void handleData(final ITmfEvent event) { | |
438 | super.handleData(event); | |
ab346a2b BH |
439 | synchronized (fOrderList) { |
440 | if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(foregroundName)) { | |
441 | fOrderList.add(foregroundName); | |
442 | } | |
306586b1 SD |
443 | } |
444 | ++nbEvents; | |
445 | } | |
446 | ||
447 | public int getNbEvents() { | |
448 | return nbEvents; | |
449 | } | |
450 | } | |
9d63098b | 451 | } |