1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * 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
.event
.ITmfEvent
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
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 DIRECTORY
= "testfiles";
65 private static final String TEST_STREAM
= "A-Test-10K";
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);
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(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
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 testGetTimestamp() {
150 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
151 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
152 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
153 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
154 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
155 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
156 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
157 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
158 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
161 // ------------------------------------------------------------------------
162 // Bookmarks file handling
163 // ------------------------------------------------------------------------
166 public void testBookmarks() {
167 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
168 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
169 fExperiment
.setBookmarksFile(bookmarks
);
170 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
173 // ------------------------------------------------------------------------
174 // State system and statistics methods
175 // ------------------------------------------------------------------------
178 public void testGetStatistics() {
179 /* There should not be any experiment-specific statistics */
180 ITmfStatistics stats
= fExperiment
.getStatistics();
185 public void testGetStateSystem() {
186 /* There should not be any experiment-specific state system */
187 ITmfStateSystem ss
= fExperiment
.getStateSystems().get("something");
191 // ------------------------------------------------------------------------
192 // seekEvent by location
193 // ------------------------------------------------------------------------
196 public void testSeekBadLocation() {
197 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
198 assertNull("seekEvent", context
);
202 public void testSeekNoTrace() {
203 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
204 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
205 assertNull("seekEvent", context
);
206 experiment
.dispose();
209 // ------------------------------------------------------------------------
210 // seekEvent on ratio
211 // ------------------------------------------------------------------------
214 public void testSeekEventOnRatio() {
216 ITmfContext context
= fExperiment
.seekEvent(0.0);
217 assertEquals("Context rank", 0, context
.getRank());
218 ITmfEvent event
= fExperiment
.parseEvent(context
);
219 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
220 assertEquals("Context rank", 0, context
.getRank());
223 int midTrace
= NB_EVENTS
/ 2;
224 context
= fExperiment
.seekEvent(0.5);
225 assertEquals("Context rank", midTrace
, context
.getRank());
226 event
= fExperiment
.parseEvent(context
);
227 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
228 assertEquals("Context rank", midTrace
, context
.getRank());
231 context
= fExperiment
.seekEvent(1.0);
232 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
233 event
= fExperiment
.parseEvent(context
);
234 assertNull("Event timestamp", event
);
235 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
238 context
= fExperiment
.seekEvent(1.1);
239 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
240 event
= fExperiment
.parseEvent(context
);
241 assertNull("Event timestamp", event
);
242 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
245 context
= fExperiment
.seekEvent(-0.5);
246 assertEquals("Context rank", 0, context
.getRank());
247 event
= fExperiment
.parseEvent(context
);
248 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
249 assertEquals("Context rank", 0, context
.getRank());
253 public void testGetLocationRatio() {
255 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
256 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
257 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
260 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
261 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
262 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
265 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
266 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
267 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
270 // @SuppressWarnings("rawtypes")
271 // public void testGetCurrentLocation() {
272 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
273 // ITmfLocation location = fExperiment.getCurrentLocation();
274 // assertEquals("getCurrentLocation", location, context.getLocation());
277 // ------------------------------------------------------------------------
279 // ------------------------------------------------------------------------
282 public void testSeekRankOnCacheBoundary() {
283 long cacheSize
= fExperiment
.getCacheSize();
285 // On lower bound, returns the first event (TS = 1)
286 ITmfContext context
= fExperiment
.seekEvent(0);
287 assertEquals("Context rank", 0, context
.getRank());
289 ITmfEvent event
= fExperiment
.getNext(context
);
290 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
291 assertEquals("Context rank", 1, context
.getRank());
293 // Position trace at event rank [cacheSize]
294 context
= fExperiment
.seekEvent(cacheSize
);
295 assertEquals("Context rank", cacheSize
, context
.getRank());
297 event
= fExperiment
.getNext(context
);
298 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
299 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
301 // Position trace at event rank [4 * cacheSize]
302 context
= fExperiment
.seekEvent(4 * cacheSize
);
303 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
305 event
= fExperiment
.getNext(context
);
306 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
307 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
311 public void testSeekRankNotOnCacheBoundary() {
312 long cacheSize
= fExperiment
.getCacheSize();
314 // Position trace at event rank 9
315 ITmfContext context
= fExperiment
.seekEvent(9);
316 assertEquals("Context rank", 9, context
.getRank());
318 ITmfEvent event
= fExperiment
.getNext(context
);
319 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
320 assertEquals("Context rank", 10, context
.getRank());
322 // Position trace at event rank [cacheSize - 1]
323 context
= fExperiment
.seekEvent(cacheSize
- 1);
324 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
326 event
= fExperiment
.getNext(context
);
327 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
328 assertEquals("Context rank", cacheSize
, context
.getRank());
330 // Position trace at event rank [cacheSize + 1]
331 context
= fExperiment
.seekEvent(cacheSize
+ 1);
332 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
334 event
= fExperiment
.getNext(context
);
335 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
336 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
338 // Position trace at event rank 4500
339 context
= fExperiment
.seekEvent(4500);
340 assertEquals("Context rank", 4500, context
.getRank());
342 event
= fExperiment
.getNext(context
);
343 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
344 assertEquals("Context rank", 4501, context
.getRank());
348 public void testSeekRankOutOfScope() {
349 // Position trace at beginning
350 ITmfContext context
= fExperiment
.seekEvent(-1);
351 assertEquals("Event rank", 0, context
.getRank());
353 ITmfEvent event
= fExperiment
.getNext(context
);
354 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
355 assertEquals("Context rank", 1, context
.getRank());
357 // Position trace at event passed the end
358 context
= fExperiment
.seekEvent(NB_EVENTS
);
359 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
361 event
= fExperiment
.getNext(context
);
362 assertNull("Event", event
);
363 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
366 // ------------------------------------------------------------------------
367 // seekEvent on timestamp
368 // ------------------------------------------------------------------------
371 public void testSeekTimestampOnCacheBoundary() {
372 long cacheSize
= fExperiment
.getCacheSize();
374 // Position trace at event rank 0
375 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
376 assertEquals("Context rank", 0, context
.getRank());
378 ITmfEvent event
= fExperiment
.getNext(context
);
379 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
380 assertEquals("Context rank", 1, context
.getRank());
382 // Position trace at event rank [cacheSize]
383 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
384 assertEquals("Event rank", cacheSize
, context
.getRank());
386 event
= fExperiment
.getNext(context
);
387 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
388 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
390 // Position trace at event rank [4 * cacheSize]
391 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
392 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
394 event
= fExperiment
.getNext(context
);
395 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
396 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
400 public void testSeekTimestampNotOnCacheBoundary() {
401 // Position trace at event rank 1 (TS = 2)
402 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
403 assertEquals("Context rank", 1, context
.getRank());
405 ITmfEvent event
= fExperiment
.getNext(context
);
406 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
407 assertEquals("Context rank", 2, context
.getRank());
409 // Position trace at event rank 9 (TS = 10)
410 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
411 assertEquals("Context rank", 9, context
.getRank());
413 event
= fExperiment
.getNext(context
);
414 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
415 assertEquals("Context rank", 10, context
.getRank());
417 // Position trace at event rank 999 (TS = 1000)
418 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
419 assertEquals("Context rank", 999, context
.getRank());
421 event
= fExperiment
.getNext(context
);
422 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
423 assertEquals("Context rank", 1000, context
.getRank());
425 // Position trace at event rank 1001 (TS = 1002)
426 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
427 assertEquals("Context rank", 1001, context
.getRank());
429 event
= fExperiment
.getNext(context
);
430 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
431 assertEquals("Context rank", 1002, context
.getRank());
433 // Position trace at event rank 4500 (TS = 4501)
434 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
435 assertEquals("Context rank", 4500, context
.getRank());
437 event
= fExperiment
.getNext(context
);
438 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
439 assertEquals("Context rank", 4501, context
.getRank());
443 public void testSeekTimestampOutOfScope() {
444 // Position trace at beginning
445 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
446 assertEquals("Event rank", 0, context
.getRank());
448 ITmfEvent event
= fExperiment
.getNext(context
);
449 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
450 assertEquals("Event rank", 1, context
.getRank());
452 // Position trace at event passed the end
453 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
454 event
= fExperiment
.getNext(context
);
455 assertNull("Event location", event
);
456 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
459 // ------------------------------------------------------------------------
460 // seekEvent by location (context rank is undefined)
461 // ------------------------------------------------------------------------
464 public void testSeekLocationOnCacheBoundary() {
465 long cacheSize
= fExperiment
.getCacheSize();
467 // Position trace at event rank 0
468 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
469 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
471 ITmfEvent event
= fExperiment
.getNext(context
);
472 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
474 event
= fExperiment
.getNext(context
);
475 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
477 // Position trace at event rank 'cacheSize'
478 tmpContext
= fExperiment
.seekEvent(cacheSize
);
479 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
481 event
= fExperiment
.getNext(context
);
482 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
484 event
= fExperiment
.getNext(context
);
485 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
487 // Position trace at event rank 4 * 'cacheSize'
488 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
489 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
491 event
= fExperiment
.getNext(context
);
492 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
494 event
= fExperiment
.getNext(context
);
495 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
499 public void testSeekLocationNotOnCacheBoundary() {
500 long cacheSize
= fExperiment
.getCacheSize();
502 // Position trace at event 'cacheSize' - 1
503 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
504 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
506 ITmfEvent event
= fExperiment
.getNext(context
);
507 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
509 event
= fExperiment
.getNext(context
);
510 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
512 // Position trace at event rank 2 * 'cacheSize' - 1
513 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
514 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
515 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
517 event
= fExperiment
.getNext(context
);
518 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
520 event
= fExperiment
.getNext(context
);
521 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
523 // Position trace at event rank 4500
524 tmpContext
= fExperiment
.seekEvent(4500);
525 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
527 event
= fExperiment
.getNext(context
);
528 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
530 event
= fExperiment
.getNext(context
);
531 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
535 public void testSeekLocationOutOfScope() {
536 // Position trace at beginning
537 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
539 ITmfEvent event
= fExperiment
.getNext(context
);
540 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
543 // ------------------------------------------------------------------------
544 // getNext - updates the context
545 // ------------------------------------------------------------------------
547 private static void validateContextRanks(ITmfContext context
) {
548 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
549 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
551 int nbTraces
= ctx
.getContexts().length
;
553 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
554 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
555 for (int i
= 0; i
< nbTraces
; i
++) {
556 long rank
= ctx
.getContexts()[i
].getRank();
563 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
567 public void testGetNextAfteSeekingOnTS_1() {
569 final long INITIAL_TS
= 1;
570 final int NB_READS
= 20;
572 // On lower bound, returns the first event (ts = 1)
573 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
575 validateContextRanks(context
);
579 for (int i
= 0; i
< NB_READS
; i
++) {
580 event
= fExperiment
.getNext(context
);
581 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
582 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
585 // Make sure we stay positioned
586 event
= fExperiment
.parseEvent(context
);
587 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
588 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
590 validateContextRanks(context
);
594 public void testGetNextAfteSeekingOnTS_2() {
595 final long INITIAL_TS
= 2;
596 final int NB_READS
= 20;
598 // On lower bound, returns the first event (ts = 2)
599 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
601 validateContextRanks(context
);
605 for (int i
= 0; i
< NB_READS
; i
++) {
606 event
= fExperiment
.getNext(context
);
607 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
608 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
611 // Make sure we stay positioned
612 event
= fExperiment
.parseEvent(context
);
613 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
614 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
616 validateContextRanks(context
);
620 public void testGetNextAfteSeekingOnTS_3() {
622 final long INITIAL_TS
= 500;
623 final int NB_READS
= 20;
625 // On lower bound, returns the first event (ts = 500)
626 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
628 validateContextRanks(context
);
632 for (int i
= 0; i
< NB_READS
; i
++) {
633 event
= fExperiment
.getNext(context
);
634 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
635 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
638 // Make sure we stay positioned
639 event
= fExperiment
.parseEvent(context
);
640 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
641 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
643 validateContextRanks(context
);
647 public void testGetNextAfterSeekingOnRank_1() {
648 final long INITIAL_RANK
= 0L;
649 final int NB_READS
= 20;
651 // On lower bound, returns the first event (rank = 0)
652 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
654 validateContextRanks(context
);
658 for (int i
= 0; i
< NB_READS
; i
++) {
659 event
= fExperiment
.getNext(context
);
660 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
661 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
664 // Make sure we stay positioned
665 event
= fExperiment
.parseEvent(context
);
666 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
667 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
669 validateContextRanks(context
);
673 public void testGetNextAfterSeekingOnRank_2() {
674 final long INITIAL_RANK
= 1L;
675 final int NB_READS
= 20;
677 // On lower bound, returns the first event (rank = 0)
678 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
680 validateContextRanks(context
);
684 for (int i
= 0; i
< NB_READS
; i
++) {
685 event
= fExperiment
.getNext(context
);
686 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
687 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
690 // Make sure we stay positioned
691 event
= fExperiment
.parseEvent(context
);
692 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
693 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
695 validateContextRanks(context
);
699 public void testGetNextAfterSeekingOnRank_3() {
700 final long INITIAL_RANK
= 500L;
701 final int NB_READS
= 20;
703 // On lower bound, returns the first event (rank = 0)
704 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
706 validateContextRanks(context
);
710 for (int i
= 0; i
< NB_READS
; i
++) {
711 event
= fExperiment
.getNext(context
);
712 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
713 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
716 // Make sure we stay positioned
717 event
= fExperiment
.parseEvent(context
);
718 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
719 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
721 validateContextRanks(context
);
725 public void testGetNextAfterSeekingOnLocation_1() {
726 final ITmfLocation INITIAL_LOC
= null;
727 final long INITIAL_TS
= 1;
728 final int NB_READS
= 20;
730 // On lower bound, returns the first event (ts = 1)
731 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
733 validateContextRanks(context
);
737 for (int i
= 0; i
< NB_READS
; i
++) {
738 event
= fExperiment
.getNext(context
);
739 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
740 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
743 // Make sure we stay positioned
744 event
= fExperiment
.parseEvent(context
);
745 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
746 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
748 validateContextRanks(context
);
752 public void testGetNextAfterSeekingOnLocation_2() {
753 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
754 final long INITIAL_TS
= 2;
755 final int NB_READS
= 20;
757 // On lower bound, returns the first event (ts = 2)
758 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
760 validateContextRanks(context
);
764 for (int i
= 0; i
< NB_READS
; i
++) {
765 event
= fExperiment
.getNext(context
);
766 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
769 // Make sure we stay positioned
770 event
= fExperiment
.parseEvent(context
);
771 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
773 validateContextRanks(context
);
777 public void testGetNextAfterSeekingOnLocation_3() {
778 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
779 final long INITIAL_TS
= 501;
780 final int NB_READS
= 20;
782 // On lower bound, returns the first event (ts = 501)
783 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
785 validateContextRanks(context
);
789 for (int i
= 0; i
< NB_READS
; i
++) {
790 event
= fExperiment
.getNext(context
);
791 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
794 // Make sure we stay positioned
795 event
= fExperiment
.parseEvent(context
);
796 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
798 validateContextRanks(context
);
802 public void testGetNextLocation() {
803 ITmfContext context1
= fExperiment
.seekEvent(0);
804 fExperiment
.getNext(context1
);
805 ITmfLocation location
= context1
.getLocation();
806 ITmfEvent event1
= fExperiment
.getNext(context1
);
807 ITmfContext context2
= fExperiment
.seekEvent(location
);
808 ITmfEvent event2
= fExperiment
.getNext(context2
);
809 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
813 public void testGetNextEndLocation() {
814 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
815 fExperiment
.getNext(context1
);
816 ITmfLocation location
= context1
.getLocation();
817 ITmfContext context2
= fExperiment
.seekEvent(location
);
818 ITmfEvent event
= fExperiment
.getNext(context2
);
819 assertNull("Event", event
);
822 // ------------------------------------------------------------------------
824 // ------------------------------------------------------------------------
827 public void testProcessRequestForNbEvents() throws InterruptedException
{
828 final int nbEvents
= 1000;
829 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
831 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
832 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
833 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
835 public void handleData(final ITmfEvent event
) {
836 super.handleData(event
);
837 requestedEvents
.add(event
);
840 fExperiment
.sendRequest(request
);
841 request
.waitForCompletion();
843 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
844 assertTrue("isCompleted", request
.isCompleted());
845 assertFalse("isCancelled", request
.isCancelled());
847 // Ensure that we have distinct events.
848 // Don't go overboard: we are not validating the stub!
849 for (int i
= 0; i
< nbEvents
; i
++) {
850 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
855 public void testProcessRequestForAllEvents() throws InterruptedException
{
856 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
857 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
858 final long nbExpectedEvents
= NB_EVENTS
;
860 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
861 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
862 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
864 public void handleData(final ITmfEvent event
) {
865 super.handleData(event
);
866 requestedEvents
.add(event
);
869 fExperiment
.sendRequest(request
);
870 request
.waitForCompletion();
872 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
873 assertTrue("isCompleted", request
.isCompleted());
874 assertFalse("isCancelled", request
.isCancelled());
876 // Ensure that we have distinct events.
877 // Don't go overboard: we are not validating the stub!
878 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
879 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
883 // ------------------------------------------------------------------------
885 // ------------------------------------------------------------------------
888 public void testCancel() throws InterruptedException
{
889 final int nbEvents
= NB_EVENTS
;
890 final int limit
= BLOCK_SIZE
;
891 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
893 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
894 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
895 range
, 0, nbEvents
, ExecutionType
.FOREGROUND
) {
899 public void handleData(final ITmfEvent event
) {
900 super.handleData(event
);
901 requestedEvents
.add(event
);
902 if (++nbRead
== limit
) {
908 public void handleCancel() {
909 if (requestedEvents
.size() < limit
) {
910 System
.out
.println("aie");
914 fExperiment
.sendRequest(request
);
915 request
.waitForCompletion();
917 assertEquals("nbEvents", limit
, requestedEvents
.size());
918 assertTrue("isCompleted", request
.isCompleted());
919 assertTrue("isCancelled", request
.isCancelled());