1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Trace Model
12 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for rank in experiment location
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertNotNull
;
21 import static org
.junit
.Assert
.assertNull
;
22 import static org
.junit
.Assert
.assertTrue
;
25 import java
.io
.IOException
;
26 import java
.net
.URISyntaxException
;
28 import java
.util
.Vector
;
30 import org
.eclipse
.core
.runtime
.FileLocator
;
31 import org
.eclipse
.core
.runtime
.Path
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfExperiment
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
50 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestExperimentAnalysis
;
51 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
52 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
53 import org
.junit
.Before
;
54 import org
.junit
.Test
;
57 * Test suite for the TmfExperiment class (single trace).
59 @SuppressWarnings("javadoc")
60 public class TmfExperimentTest
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 private static final String EXPERIMENT
= "MyExperiment";
67 private static int NB_EVENTS
= 10000;
68 private static int BLOCK_SIZE
= 1000;
70 private static final double DELTA
= 1e-15;
72 private ITmfTrace
[] fTestTraces
;
73 private TmfExperimentStub fExperiment
;
75 private static byte SCALE
= (byte) -3;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 private synchronized ITmfTrace
[] setupTrace(final String path
) {
82 if (fTestTraces
== null) {
83 fTestTraces
= new ITmfTrace
[1];
85 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
86 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
87 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true, null);
88 fTestTraces
[0] = trace
;
89 } catch (final TmfTraceException e
) {
91 } catch (final URISyntaxException e
) {
93 } catch (final IOException e
) {
100 private synchronized void setupExperiment() {
101 if (fExperiment
== null) {
102 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
103 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
108 public void setUp() {
109 setupTrace(TmfTestTrace
.A_TEST_10K
.getFullPath());
113 // ------------------------------------------------------------------------
115 // ------------------------------------------------------------------------
118 public void testSimpleTmfExperimentConstructor() {
119 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
);
120 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
121 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
122 experiment
.dispose();
124 experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
125 experiment
.dispose();
129 public void testNormalTmfExperimentConstructor() {
130 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
131 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
133 final long nbExperimentEvents
= fExperiment
.getNbEvents();
134 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
136 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
137 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
139 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
140 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
141 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 public void testExperimentInitialization() {
151 * Calling default constructor, then init should be equivalent to
152 * calling the full constructor
155 TmfExperimentStub experiment
= new TmfExperimentStub();
156 experiment
.initExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
, 5000, null);
157 experiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
159 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
160 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
162 final long nbExperimentEvents
= fExperiment
.getNbEvents();
163 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
165 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
166 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
168 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
169 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
170 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
173 // ------------------------------------------------------------------------
175 // ------------------------------------------------------------------------
178 public void testGetTimestamp() {
179 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
180 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
181 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
182 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
183 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
184 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
185 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
186 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
187 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
190 // ------------------------------------------------------------------------
191 // State system, statistics and modules methods
192 // ------------------------------------------------------------------------
195 public void testGetAnalysisModules() {
196 /* There should not be any modules at this point */
197 Iterable
<IAnalysisModule
> modules
= fExperiment
.getAnalysisModules();
198 assertFalse(modules
.iterator().hasNext());
200 /* Open the experiment, the modules should be populated */
201 fExperiment
.traceOpened(new TmfTraceOpenedSignal(this, fExperiment
, null));
202 modules
= fExperiment
.getAnalysisModules();
203 Iterable
<TestExperimentAnalysis
> testModules
= fExperiment
.getAnalysisModulesOfClass(TestExperimentAnalysis
.class);
204 assertTrue(modules
.iterator().hasNext());
205 assertTrue(testModules
.iterator().hasNext());
208 // ------------------------------------------------------------------------
209 // seekEvent by location
210 // ------------------------------------------------------------------------
213 public void testSeekBadLocation() {
214 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
215 assertNull("seekEvent", context
);
219 public void testSeekNoTrace() {
220 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
221 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
222 assertNull("seekEvent", context
);
223 experiment
.dispose();
226 // ------------------------------------------------------------------------
227 // seekEvent on ratio
228 // ------------------------------------------------------------------------
231 public void testSeekEventOnRatio() {
233 ITmfContext context
= fExperiment
.seekEvent(0.0);
234 assertEquals("Context rank", 0, context
.getRank());
235 ITmfEvent event
= fExperiment
.parseEvent(context
);
236 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
237 assertEquals("Context rank", 0, context
.getRank());
240 int midTrace
= NB_EVENTS
/ 2;
241 context
= fExperiment
.seekEvent(0.5);
242 assertEquals("Context rank", midTrace
, context
.getRank());
243 event
= fExperiment
.parseEvent(context
);
244 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
245 assertEquals("Context rank", midTrace
, context
.getRank());
248 context
= fExperiment
.seekEvent(1.0);
249 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
250 event
= fExperiment
.parseEvent(context
);
251 assertNull("Event timestamp", event
);
252 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
255 context
= fExperiment
.seekEvent(1.1);
256 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
257 event
= fExperiment
.parseEvent(context
);
258 assertNull("Event timestamp", event
);
259 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
262 context
= fExperiment
.seekEvent(-0.5);
263 assertEquals("Context rank", 0, context
.getRank());
264 event
= fExperiment
.parseEvent(context
);
265 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
266 assertEquals("Context rank", 0, context
.getRank());
270 public void testGetLocationRatio() {
272 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
273 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
274 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
277 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
278 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
279 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
282 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
283 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
284 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
287 // @SuppressWarnings("rawtypes")
288 // public void testGetCurrentLocation() {
289 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
290 // ITmfLocation location = fExperiment.getCurrentLocation();
291 // assertEquals("getCurrentLocation", location, context.getLocation());
294 // ------------------------------------------------------------------------
296 // ------------------------------------------------------------------------
299 public void testSeekRankOnCacheBoundary() {
300 long cacheSize
= fExperiment
.getCacheSize();
302 // On lower bound, returns the first event (TS = 1)
303 ITmfContext context
= fExperiment
.seekEvent(0);
304 assertEquals("Context rank", 0, context
.getRank());
306 ITmfEvent event
= fExperiment
.getNext(context
);
307 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
308 assertEquals("Context rank", 1, context
.getRank());
310 // Position trace at event rank [cacheSize]
311 context
= fExperiment
.seekEvent(cacheSize
);
312 assertEquals("Context rank", cacheSize
, context
.getRank());
314 event
= fExperiment
.getNext(context
);
315 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
316 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
318 // Position trace at event rank [4 * cacheSize]
319 context
= fExperiment
.seekEvent(4 * cacheSize
);
320 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
322 event
= fExperiment
.getNext(context
);
323 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
324 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
328 public void testSeekRankNotOnCacheBoundary() {
329 long cacheSize
= fExperiment
.getCacheSize();
331 // Position trace at event rank 9
332 ITmfContext context
= fExperiment
.seekEvent(9);
333 assertEquals("Context rank", 9, context
.getRank());
335 ITmfEvent event
= fExperiment
.getNext(context
);
336 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
337 assertEquals("Context rank", 10, context
.getRank());
339 // Position trace at event rank [cacheSize - 1]
340 context
= fExperiment
.seekEvent(cacheSize
- 1);
341 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
343 event
= fExperiment
.getNext(context
);
344 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
345 assertEquals("Context rank", cacheSize
, context
.getRank());
347 // Position trace at event rank [cacheSize + 1]
348 context
= fExperiment
.seekEvent(cacheSize
+ 1);
349 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
351 event
= fExperiment
.getNext(context
);
352 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
353 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
355 // Position trace at event rank 4500
356 context
= fExperiment
.seekEvent(4500);
357 assertEquals("Context rank", 4500, context
.getRank());
359 event
= fExperiment
.getNext(context
);
360 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
361 assertEquals("Context rank", 4501, context
.getRank());
365 public void testSeekRankOutOfScope() {
366 // Position trace at beginning
367 ITmfContext context
= fExperiment
.seekEvent(-1);
368 assertEquals("Event rank", 0, context
.getRank());
370 ITmfEvent event
= fExperiment
.getNext(context
);
371 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
372 assertEquals("Context rank", 1, context
.getRank());
374 // Position trace at event passed the end
375 context
= fExperiment
.seekEvent(NB_EVENTS
);
376 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
378 event
= fExperiment
.getNext(context
);
379 assertNull("Event", event
);
380 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
383 // ------------------------------------------------------------------------
384 // seekEvent on timestamp
385 // ------------------------------------------------------------------------
388 public void testSeekTimestampOnCacheBoundary() {
389 long cacheSize
= fExperiment
.getCacheSize();
391 // Position trace at event rank 0
392 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
393 assertEquals("Context rank", 0, context
.getRank());
395 ITmfEvent event
= fExperiment
.getNext(context
);
396 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
397 assertEquals("Context rank", 1, context
.getRank());
399 // Position trace at event rank [cacheSize]
400 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
401 assertEquals("Event rank", cacheSize
, context
.getRank());
403 event
= fExperiment
.getNext(context
);
404 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
405 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
407 // Position trace at event rank [4 * cacheSize]
408 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
409 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
411 event
= fExperiment
.getNext(context
);
412 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
413 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
417 public void testSeekTimestampNotOnCacheBoundary() {
418 // Position trace at event rank 1 (TS = 2)
419 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
420 assertEquals("Context rank", 1, context
.getRank());
422 ITmfEvent event
= fExperiment
.getNext(context
);
423 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
424 assertEquals("Context rank", 2, context
.getRank());
426 // Position trace at event rank 9 (TS = 10)
427 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
428 assertEquals("Context rank", 9, context
.getRank());
430 event
= fExperiment
.getNext(context
);
431 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
432 assertEquals("Context rank", 10, context
.getRank());
434 // Position trace at event rank 999 (TS = 1000)
435 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
436 assertEquals("Context rank", 999, context
.getRank());
438 event
= fExperiment
.getNext(context
);
439 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
440 assertEquals("Context rank", 1000, context
.getRank());
442 // Position trace at event rank 1001 (TS = 1002)
443 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
444 assertEquals("Context rank", 1001, context
.getRank());
446 event
= fExperiment
.getNext(context
);
447 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
448 assertEquals("Context rank", 1002, context
.getRank());
450 // Position trace at event rank 4500 (TS = 4501)
451 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
452 assertEquals("Context rank", 4500, context
.getRank());
454 event
= fExperiment
.getNext(context
);
455 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
456 assertEquals("Context rank", 4501, context
.getRank());
460 public void testSeekTimestampOutOfScope() {
461 // Position trace at beginning
462 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
463 assertEquals("Event rank", 0, context
.getRank());
465 ITmfEvent event
= fExperiment
.getNext(context
);
466 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
467 assertEquals("Event rank", 1, context
.getRank());
469 // Position trace at event passed the end
470 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
471 event
= fExperiment
.getNext(context
);
472 assertNull("Event location", event
);
473 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
476 // ------------------------------------------------------------------------
477 // seekEvent by location (context rank is undefined)
478 // ------------------------------------------------------------------------
481 public void testSeekLocationOnCacheBoundary() {
482 long cacheSize
= fExperiment
.getCacheSize();
484 // Position trace at event rank 0
485 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
486 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
488 ITmfEvent event
= fExperiment
.getNext(context
);
489 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
491 event
= fExperiment
.getNext(context
);
492 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
494 // Position trace at event rank 'cacheSize'
495 tmpContext
= fExperiment
.seekEvent(cacheSize
);
496 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
498 event
= fExperiment
.getNext(context
);
499 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
501 event
= fExperiment
.getNext(context
);
502 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
504 // Position trace at event rank 4 * 'cacheSize'
505 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
506 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
508 event
= fExperiment
.getNext(context
);
509 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
511 event
= fExperiment
.getNext(context
);
512 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
516 public void testSeekLocationNotOnCacheBoundary() {
517 long cacheSize
= fExperiment
.getCacheSize();
519 // Position trace at event 'cacheSize' - 1
520 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
521 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
523 ITmfEvent event
= fExperiment
.getNext(context
);
524 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
526 event
= fExperiment
.getNext(context
);
527 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
529 // Position trace at event rank 2 * 'cacheSize' - 1
530 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
531 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
532 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
534 event
= fExperiment
.getNext(context
);
535 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
537 event
= fExperiment
.getNext(context
);
538 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
540 // Position trace at event rank 4500
541 tmpContext
= fExperiment
.seekEvent(4500);
542 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
544 event
= fExperiment
.getNext(context
);
545 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
547 event
= fExperiment
.getNext(context
);
548 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
552 public void testSeekLocationOutOfScope() {
553 // Position trace at beginning
554 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
556 ITmfEvent event
= fExperiment
.getNext(context
);
557 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
560 // ------------------------------------------------------------------------
561 // getNext - updates the context
562 // ------------------------------------------------------------------------
564 private static void validateContextRanks(ITmfContext context
) {
565 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
566 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
568 int nbTraces
= ctx
.getNbTraces();
570 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
571 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
572 for (int i
= 0; i
< nbTraces
; i
++) {
573 ITmfContext subContext
= ctx
.getContext(i
);
574 assertNotNull(subContext
);
575 long rank
= subContext
.getRank();
582 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
586 public void testGetNextAfteSeekingOnTS_1() {
588 final long INITIAL_TS
= 1;
589 final int NB_READS
= 20;
591 // On lower bound, returns the first event (ts = 1)
592 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
594 validateContextRanks(context
);
598 for (int i
= 0; i
< NB_READS
; i
++) {
599 event
= fExperiment
.getNext(context
);
600 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
601 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
604 // Make sure we stay positioned
605 event
= fExperiment
.parseEvent(context
);
606 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
607 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
609 validateContextRanks(context
);
613 public void testGetNextAfteSeekingOnTS_2() {
614 final long INITIAL_TS
= 2;
615 final int NB_READS
= 20;
617 // On lower bound, returns the first event (ts = 2)
618 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
620 validateContextRanks(context
);
624 for (int i
= 0; i
< NB_READS
; i
++) {
625 event
= fExperiment
.getNext(context
);
626 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
627 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
630 // Make sure we stay positioned
631 event
= fExperiment
.parseEvent(context
);
632 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
633 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
635 validateContextRanks(context
);
639 public void testGetNextAfteSeekingOnTS_3() {
641 final long INITIAL_TS
= 500;
642 final int NB_READS
= 20;
644 // On lower bound, returns the first event (ts = 500)
645 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
647 validateContextRanks(context
);
651 for (int i
= 0; i
< NB_READS
; i
++) {
652 event
= fExperiment
.getNext(context
);
653 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
654 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
657 // Make sure we stay positioned
658 event
= fExperiment
.parseEvent(context
);
659 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
660 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
662 validateContextRanks(context
);
666 public void testGetNextAfterSeekingOnRank_1() {
667 final long INITIAL_RANK
= 0L;
668 final int NB_READS
= 20;
670 // On lower bound, returns the first event (rank = 0)
671 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
673 validateContextRanks(context
);
677 for (int i
= 0; i
< NB_READS
; i
++) {
678 event
= fExperiment
.getNext(context
);
679 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
680 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
683 // Make sure we stay positioned
684 event
= fExperiment
.parseEvent(context
);
685 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
686 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
688 validateContextRanks(context
);
692 public void testGetNextAfterSeekingOnRank_2() {
693 final long INITIAL_RANK
= 1L;
694 final int NB_READS
= 20;
696 // On lower bound, returns the first event (rank = 0)
697 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
699 validateContextRanks(context
);
703 for (int i
= 0; i
< NB_READS
; i
++) {
704 event
= fExperiment
.getNext(context
);
705 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
706 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
709 // Make sure we stay positioned
710 event
= fExperiment
.parseEvent(context
);
711 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
712 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
714 validateContextRanks(context
);
718 public void testGetNextAfterSeekingOnRank_3() {
719 final long INITIAL_RANK
= 500L;
720 final int NB_READS
= 20;
722 // On lower bound, returns the first event (rank = 0)
723 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
725 validateContextRanks(context
);
729 for (int i
= 0; i
< NB_READS
; i
++) {
730 event
= fExperiment
.getNext(context
);
731 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
732 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
735 // Make sure we stay positioned
736 event
= fExperiment
.parseEvent(context
);
737 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
738 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
740 validateContextRanks(context
);
744 public void testGetNextAfterSeekingOnLocation_1() {
745 final ITmfLocation INITIAL_LOC
= null;
746 final long INITIAL_TS
= 1;
747 final int NB_READS
= 20;
749 // On lower bound, returns the first event (ts = 1)
750 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
752 validateContextRanks(context
);
756 for (int i
= 0; i
< NB_READS
; i
++) {
757 event
= fExperiment
.getNext(context
);
758 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
759 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
762 // Make sure we stay positioned
763 event
= fExperiment
.parseEvent(context
);
764 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
765 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
767 validateContextRanks(context
);
771 public void testGetNextAfterSeekingOnLocation_2() {
772 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
773 final long INITIAL_TS
= 2;
774 final int NB_READS
= 20;
776 // On lower bound, returns the first event (ts = 2)
777 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
779 validateContextRanks(context
);
783 for (int i
= 0; i
< NB_READS
; i
++) {
784 event
= fExperiment
.getNext(context
);
785 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
788 // Make sure we stay positioned
789 event
= fExperiment
.parseEvent(context
);
790 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
792 validateContextRanks(context
);
796 public void testGetNextAfterSeekingOnLocation_3() {
797 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
798 final long INITIAL_TS
= 501;
799 final int NB_READS
= 20;
801 // On lower bound, returns the first event (ts = 501)
802 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
804 validateContextRanks(context
);
808 for (int i
= 0; i
< NB_READS
; i
++) {
809 event
= fExperiment
.getNext(context
);
810 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
813 // Make sure we stay positioned
814 event
= fExperiment
.parseEvent(context
);
815 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
817 validateContextRanks(context
);
821 public void testGetNextLocation() {
822 ITmfContext context1
= fExperiment
.seekEvent(0);
823 fExperiment
.getNext(context1
);
824 ITmfLocation location
= context1
.getLocation();
825 ITmfEvent event1
= fExperiment
.getNext(context1
);
826 ITmfContext context2
= fExperiment
.seekEvent(location
);
827 ITmfEvent event2
= fExperiment
.getNext(context2
);
828 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
832 public void testGetNextEndLocation() {
833 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
834 fExperiment
.getNext(context1
);
835 ITmfLocation location
= context1
.getLocation();
836 ITmfContext context2
= fExperiment
.seekEvent(location
);
837 ITmfEvent event
= fExperiment
.getNext(context2
);
838 assertNull("Event", event
);
841 // ------------------------------------------------------------------------
843 // ------------------------------------------------------------------------
846 public void testProcessRequestForNbEvents() throws InterruptedException
{
847 final int nbEvents
= 1000;
848 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
850 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
851 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
852 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
854 public void handleData(final ITmfEvent event
) {
855 super.handleData(event
);
856 requestedEvents
.add(event
);
859 fExperiment
.sendRequest(request
);
860 request
.waitForCompletion();
862 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
863 assertTrue("isCompleted", request
.isCompleted());
864 assertFalse("isCancelled", request
.isCancelled());
866 // Ensure that we have distinct events.
867 // Don't go overboard: we are not validating the stub!
868 for (int i
= 0; i
< nbEvents
; i
++) {
869 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
874 public void testProcessRequestForAllEvents() throws InterruptedException
{
875 final int nbEvents
= ITmfEventRequest
.ALL_DATA
;
876 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
877 final long nbExpectedEvents
= NB_EVENTS
;
879 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
880 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
881 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
883 public void handleData(final ITmfEvent event
) {
884 super.handleData(event
);
885 requestedEvents
.add(event
);
888 fExperiment
.sendRequest(request
);
889 request
.waitForCompletion();
891 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
892 assertTrue("isCompleted", request
.isCompleted());
893 assertFalse("isCancelled", request
.isCancelled());
895 // Ensure that we have distinct events.
896 // Don't go overboard: we are not validating the stub!
897 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
898 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
902 // ------------------------------------------------------------------------
904 // ------------------------------------------------------------------------
907 public void testCancel() throws InterruptedException
{
908 final int nbEvents
= NB_EVENTS
;
909 final int limit
= BLOCK_SIZE
;
910 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
912 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
913 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
914 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
918 public void handleData(final ITmfEvent event
) {
919 super.handleData(event
);
920 requestedEvents
.add(event
);
921 if (++nbRead
== limit
) {
927 public void handleCancel() {
928 if (requestedEvents
.size() < limit
) {
929 System
.out
.println("aie");
933 fExperiment
.sendRequest(request
);
934 request
.waitForCompletion();
936 assertEquals("nbEvents", limit
, requestedEvents
.size());
937 assertTrue("isCompleted", request
.isCompleted());
938 assertTrue("isCancelled", request
.isCancelled());