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
.linuxtools
.tmf
.core
.tests
.trace
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertNull
;
21 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import java
.net
.URISyntaxException
;
27 import java
.util
.Vector
;
29 import org
.eclipse
.core
.resources
.IFile
;
30 import org
.eclipse
.core
.runtime
.FileLocator
;
31 import org
.eclipse
.core
.runtime
.Path
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
49 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
50 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
51 import org
.junit
.Before
;
52 import org
.junit
.Test
;
55 * Test suite for the TmfExperiment class (single trace).
57 @SuppressWarnings("javadoc")
58 public class TmfExperimentTest
{
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 private static final String EXPERIMENT
= "MyExperiment";
65 private static int NB_EVENTS
= 10000;
66 private static int BLOCK_SIZE
= 1000;
68 private static final double DELTA
= 1e-15;
70 private ITmfTrace
[] fTestTraces
;
71 private TmfExperimentStub fExperiment
;
73 private static byte SCALE
= (byte) -3;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
79 private synchronized ITmfTrace
[] setupTrace(final String path
) {
80 if (fTestTraces
== null) {
81 fTestTraces
= new ITmfTrace
[1];
83 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
84 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
85 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true, null);
86 fTestTraces
[0] = trace
;
87 } catch (final TmfTraceException e
) {
89 } catch (final URISyntaxException e
) {
91 } catch (final IOException e
) {
98 private synchronized void setupExperiment() {
99 if (fExperiment
== null) {
100 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
101 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
106 public void setUp() {
107 setupTrace(TmfTestTrace
.A_TEST_10K
.getFullPath());
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 public void testSimpleTmfExperimentConstructor() {
117 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
);
118 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
119 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
120 experiment
.dispose();
122 experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
123 experiment
.dispose();
127 public void testNormalTmfExperimentConstructor() {
128 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
129 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
131 final long nbExperimentEvents
= fExperiment
.getNbEvents();
132 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
134 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
135 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
137 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
138 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
139 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
142 // ------------------------------------------------------------------------
144 // ------------------------------------------------------------------------
147 public void testExperimentInitialization() {
149 * Calling default constructor, then init should be equivalent to
150 * calling the full constructor
153 TmfExperimentStub experiment
= new TmfExperimentStub();
154 experiment
.initExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
, 5000, null);
155 experiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
157 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
158 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
160 final long nbExperimentEvents
= fExperiment
.getNbEvents();
161 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
163 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
164 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
166 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
167 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
168 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 public void testGetTimestamp() {
177 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
178 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
179 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
180 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
181 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
182 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
183 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
184 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
185 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
188 // ------------------------------------------------------------------------
189 // Bookmarks file handling
190 // ------------------------------------------------------------------------
193 public void testBookmarks() {
194 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
195 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
196 fExperiment
.setBookmarksFile(bookmarks
);
197 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
200 // ------------------------------------------------------------------------
201 // State system, statistics and modules methods
202 // ------------------------------------------------------------------------
205 public void testGetAnalysisModules() {
206 /* There should not be any modules at this point */
207 Iterable
<IAnalysisModule
> modules
= fExperiment
.getAnalysisModules();
208 assertFalse(modules
.iterator().hasNext());
211 // ------------------------------------------------------------------------
212 // seekEvent by location
213 // ------------------------------------------------------------------------
216 public void testSeekBadLocation() {
217 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
218 assertNull("seekEvent", context
);
222 public void testSeekNoTrace() {
223 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
224 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
225 assertNull("seekEvent", context
);
226 experiment
.dispose();
229 // ------------------------------------------------------------------------
230 // seekEvent on ratio
231 // ------------------------------------------------------------------------
234 public void testSeekEventOnRatio() {
236 ITmfContext context
= fExperiment
.seekEvent(0.0);
237 assertEquals("Context rank", 0, context
.getRank());
238 ITmfEvent event
= fExperiment
.parseEvent(context
);
239 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
240 assertEquals("Context rank", 0, context
.getRank());
243 int midTrace
= NB_EVENTS
/ 2;
244 context
= fExperiment
.seekEvent(0.5);
245 assertEquals("Context rank", midTrace
, context
.getRank());
246 event
= fExperiment
.parseEvent(context
);
247 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
248 assertEquals("Context rank", midTrace
, context
.getRank());
251 context
= fExperiment
.seekEvent(1.0);
252 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
253 event
= fExperiment
.parseEvent(context
);
254 assertNull("Event timestamp", event
);
255 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
258 context
= fExperiment
.seekEvent(1.1);
259 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
260 event
= fExperiment
.parseEvent(context
);
261 assertNull("Event timestamp", event
);
262 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
265 context
= fExperiment
.seekEvent(-0.5);
266 assertEquals("Context rank", 0, context
.getRank());
267 event
= fExperiment
.parseEvent(context
);
268 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
269 assertEquals("Context rank", 0, context
.getRank());
273 public void testGetLocationRatio() {
275 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
276 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
277 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
280 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
281 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
282 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
285 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
286 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
287 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
290 // @SuppressWarnings("rawtypes")
291 // public void testGetCurrentLocation() {
292 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
293 // ITmfLocation location = fExperiment.getCurrentLocation();
294 // assertEquals("getCurrentLocation", location, context.getLocation());
297 // ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
302 public void testSeekRankOnCacheBoundary() {
303 long cacheSize
= fExperiment
.getCacheSize();
305 // On lower bound, returns the first event (TS = 1)
306 ITmfContext context
= fExperiment
.seekEvent(0);
307 assertEquals("Context rank", 0, context
.getRank());
309 ITmfEvent event
= fExperiment
.getNext(context
);
310 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
311 assertEquals("Context rank", 1, context
.getRank());
313 // Position trace at event rank [cacheSize]
314 context
= fExperiment
.seekEvent(cacheSize
);
315 assertEquals("Context rank", cacheSize
, context
.getRank());
317 event
= fExperiment
.getNext(context
);
318 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
319 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
321 // Position trace at event rank [4 * cacheSize]
322 context
= fExperiment
.seekEvent(4 * cacheSize
);
323 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
325 event
= fExperiment
.getNext(context
);
326 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
327 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
331 public void testSeekRankNotOnCacheBoundary() {
332 long cacheSize
= fExperiment
.getCacheSize();
334 // Position trace at event rank 9
335 ITmfContext context
= fExperiment
.seekEvent(9);
336 assertEquals("Context rank", 9, context
.getRank());
338 ITmfEvent event
= fExperiment
.getNext(context
);
339 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
340 assertEquals("Context rank", 10, context
.getRank());
342 // Position trace at event rank [cacheSize - 1]
343 context
= fExperiment
.seekEvent(cacheSize
- 1);
344 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
346 event
= fExperiment
.getNext(context
);
347 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
348 assertEquals("Context rank", cacheSize
, context
.getRank());
350 // Position trace at event rank [cacheSize + 1]
351 context
= fExperiment
.seekEvent(cacheSize
+ 1);
352 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
354 event
= fExperiment
.getNext(context
);
355 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
356 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
358 // Position trace at event rank 4500
359 context
= fExperiment
.seekEvent(4500);
360 assertEquals("Context rank", 4500, context
.getRank());
362 event
= fExperiment
.getNext(context
);
363 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
364 assertEquals("Context rank", 4501, context
.getRank());
368 public void testSeekRankOutOfScope() {
369 // Position trace at beginning
370 ITmfContext context
= fExperiment
.seekEvent(-1);
371 assertEquals("Event rank", 0, context
.getRank());
373 ITmfEvent event
= fExperiment
.getNext(context
);
374 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
375 assertEquals("Context rank", 1, context
.getRank());
377 // Position trace at event passed the end
378 context
= fExperiment
.seekEvent(NB_EVENTS
);
379 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
381 event
= fExperiment
.getNext(context
);
382 assertNull("Event", event
);
383 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
386 // ------------------------------------------------------------------------
387 // seekEvent on timestamp
388 // ------------------------------------------------------------------------
391 public void testSeekTimestampOnCacheBoundary() {
392 long cacheSize
= fExperiment
.getCacheSize();
394 // Position trace at event rank 0
395 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
396 assertEquals("Context rank", 0, context
.getRank());
398 ITmfEvent event
= fExperiment
.getNext(context
);
399 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
400 assertEquals("Context rank", 1, context
.getRank());
402 // Position trace at event rank [cacheSize]
403 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
404 assertEquals("Event rank", cacheSize
, context
.getRank());
406 event
= fExperiment
.getNext(context
);
407 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
408 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
410 // Position trace at event rank [4 * cacheSize]
411 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
412 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
414 event
= fExperiment
.getNext(context
);
415 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
416 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
420 public void testSeekTimestampNotOnCacheBoundary() {
421 // Position trace at event rank 1 (TS = 2)
422 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
423 assertEquals("Context rank", 1, context
.getRank());
425 ITmfEvent event
= fExperiment
.getNext(context
);
426 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
427 assertEquals("Context rank", 2, context
.getRank());
429 // Position trace at event rank 9 (TS = 10)
430 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
431 assertEquals("Context rank", 9, context
.getRank());
433 event
= fExperiment
.getNext(context
);
434 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
435 assertEquals("Context rank", 10, context
.getRank());
437 // Position trace at event rank 999 (TS = 1000)
438 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
439 assertEquals("Context rank", 999, context
.getRank());
441 event
= fExperiment
.getNext(context
);
442 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
443 assertEquals("Context rank", 1000, context
.getRank());
445 // Position trace at event rank 1001 (TS = 1002)
446 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
447 assertEquals("Context rank", 1001, context
.getRank());
449 event
= fExperiment
.getNext(context
);
450 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
451 assertEquals("Context rank", 1002, context
.getRank());
453 // Position trace at event rank 4500 (TS = 4501)
454 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
455 assertEquals("Context rank", 4500, context
.getRank());
457 event
= fExperiment
.getNext(context
);
458 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
459 assertEquals("Context rank", 4501, context
.getRank());
463 public void testSeekTimestampOutOfScope() {
464 // Position trace at beginning
465 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
466 assertEquals("Event rank", 0, context
.getRank());
468 ITmfEvent event
= fExperiment
.getNext(context
);
469 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
470 assertEquals("Event rank", 1, context
.getRank());
472 // Position trace at event passed the end
473 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
474 event
= fExperiment
.getNext(context
);
475 assertNull("Event location", event
);
476 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
479 // ------------------------------------------------------------------------
480 // seekEvent by location (context rank is undefined)
481 // ------------------------------------------------------------------------
484 public void testSeekLocationOnCacheBoundary() {
485 long cacheSize
= fExperiment
.getCacheSize();
487 // Position trace at event rank 0
488 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
489 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
491 ITmfEvent event
= fExperiment
.getNext(context
);
492 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
494 event
= fExperiment
.getNext(context
);
495 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
497 // Position trace at event rank 'cacheSize'
498 tmpContext
= fExperiment
.seekEvent(cacheSize
);
499 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
501 event
= fExperiment
.getNext(context
);
502 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
504 event
= fExperiment
.getNext(context
);
505 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
507 // Position trace at event rank 4 * 'cacheSize'
508 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
509 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
511 event
= fExperiment
.getNext(context
);
512 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
514 event
= fExperiment
.getNext(context
);
515 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
519 public void testSeekLocationNotOnCacheBoundary() {
520 long cacheSize
= fExperiment
.getCacheSize();
522 // Position trace at event 'cacheSize' - 1
523 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
524 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
526 ITmfEvent event
= fExperiment
.getNext(context
);
527 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
529 event
= fExperiment
.getNext(context
);
530 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
532 // Position trace at event rank 2 * 'cacheSize' - 1
533 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
534 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
535 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
537 event
= fExperiment
.getNext(context
);
538 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
540 event
= fExperiment
.getNext(context
);
541 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
543 // Position trace at event rank 4500
544 tmpContext
= fExperiment
.seekEvent(4500);
545 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
547 event
= fExperiment
.getNext(context
);
548 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
550 event
= fExperiment
.getNext(context
);
551 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
555 public void testSeekLocationOutOfScope() {
556 // Position trace at beginning
557 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
559 ITmfEvent event
= fExperiment
.getNext(context
);
560 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
563 // ------------------------------------------------------------------------
564 // getNext - updates the context
565 // ------------------------------------------------------------------------
567 private static void validateContextRanks(ITmfContext context
) {
568 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
569 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
571 int nbTraces
= ctx
.getContexts().length
;
573 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
574 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
575 for (int i
= 0; i
< nbTraces
; i
++) {
576 long rank
= ctx
.getContexts()[i
].getRank();
583 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
587 public void testGetNextAfteSeekingOnTS_1() {
589 final long INITIAL_TS
= 1;
590 final int NB_READS
= 20;
592 // On lower bound, returns the first event (ts = 1)
593 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
595 validateContextRanks(context
);
599 for (int i
= 0; i
< NB_READS
; i
++) {
600 event
= fExperiment
.getNext(context
);
601 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
602 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
605 // Make sure we stay positioned
606 event
= fExperiment
.parseEvent(context
);
607 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
608 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
610 validateContextRanks(context
);
614 public void testGetNextAfteSeekingOnTS_2() {
615 final long INITIAL_TS
= 2;
616 final int NB_READS
= 20;
618 // On lower bound, returns the first event (ts = 2)
619 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
621 validateContextRanks(context
);
625 for (int i
= 0; i
< NB_READS
; i
++) {
626 event
= fExperiment
.getNext(context
);
627 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
628 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
631 // Make sure we stay positioned
632 event
= fExperiment
.parseEvent(context
);
633 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
634 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
636 validateContextRanks(context
);
640 public void testGetNextAfteSeekingOnTS_3() {
642 final long INITIAL_TS
= 500;
643 final int NB_READS
= 20;
645 // On lower bound, returns the first event (ts = 500)
646 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
648 validateContextRanks(context
);
652 for (int i
= 0; i
< NB_READS
; i
++) {
653 event
= fExperiment
.getNext(context
);
654 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
655 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
658 // Make sure we stay positioned
659 event
= fExperiment
.parseEvent(context
);
660 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
661 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
663 validateContextRanks(context
);
667 public void testGetNextAfterSeekingOnRank_1() {
668 final long INITIAL_RANK
= 0L;
669 final int NB_READS
= 20;
671 // On lower bound, returns the first event (rank = 0)
672 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
674 validateContextRanks(context
);
678 for (int i
= 0; i
< NB_READS
; i
++) {
679 event
= fExperiment
.getNext(context
);
680 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
681 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
684 // Make sure we stay positioned
685 event
= fExperiment
.parseEvent(context
);
686 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
687 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
689 validateContextRanks(context
);
693 public void testGetNextAfterSeekingOnRank_2() {
694 final long INITIAL_RANK
= 1L;
695 final int NB_READS
= 20;
697 // On lower bound, returns the first event (rank = 0)
698 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
700 validateContextRanks(context
);
704 for (int i
= 0; i
< NB_READS
; i
++) {
705 event
= fExperiment
.getNext(context
);
706 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
707 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
710 // Make sure we stay positioned
711 event
= fExperiment
.parseEvent(context
);
712 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
713 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
715 validateContextRanks(context
);
719 public void testGetNextAfterSeekingOnRank_3() {
720 final long INITIAL_RANK
= 500L;
721 final int NB_READS
= 20;
723 // On lower bound, returns the first event (rank = 0)
724 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
726 validateContextRanks(context
);
730 for (int i
= 0; i
< NB_READS
; i
++) {
731 event
= fExperiment
.getNext(context
);
732 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
733 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
736 // Make sure we stay positioned
737 event
= fExperiment
.parseEvent(context
);
738 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
739 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
741 validateContextRanks(context
);
745 public void testGetNextAfterSeekingOnLocation_1() {
746 final ITmfLocation INITIAL_LOC
= null;
747 final long INITIAL_TS
= 1;
748 final int NB_READS
= 20;
750 // On lower bound, returns the first event (ts = 1)
751 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
753 validateContextRanks(context
);
757 for (int i
= 0; i
< NB_READS
; i
++) {
758 event
= fExperiment
.getNext(context
);
759 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
760 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
763 // Make sure we stay positioned
764 event
= fExperiment
.parseEvent(context
);
765 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
766 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
768 validateContextRanks(context
);
772 public void testGetNextAfterSeekingOnLocation_2() {
773 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
774 final long INITIAL_TS
= 2;
775 final int NB_READS
= 20;
777 // On lower bound, returns the first event (ts = 2)
778 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
780 validateContextRanks(context
);
784 for (int i
= 0; i
< NB_READS
; i
++) {
785 event
= fExperiment
.getNext(context
);
786 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
789 // Make sure we stay positioned
790 event
= fExperiment
.parseEvent(context
);
791 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
793 validateContextRanks(context
);
797 public void testGetNextAfterSeekingOnLocation_3() {
798 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
799 final long INITIAL_TS
= 501;
800 final int NB_READS
= 20;
802 // On lower bound, returns the first event (ts = 501)
803 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
805 validateContextRanks(context
);
809 for (int i
= 0; i
< NB_READS
; i
++) {
810 event
= fExperiment
.getNext(context
);
811 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
814 // Make sure we stay positioned
815 event
= fExperiment
.parseEvent(context
);
816 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
818 validateContextRanks(context
);
822 public void testGetNextLocation() {
823 ITmfContext context1
= fExperiment
.seekEvent(0);
824 fExperiment
.getNext(context1
);
825 ITmfLocation location
= context1
.getLocation();
826 ITmfEvent event1
= fExperiment
.getNext(context1
);
827 ITmfContext context2
= fExperiment
.seekEvent(location
);
828 ITmfEvent event2
= fExperiment
.getNext(context2
);
829 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
833 public void testGetNextEndLocation() {
834 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
835 fExperiment
.getNext(context1
);
836 ITmfLocation location
= context1
.getLocation();
837 ITmfContext context2
= fExperiment
.seekEvent(location
);
838 ITmfEvent event
= fExperiment
.getNext(context2
);
839 assertNull("Event", event
);
842 // ------------------------------------------------------------------------
844 // ------------------------------------------------------------------------
847 public void testProcessRequestForNbEvents() throws InterruptedException
{
848 final int nbEvents
= 1000;
849 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
851 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
852 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
853 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
855 public void handleData(final ITmfEvent event
) {
856 super.handleData(event
);
857 requestedEvents
.add(event
);
860 fExperiment
.sendRequest(request
);
861 request
.waitForCompletion();
863 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
864 assertTrue("isCompleted", request
.isCompleted());
865 assertFalse("isCancelled", request
.isCancelled());
867 // Ensure that we have distinct events.
868 // Don't go overboard: we are not validating the stub!
869 for (int i
= 0; i
< nbEvents
; i
++) {
870 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
875 public void testProcessRequestForAllEvents() throws InterruptedException
{
876 final int nbEvents
= ITmfEventRequest
.ALL_DATA
;
877 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
878 final long nbExpectedEvents
= NB_EVENTS
;
880 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
881 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
882 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
884 public void handleData(final ITmfEvent event
) {
885 super.handleData(event
);
886 requestedEvents
.add(event
);
889 fExperiment
.sendRequest(request
);
890 request
.waitForCompletion();
892 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
893 assertTrue("isCompleted", request
.isCompleted());
894 assertFalse("isCancelled", request
.isCancelled());
896 // Ensure that we have distinct events.
897 // Don't go overboard: we are not validating the stub!
898 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
899 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
903 // ------------------------------------------------------------------------
905 // ------------------------------------------------------------------------
908 public void testCancel() throws InterruptedException
{
909 final int nbEvents
= NB_EVENTS
;
910 final int limit
= BLOCK_SIZE
;
911 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
913 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
914 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
915 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
919 public void handleData(final ITmfEvent event
) {
920 super.handleData(event
);
921 requestedEvents
.add(event
);
922 if (++nbRead
== limit
) {
928 public void handleCancel() {
929 if (requestedEvents
.size() < limit
) {
930 System
.out
.println("aie");
934 fExperiment
.sendRequest(request
);
935 request
.waitForCompletion();
937 assertEquals("nbEvents", limit
, requestedEvents
.size());
938 assertTrue("isCompleted", request
.isCompleted());
939 assertTrue("isCancelled", request
.isCancelled());