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
.statesystem
.ITmfStateSystem
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
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
.TmfLongLocation
;
48 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
49 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
50 import org
.junit
.Before
;
51 import org
.junit
.Test
;
54 * Test suite for the TmfExperiment class (single trace).
56 @SuppressWarnings("javadoc")
57 public class TmfExperimentTest
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private static final String DIRECTORY
= "testfiles";
64 private static final String TEST_STREAM
= "A-Test-10K";
65 private static final String EXPERIMENT
= "MyExperiment";
66 private static int NB_EVENTS
= 10000;
67 private static int BLOCK_SIZE
= 1000;
69 private static final double DELTA
= 1e-15;
71 private ITmfTrace
[] fTestTraces
;
72 private TmfExperimentStub fExperiment
;
74 private static byte SCALE
= (byte) -3;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
80 private synchronized ITmfTrace
[] setupTrace(final String path
) {
81 if (fTestTraces
== null) {
82 fTestTraces
= new ITmfTrace
[1];
84 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
85 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
86 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true);
87 fTestTraces
[0] = trace
;
88 } catch (final TmfTraceException e
) {
90 } catch (final URISyntaxException e
) {
92 } catch (final IOException e
) {
99 private synchronized void setupExperiment() {
100 if (fExperiment
== null) {
101 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
102 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
107 public void setUp() {
108 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
117 public void testSimpleTmfExperimentConstructor() {
118 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
);
119 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
120 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
121 experiment
.dispose();
123 experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
124 experiment
.dispose();
128 public void testNormalTmfExperimentConstructor() {
129 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
130 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
132 final long nbExperimentEvents
= fExperiment
.getNbEvents();
133 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
135 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
136 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
138 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
139 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
140 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 public void testGetTimestamp() {
149 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
150 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
151 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
152 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
153 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
154 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
155 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
156 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
157 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
160 // ------------------------------------------------------------------------
161 // Bookmarks file handling
162 // ------------------------------------------------------------------------
165 public void testBookmarks() {
166 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
167 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
168 fExperiment
.setBookmarksFile(bookmarks
);
169 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
172 // ------------------------------------------------------------------------
173 // State system and statistics methods
174 // ------------------------------------------------------------------------
177 public void testGetStatistics() {
178 /* There should not be any experiment-specific statistics */
179 ITmfStatistics stats
= fExperiment
.getStatistics();
184 public void testGetStateSystem() {
185 /* There should not be any experiment-specific state system */
186 ITmfStateSystem ss
= fExperiment
.getStateSystems().get("something");
190 // ------------------------------------------------------------------------
191 // seekEvent by location
192 // ------------------------------------------------------------------------
195 public void testSeekBadLocation() {
196 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
197 assertNull("seekEvent", context
);
201 public void testSeekNoTrace() {
202 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
203 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
204 assertNull("seekEvent", context
);
205 experiment
.dispose();
208 // ------------------------------------------------------------------------
209 // seekEvent on ratio
210 // ------------------------------------------------------------------------
213 public void testSeekEventOnRatio() {
215 ITmfContext context
= fExperiment
.seekEvent(0.0);
216 assertEquals("Context rank", 0, context
.getRank());
217 ITmfEvent event
= fExperiment
.parseEvent(context
);
218 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
219 assertEquals("Context rank", 0, context
.getRank());
222 int midTrace
= NB_EVENTS
/ 2;
223 context
= fExperiment
.seekEvent(0.5);
224 assertEquals("Context rank", midTrace
, context
.getRank());
225 event
= fExperiment
.parseEvent(context
);
226 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
227 assertEquals("Context rank", midTrace
, context
.getRank());
230 context
= fExperiment
.seekEvent(1.0);
231 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
232 event
= fExperiment
.parseEvent(context
);
233 assertNull("Event timestamp", event
);
234 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
237 context
= fExperiment
.seekEvent(1.1);
238 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
239 event
= fExperiment
.parseEvent(context
);
240 assertNull("Event timestamp", event
);
241 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
244 context
= fExperiment
.seekEvent(-0.5);
245 assertEquals("Context rank", 0, context
.getRank());
246 event
= fExperiment
.parseEvent(context
);
247 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
248 assertEquals("Context rank", 0, context
.getRank());
252 public void testGetLocationRatio() {
254 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
255 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
256 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
259 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
260 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
261 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
264 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
265 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
266 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
269 // @SuppressWarnings("rawtypes")
270 // public void testGetCurrentLocation() {
271 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
272 // ITmfLocation location = fExperiment.getCurrentLocation();
273 // assertEquals("getCurrentLocation", location, context.getLocation());
276 // ------------------------------------------------------------------------
278 // ------------------------------------------------------------------------
281 public void testSeekRankOnCacheBoundary() {
282 long cacheSize
= fExperiment
.getCacheSize();
284 // On lower bound, returns the first event (TS = 1)
285 ITmfContext context
= fExperiment
.seekEvent(0);
286 assertEquals("Context rank", 0, context
.getRank());
288 ITmfEvent event
= fExperiment
.getNext(context
);
289 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
290 assertEquals("Context rank", 1, context
.getRank());
292 // Position trace at event rank [cacheSize]
293 context
= fExperiment
.seekEvent(cacheSize
);
294 assertEquals("Context rank", cacheSize
, context
.getRank());
296 event
= fExperiment
.getNext(context
);
297 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
298 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
300 // Position trace at event rank [4 * cacheSize]
301 context
= fExperiment
.seekEvent(4 * cacheSize
);
302 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
304 event
= fExperiment
.getNext(context
);
305 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
306 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
310 public void testSeekRankNotOnCacheBoundary() {
311 long cacheSize
= fExperiment
.getCacheSize();
313 // Position trace at event rank 9
314 ITmfContext context
= fExperiment
.seekEvent(9);
315 assertEquals("Context rank", 9, context
.getRank());
317 ITmfEvent event
= fExperiment
.getNext(context
);
318 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
319 assertEquals("Context rank", 10, context
.getRank());
321 // Position trace at event rank [cacheSize - 1]
322 context
= fExperiment
.seekEvent(cacheSize
- 1);
323 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
325 event
= fExperiment
.getNext(context
);
326 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
327 assertEquals("Context rank", cacheSize
, context
.getRank());
329 // Position trace at event rank [cacheSize + 1]
330 context
= fExperiment
.seekEvent(cacheSize
+ 1);
331 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
333 event
= fExperiment
.getNext(context
);
334 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
335 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
337 // Position trace at event rank 4500
338 context
= fExperiment
.seekEvent(4500);
339 assertEquals("Context rank", 4500, context
.getRank());
341 event
= fExperiment
.getNext(context
);
342 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
343 assertEquals("Context rank", 4501, context
.getRank());
347 public void testSeekRankOutOfScope() {
348 // Position trace at beginning
349 ITmfContext context
= fExperiment
.seekEvent(-1);
350 assertEquals("Event rank", 0, context
.getRank());
352 ITmfEvent event
= fExperiment
.getNext(context
);
353 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
354 assertEquals("Context rank", 1, context
.getRank());
356 // Position trace at event passed the end
357 context
= fExperiment
.seekEvent(NB_EVENTS
);
358 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
360 event
= fExperiment
.getNext(context
);
361 assertNull("Event", event
);
362 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
365 // ------------------------------------------------------------------------
366 // seekEvent on timestamp
367 // ------------------------------------------------------------------------
370 public void testSeekTimestampOnCacheBoundary() {
371 long cacheSize
= fExperiment
.getCacheSize();
373 // Position trace at event rank 0
374 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
375 assertEquals("Context rank", 0, context
.getRank());
377 ITmfEvent event
= fExperiment
.getNext(context
);
378 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
379 assertEquals("Context rank", 1, context
.getRank());
381 // Position trace at event rank [cacheSize]
382 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
383 assertEquals("Event rank", cacheSize
, context
.getRank());
385 event
= fExperiment
.getNext(context
);
386 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
387 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
389 // Position trace at event rank [4 * cacheSize]
390 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
391 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
393 event
= fExperiment
.getNext(context
);
394 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
395 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
399 public void testSeekTimestampNotOnCacheBoundary() {
400 // Position trace at event rank 1 (TS = 2)
401 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
402 assertEquals("Context rank", 1, context
.getRank());
404 ITmfEvent event
= fExperiment
.getNext(context
);
405 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
406 assertEquals("Context rank", 2, context
.getRank());
408 // Position trace at event rank 9 (TS = 10)
409 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
410 assertEquals("Context rank", 9, context
.getRank());
412 event
= fExperiment
.getNext(context
);
413 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
414 assertEquals("Context rank", 10, context
.getRank());
416 // Position trace at event rank 999 (TS = 1000)
417 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
418 assertEquals("Context rank", 999, context
.getRank());
420 event
= fExperiment
.getNext(context
);
421 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
422 assertEquals("Context rank", 1000, context
.getRank());
424 // Position trace at event rank 1001 (TS = 1002)
425 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
426 assertEquals("Context rank", 1001, context
.getRank());
428 event
= fExperiment
.getNext(context
);
429 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
430 assertEquals("Context rank", 1002, context
.getRank());
432 // Position trace at event rank 4500 (TS = 4501)
433 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
434 assertEquals("Context rank", 4500, context
.getRank());
436 event
= fExperiment
.getNext(context
);
437 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
438 assertEquals("Context rank", 4501, context
.getRank());
442 public void testSeekTimestampOutOfScope() {
443 // Position trace at beginning
444 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
445 assertEquals("Event rank", 0, context
.getRank());
447 ITmfEvent event
= fExperiment
.getNext(context
);
448 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
449 assertEquals("Event rank", 1, context
.getRank());
451 // Position trace at event passed the end
452 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
453 event
= fExperiment
.getNext(context
);
454 assertNull("Event location", event
);
455 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
458 // ------------------------------------------------------------------------
459 // seekEvent by location (context rank is undefined)
460 // ------------------------------------------------------------------------
463 public void testSeekLocationOnCacheBoundary() {
464 long cacheSize
= fExperiment
.getCacheSize();
466 // Position trace at event rank 0
467 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
468 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
470 ITmfEvent event
= fExperiment
.getNext(context
);
471 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
473 event
= fExperiment
.getNext(context
);
474 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
476 // Position trace at event rank 'cacheSize'
477 tmpContext
= fExperiment
.seekEvent(cacheSize
);
478 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
480 event
= fExperiment
.getNext(context
);
481 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
483 event
= fExperiment
.getNext(context
);
484 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
486 // Position trace at event rank 4 * 'cacheSize'
487 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
488 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
490 event
= fExperiment
.getNext(context
);
491 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
493 event
= fExperiment
.getNext(context
);
494 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
498 public void testSeekLocationNotOnCacheBoundary() {
499 long cacheSize
= fExperiment
.getCacheSize();
501 // Position trace at event 'cacheSize' - 1
502 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
503 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
505 ITmfEvent event
= fExperiment
.getNext(context
);
506 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
508 event
= fExperiment
.getNext(context
);
509 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
511 // Position trace at event rank 2 * 'cacheSize' - 1
512 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
513 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
514 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
516 event
= fExperiment
.getNext(context
);
517 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
519 event
= fExperiment
.getNext(context
);
520 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
522 // Position trace at event rank 4500
523 tmpContext
= fExperiment
.seekEvent(4500);
524 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
526 event
= fExperiment
.getNext(context
);
527 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
529 event
= fExperiment
.getNext(context
);
530 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
534 public void testSeekLocationOutOfScope() {
535 // Position trace at beginning
536 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
538 ITmfEvent event
= fExperiment
.getNext(context
);
539 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
542 // ------------------------------------------------------------------------
543 // getNext - updates the context
544 // ------------------------------------------------------------------------
546 private static void validateContextRanks(ITmfContext context
) {
547 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
548 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
550 int nbTraces
= ctx
.getContexts().length
;
552 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
553 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
554 for (int i
= 0; i
< nbTraces
; i
++) {
555 long rank
= ctx
.getContexts()[i
].getRank();
562 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
566 public void testGetNextAfteSeekingOnTS_1() {
568 final long INITIAL_TS
= 1;
569 final int NB_READS
= 20;
571 // On lower bound, returns the first event (ts = 1)
572 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
574 validateContextRanks(context
);
578 for (int i
= 0; i
< NB_READS
; i
++) {
579 event
= fExperiment
.getNext(context
);
580 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
581 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
584 // Make sure we stay positioned
585 event
= fExperiment
.parseEvent(context
);
586 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
587 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
589 validateContextRanks(context
);
593 public void testGetNextAfteSeekingOnTS_2() {
594 final long INITIAL_TS
= 2;
595 final int NB_READS
= 20;
597 // On lower bound, returns the first event (ts = 2)
598 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
600 validateContextRanks(context
);
604 for (int i
= 0; i
< NB_READS
; i
++) {
605 event
= fExperiment
.getNext(context
);
606 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
607 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
610 // Make sure we stay positioned
611 event
= fExperiment
.parseEvent(context
);
612 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
613 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
615 validateContextRanks(context
);
619 public void testGetNextAfteSeekingOnTS_3() {
621 final long INITIAL_TS
= 500;
622 final int NB_READS
= 20;
624 // On lower bound, returns the first event (ts = 500)
625 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
627 validateContextRanks(context
);
631 for (int i
= 0; i
< NB_READS
; i
++) {
632 event
= fExperiment
.getNext(context
);
633 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
634 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
637 // Make sure we stay positioned
638 event
= fExperiment
.parseEvent(context
);
639 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
640 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
642 validateContextRanks(context
);
646 public void testGetNextAfterSeekingOnRank_1() {
647 final long INITIAL_RANK
= 0L;
648 final int NB_READS
= 20;
650 // On lower bound, returns the first event (rank = 0)
651 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
653 validateContextRanks(context
);
657 for (int i
= 0; i
< NB_READS
; i
++) {
658 event
= fExperiment
.getNext(context
);
659 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
660 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
663 // Make sure we stay positioned
664 event
= fExperiment
.parseEvent(context
);
665 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
666 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
668 validateContextRanks(context
);
672 public void testGetNextAfterSeekingOnRank_2() {
673 final long INITIAL_RANK
= 1L;
674 final int NB_READS
= 20;
676 // On lower bound, returns the first event (rank = 0)
677 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
679 validateContextRanks(context
);
683 for (int i
= 0; i
< NB_READS
; i
++) {
684 event
= fExperiment
.getNext(context
);
685 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
686 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
689 // Make sure we stay positioned
690 event
= fExperiment
.parseEvent(context
);
691 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
692 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
694 validateContextRanks(context
);
698 public void testGetNextAfterSeekingOnRank_3() {
699 final long INITIAL_RANK
= 500L;
700 final int NB_READS
= 20;
702 // On lower bound, returns the first event (rank = 0)
703 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
705 validateContextRanks(context
);
709 for (int i
= 0; i
< NB_READS
; i
++) {
710 event
= fExperiment
.getNext(context
);
711 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
712 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
715 // Make sure we stay positioned
716 event
= fExperiment
.parseEvent(context
);
717 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
718 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
720 validateContextRanks(context
);
724 public void testGetNextAfterSeekingOnLocation_1() {
725 final ITmfLocation INITIAL_LOC
= null;
726 final long INITIAL_TS
= 1;
727 final int NB_READS
= 20;
729 // On lower bound, returns the first event (ts = 1)
730 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
732 validateContextRanks(context
);
736 for (int i
= 0; i
< NB_READS
; i
++) {
737 event
= fExperiment
.getNext(context
);
738 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
739 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
742 // Make sure we stay positioned
743 event
= fExperiment
.parseEvent(context
);
744 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
745 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
747 validateContextRanks(context
);
751 public void testGetNextAfterSeekingOnLocation_2() {
752 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
753 final long INITIAL_TS
= 2;
754 final int NB_READS
= 20;
756 // On lower bound, returns the first event (ts = 2)
757 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
759 validateContextRanks(context
);
763 for (int i
= 0; i
< NB_READS
; i
++) {
764 event
= fExperiment
.getNext(context
);
765 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
768 // Make sure we stay positioned
769 event
= fExperiment
.parseEvent(context
);
770 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
772 validateContextRanks(context
);
776 public void testGetNextAfterSeekingOnLocation_3() {
777 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
778 final long INITIAL_TS
= 501;
779 final int NB_READS
= 20;
781 // On lower bound, returns the first event (ts = 501)
782 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
784 validateContextRanks(context
);
788 for (int i
= 0; i
< NB_READS
; i
++) {
789 event
= fExperiment
.getNext(context
);
790 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
793 // Make sure we stay positioned
794 event
= fExperiment
.parseEvent(context
);
795 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
797 validateContextRanks(context
);
801 public void testGetNextLocation() {
802 ITmfContext context1
= fExperiment
.seekEvent(0);
803 fExperiment
.getNext(context1
);
804 ITmfLocation location
= context1
.getLocation();
805 ITmfEvent event1
= fExperiment
.getNext(context1
);
806 ITmfContext context2
= fExperiment
.seekEvent(location
);
807 ITmfEvent event2
= fExperiment
.getNext(context2
);
808 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
812 public void testGetNextEndLocation() {
813 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
814 fExperiment
.getNext(context1
);
815 ITmfLocation location
= context1
.getLocation();
816 ITmfContext context2
= fExperiment
.seekEvent(location
);
817 ITmfEvent event
= fExperiment
.getNext(context2
);
818 assertNull("Event", event
);
821 // ------------------------------------------------------------------------
823 // ------------------------------------------------------------------------
826 public void testProcessRequestForNbEvents() throws InterruptedException
{
827 final int blockSize
= 100;
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, range
, nbEvents
, blockSize
) {
834 public void handleData(final ITmfEvent event
) {
835 super.handleData(event
);
836 requestedEvents
.add(event
);
839 fExperiment
.sendRequest(request
);
840 request
.waitForCompletion();
842 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
843 assertTrue("isCompleted", request
.isCompleted());
844 assertFalse("isCancelled", request
.isCancelled());
846 // Ensure that we have distinct events.
847 // Don't go overboard: we are not validating the stub!
848 for (int i
= 0; i
< nbEvents
; i
++) {
849 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
854 public void testProcessRequestForNbEvents2() throws InterruptedException
{
855 final int blockSize
= 2 * NB_EVENTS
;
856 final int nbEvents
= 1000;
857 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
859 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
860 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
862 public void handleData(final ITmfEvent event
) {
863 super.handleData(event
);
864 requestedEvents
.add(event
);
867 fExperiment
.sendRequest(request
);
868 request
.waitForCompletion();
870 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
871 assertTrue("isCompleted", request
.isCompleted());
872 assertFalse("isCancelled", request
.isCancelled());
874 // Ensure that we have distinct events.
875 // Don't go overboard: we are not validating the stub!
876 for (int i
= 0; i
< nbEvents
; i
++) {
877 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
882 public void testProcessRequestForAllEvents() throws InterruptedException
{
883 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
884 final int blockSize
= 1;
885 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
886 final long nbExpectedEvents
= NB_EVENTS
;
888 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
889 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
891 public void handleData(final ITmfEvent event
) {
892 super.handleData(event
);
893 requestedEvents
.add(event
);
896 fExperiment
.sendRequest(request
);
897 request
.waitForCompletion();
899 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
900 assertTrue("isCompleted", request
.isCompleted());
901 assertFalse("isCancelled", request
.isCancelled());
903 // Ensure that we have distinct events.
904 // Don't go overboard: we are not validating the stub!
905 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
906 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
910 // ------------------------------------------------------------------------
912 // ------------------------------------------------------------------------
915 public void testCancel() throws InterruptedException
{
916 final int nbEvents
= NB_EVENTS
;
917 final int blockSize
= BLOCK_SIZE
;
918 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
920 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
921 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
924 public void handleData(final ITmfEvent event
) {
925 super.handleData(event
);
926 requestedEvents
.add(event
);
927 if (++nbRead
== blockSize
) {
932 public void handleCancel() {
933 if (requestedEvents
.size() < blockSize
) {
934 System
.out
.println("aie");
938 fExperiment
.sendRequest(request
);
939 request
.waitForCompletion();
941 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
942 assertTrue("isCompleted", request
.isCompleted());
943 assertTrue("isCancelled", request
.isCancelled());