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