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
.assertNotNull
;
21 import static org
.junit
.Assert
.assertNull
;
22 import static org
.junit
.Assert
.assertTrue
;
25 import java
.io
.IOException
;
26 import java
.net
.URISyntaxException
;
28 import java
.util
.Collection
;
29 import java
.util
.Vector
;
31 import org
.eclipse
.core
.resources
.IFile
;
32 import org
.eclipse
.core
.runtime
.FileLocator
;
33 import org
.eclipse
.core
.runtime
.Path
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
50 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
51 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
52 import org
.junit
.Before
;
53 import org
.junit
.Test
;
56 * Test suite for the TmfExperiment class (single trace).
58 @SuppressWarnings("javadoc")
59 public class TmfExperimentTest
{
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 private static final String DIRECTORY
= "testfiles";
66 private static final String TEST_STREAM
= "A-Test-10K";
67 private static final String EXPERIMENT
= "MyExperiment";
68 private static int NB_EVENTS
= 10000;
69 private static int BLOCK_SIZE
= 1000;
71 private static final double DELTA
= 1e-15;
73 private ITmfTrace
[] fTestTraces
;
74 private TmfExperimentStub fExperiment
;
76 private static byte SCALE
= (byte) -3;
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
82 private synchronized ITmfTrace
[] setupTrace(final String path
) {
83 if (fTestTraces
== null) {
84 fTestTraces
= new ITmfTrace
[1];
86 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
87 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
88 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true);
89 fTestTraces
[0] = trace
;
90 } catch (final TmfTraceException e
) {
92 } catch (final URISyntaxException e
) {
94 } catch (final IOException e
) {
101 private synchronized void setupExperiment() {
102 if (fExperiment
== null) {
103 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
104 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
109 public void setUp() {
110 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
114 // ------------------------------------------------------------------------
116 // ------------------------------------------------------------------------
119 public void testSimpleTmfExperimentConstructor() {
120 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
);
121 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
122 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
123 experiment
.dispose();
125 experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
126 experiment
.dispose();
130 public void testNormalTmfExperimentConstructor() {
131 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
132 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
134 final long nbExperimentEvents
= fExperiment
.getNbEvents();
135 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
137 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
138 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
140 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
141 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
142 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
150 public void testGetTimestamp() {
151 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
152 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
153 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
154 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
155 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
156 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
157 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
158 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
159 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
162 // ------------------------------------------------------------------------
163 // Bookmarks file handling
164 // ------------------------------------------------------------------------
167 public void testBookmarks() {
168 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
169 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
170 fExperiment
.setBookmarksFile(bookmarks
);
171 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
174 // ------------------------------------------------------------------------
175 // State system and statistics methods
176 // ------------------------------------------------------------------------
179 public void testGetStatistics() {
180 /* There should not be any experiment-specific statistics */
181 ITmfStatistics stats
= fExperiment
.getStatistics();
186 public void testGetStateSystem() {
187 /* There should not be any experiment-specific state system */
188 ITmfStateSystem ss
= fExperiment
.getStateSystem("something");
193 public void testListStateSystem() {
194 Collection
<String
> sss
= fExperiment
.listStateSystems();
196 assertEquals(0, sss
.size());
199 // ------------------------------------------------------------------------
200 // seekEvent by location
201 // ------------------------------------------------------------------------
204 public void testSeekBadLocation() {
205 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
206 assertNull("seekEvent", context
);
210 public void testSeekNoTrace() {
211 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
212 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
213 assertNull("seekEvent", context
);
214 experiment
.dispose();
217 // ------------------------------------------------------------------------
218 // seekEvent on ratio
219 // ------------------------------------------------------------------------
222 public void testSeekEventOnRatio() {
224 ITmfContext context
= fExperiment
.seekEvent(0.0);
225 assertEquals("Context rank", 0, context
.getRank());
226 ITmfEvent event
= fExperiment
.parseEvent(context
);
227 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
228 assertEquals("Context rank", 0, context
.getRank());
231 int midTrace
= NB_EVENTS
/ 2;
232 context
= fExperiment
.seekEvent(0.5);
233 assertEquals("Context rank", midTrace
, context
.getRank());
234 event
= fExperiment
.parseEvent(context
);
235 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
236 assertEquals("Context rank", midTrace
, context
.getRank());
239 context
= fExperiment
.seekEvent(1.0);
240 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
241 event
= fExperiment
.parseEvent(context
);
242 assertNull("Event timestamp", event
);
243 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
246 context
= fExperiment
.seekEvent(1.1);
247 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
248 event
= fExperiment
.parseEvent(context
);
249 assertNull("Event timestamp", event
);
250 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
253 context
= fExperiment
.seekEvent(-0.5);
254 assertEquals("Context rank", 0, context
.getRank());
255 event
= fExperiment
.parseEvent(context
);
256 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
257 assertEquals("Context rank", 0, context
.getRank());
261 public void testGetLocationRatio() {
263 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
264 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
265 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
268 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
269 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
270 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
273 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
274 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
275 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
278 // @SuppressWarnings("rawtypes")
279 // public void testGetCurrentLocation() {
280 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
281 // ITmfLocation location = fExperiment.getCurrentLocation();
282 // assertEquals("getCurrentLocation", location, context.getLocation());
285 // ------------------------------------------------------------------------
287 // ------------------------------------------------------------------------
290 public void testSeekRankOnCacheBoundary() {
291 long cacheSize
= fExperiment
.getCacheSize();
293 // On lower bound, returns the first event (TS = 1)
294 ITmfContext context
= fExperiment
.seekEvent(0);
295 assertEquals("Context rank", 0, context
.getRank());
297 ITmfEvent event
= fExperiment
.getNext(context
);
298 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
299 assertEquals("Context rank", 1, context
.getRank());
301 // Position trace at event rank [cacheSize]
302 context
= fExperiment
.seekEvent(cacheSize
);
303 assertEquals("Context rank", cacheSize
, context
.getRank());
305 event
= fExperiment
.getNext(context
);
306 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
307 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
309 // Position trace at event rank [4 * cacheSize]
310 context
= fExperiment
.seekEvent(4 * cacheSize
);
311 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
313 event
= fExperiment
.getNext(context
);
314 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
315 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
319 public void testSeekRankNotOnCacheBoundary() {
320 long cacheSize
= fExperiment
.getCacheSize();
322 // Position trace at event rank 9
323 ITmfContext context
= fExperiment
.seekEvent(9);
324 assertEquals("Context rank", 9, context
.getRank());
326 ITmfEvent event
= fExperiment
.getNext(context
);
327 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
328 assertEquals("Context rank", 10, 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
, event
.getTimestamp().getValue());
336 assertEquals("Context rank", cacheSize
, context
.getRank());
338 // Position trace at event rank [cacheSize + 1]
339 context
= fExperiment
.seekEvent(cacheSize
+ 1);
340 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
342 event
= fExperiment
.getNext(context
);
343 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
344 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
346 // Position trace at event rank 4500
347 context
= fExperiment
.seekEvent(4500);
348 assertEquals("Context rank", 4500, context
.getRank());
350 event
= fExperiment
.getNext(context
);
351 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
352 assertEquals("Context rank", 4501, context
.getRank());
356 public void testSeekRankOutOfScope() {
357 // Position trace at beginning
358 ITmfContext context
= fExperiment
.seekEvent(-1);
359 assertEquals("Event rank", 0, context
.getRank());
361 ITmfEvent event
= fExperiment
.getNext(context
);
362 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
363 assertEquals("Context rank", 1, context
.getRank());
365 // Position trace at event passed the end
366 context
= fExperiment
.seekEvent(NB_EVENTS
);
367 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
369 event
= fExperiment
.getNext(context
);
370 assertNull("Event", event
);
371 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
374 // ------------------------------------------------------------------------
375 // seekEvent on timestamp
376 // ------------------------------------------------------------------------
379 public void testSeekTimestampOnCacheBoundary() {
380 long cacheSize
= fExperiment
.getCacheSize();
382 // Position trace at event rank 0
383 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
384 assertEquals("Context rank", 0, context
.getRank());
386 ITmfEvent event
= fExperiment
.getNext(context
);
387 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
388 assertEquals("Context rank", 1, context
.getRank());
390 // Position trace at event rank [cacheSize]
391 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
392 assertEquals("Event rank", cacheSize
, context
.getRank());
394 event
= fExperiment
.getNext(context
);
395 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
396 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
398 // Position trace at event rank [4 * cacheSize]
399 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
400 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
402 event
= fExperiment
.getNext(context
);
403 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
404 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
408 public void testSeekTimestampNotOnCacheBoundary() {
409 // Position trace at event rank 1 (TS = 2)
410 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
411 assertEquals("Context rank", 1, context
.getRank());
413 ITmfEvent event
= fExperiment
.getNext(context
);
414 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
415 assertEquals("Context rank", 2, context
.getRank());
417 // Position trace at event rank 9 (TS = 10)
418 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
419 assertEquals("Context rank", 9, context
.getRank());
421 event
= fExperiment
.getNext(context
);
422 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
423 assertEquals("Context rank", 10, context
.getRank());
425 // Position trace at event rank 999 (TS = 1000)
426 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
427 assertEquals("Context rank", 999, context
.getRank());
429 event
= fExperiment
.getNext(context
);
430 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
431 assertEquals("Context rank", 1000, context
.getRank());
433 // Position trace at event rank 1001 (TS = 1002)
434 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
435 assertEquals("Context rank", 1001, context
.getRank());
437 event
= fExperiment
.getNext(context
);
438 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
439 assertEquals("Context rank", 1002, context
.getRank());
441 // Position trace at event rank 4500 (TS = 4501)
442 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
443 assertEquals("Context rank", 4500, context
.getRank());
445 event
= fExperiment
.getNext(context
);
446 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
447 assertEquals("Context rank", 4501, context
.getRank());
451 public void testSeekTimestampOutOfScope() {
452 // Position trace at beginning
453 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
454 assertEquals("Event rank", 0, context
.getRank());
456 ITmfEvent event
= fExperiment
.getNext(context
);
457 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
458 assertEquals("Event rank", 1, context
.getRank());
460 // Position trace at event passed the end
461 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
462 event
= fExperiment
.getNext(context
);
463 assertNull("Event location", event
);
464 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
467 // ------------------------------------------------------------------------
468 // seekEvent by location (context rank is undefined)
469 // ------------------------------------------------------------------------
472 public void testSeekLocationOnCacheBoundary() {
473 long cacheSize
= fExperiment
.getCacheSize();
475 // Position trace at event rank 0
476 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
477 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
479 ITmfEvent event
= fExperiment
.getNext(context
);
480 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
482 event
= fExperiment
.getNext(context
);
483 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
485 // Position trace at event rank 'cacheSize'
486 tmpContext
= fExperiment
.seekEvent(cacheSize
);
487 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
489 event
= fExperiment
.getNext(context
);
490 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
492 event
= fExperiment
.getNext(context
);
493 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
495 // Position trace at event rank 4 * 'cacheSize'
496 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
497 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
499 event
= fExperiment
.getNext(context
);
500 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
502 event
= fExperiment
.getNext(context
);
503 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
507 public void testSeekLocationNotOnCacheBoundary() {
508 long cacheSize
= fExperiment
.getCacheSize();
510 // Position trace at event 'cacheSize' - 1
511 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
512 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
514 ITmfEvent event
= fExperiment
.getNext(context
);
515 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
517 event
= fExperiment
.getNext(context
);
518 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
520 // Position trace at event rank 2 * 'cacheSize' - 1
521 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
522 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
523 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
525 event
= fExperiment
.getNext(context
);
526 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
528 event
= fExperiment
.getNext(context
);
529 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
531 // Position trace at event rank 4500
532 tmpContext
= fExperiment
.seekEvent(4500);
533 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
535 event
= fExperiment
.getNext(context
);
536 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
538 event
= fExperiment
.getNext(context
);
539 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
543 public void testSeekLocationOutOfScope() {
544 // Position trace at beginning
545 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
547 ITmfEvent event
= fExperiment
.getNext(context
);
548 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
551 // ------------------------------------------------------------------------
552 // getNext - updates the context
553 // ------------------------------------------------------------------------
555 private static void validateContextRanks(ITmfContext context
) {
556 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
557 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
559 int nbTraces
= ctx
.getContexts().length
;
561 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
562 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
563 for (int i
= 0; i
< nbTraces
; i
++) {
564 long rank
= ctx
.getContexts()[i
].getRank();
571 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
575 public void testGetNextAfteSeekingOnTS_1() {
577 final long INITIAL_TS
= 1;
578 final int NB_READS
= 20;
580 // On lower bound, returns the first event (ts = 1)
581 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
583 validateContextRanks(context
);
587 for (int i
= 0; i
< NB_READS
; i
++) {
588 event
= fExperiment
.getNext(context
);
589 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
590 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
593 // Make sure we stay positioned
594 event
= fExperiment
.parseEvent(context
);
595 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
596 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
598 validateContextRanks(context
);
602 public void testGetNextAfteSeekingOnTS_2() {
603 final long INITIAL_TS
= 2;
604 final int NB_READS
= 20;
606 // On lower bound, returns the first event (ts = 2)
607 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
609 validateContextRanks(context
);
613 for (int i
= 0; i
< NB_READS
; i
++) {
614 event
= fExperiment
.getNext(context
);
615 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
616 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
619 // Make sure we stay positioned
620 event
= fExperiment
.parseEvent(context
);
621 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
622 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
624 validateContextRanks(context
);
628 public void testGetNextAfteSeekingOnTS_3() {
630 final long INITIAL_TS
= 500;
631 final int NB_READS
= 20;
633 // On lower bound, returns the first event (ts = 500)
634 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
636 validateContextRanks(context
);
640 for (int i
= 0; i
< NB_READS
; i
++) {
641 event
= fExperiment
.getNext(context
);
642 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
643 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
646 // Make sure we stay positioned
647 event
= fExperiment
.parseEvent(context
);
648 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
649 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
651 validateContextRanks(context
);
655 public void testGetNextAfterSeekingOnRank_1() {
656 final long INITIAL_RANK
= 0L;
657 final int NB_READS
= 20;
659 // On lower bound, returns the first event (rank = 0)
660 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
662 validateContextRanks(context
);
666 for (int i
= 0; i
< NB_READS
; i
++) {
667 event
= fExperiment
.getNext(context
);
668 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
669 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
672 // Make sure we stay positioned
673 event
= fExperiment
.parseEvent(context
);
674 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
675 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
677 validateContextRanks(context
);
681 public void testGetNextAfterSeekingOnRank_2() {
682 final long INITIAL_RANK
= 1L;
683 final int NB_READS
= 20;
685 // On lower bound, returns the first event (rank = 0)
686 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
688 validateContextRanks(context
);
692 for (int i
= 0; i
< NB_READS
; i
++) {
693 event
= fExperiment
.getNext(context
);
694 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
695 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
698 // Make sure we stay positioned
699 event
= fExperiment
.parseEvent(context
);
700 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
701 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
703 validateContextRanks(context
);
707 public void testGetNextAfterSeekingOnRank_3() {
708 final long INITIAL_RANK
= 500L;
709 final int NB_READS
= 20;
711 // On lower bound, returns the first event (rank = 0)
712 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
714 validateContextRanks(context
);
718 for (int i
= 0; i
< NB_READS
; i
++) {
719 event
= fExperiment
.getNext(context
);
720 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
721 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
724 // Make sure we stay positioned
725 event
= fExperiment
.parseEvent(context
);
726 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
727 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
729 validateContextRanks(context
);
733 public void testGetNextAfterSeekingOnLocation_1() {
734 final ITmfLocation INITIAL_LOC
= null;
735 final long INITIAL_TS
= 1;
736 final int NB_READS
= 20;
738 // On lower bound, returns the first event (ts = 1)
739 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
741 validateContextRanks(context
);
745 for (int i
= 0; i
< NB_READS
; i
++) {
746 event
= fExperiment
.getNext(context
);
747 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
748 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
751 // Make sure we stay positioned
752 event
= fExperiment
.parseEvent(context
);
753 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
754 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
756 validateContextRanks(context
);
760 public void testGetNextAfterSeekingOnLocation_2() {
761 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
762 final long INITIAL_TS
= 2;
763 final int NB_READS
= 20;
765 // On lower bound, returns the first event (ts = 2)
766 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
768 validateContextRanks(context
);
772 for (int i
= 0; i
< NB_READS
; i
++) {
773 event
= fExperiment
.getNext(context
);
774 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
777 // Make sure we stay positioned
778 event
= fExperiment
.parseEvent(context
);
779 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
781 validateContextRanks(context
);
785 public void testGetNextAfterSeekingOnLocation_3() {
786 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
787 final long INITIAL_TS
= 501;
788 final int NB_READS
= 20;
790 // On lower bound, returns the first event (ts = 501)
791 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
793 validateContextRanks(context
);
797 for (int i
= 0; i
< NB_READS
; i
++) {
798 event
= fExperiment
.getNext(context
);
799 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
802 // Make sure we stay positioned
803 event
= fExperiment
.parseEvent(context
);
804 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
806 validateContextRanks(context
);
810 public void testGetNextLocation() {
811 ITmfContext context1
= fExperiment
.seekEvent(0);
812 fExperiment
.getNext(context1
);
813 ITmfLocation location
= context1
.getLocation();
814 ITmfEvent event1
= fExperiment
.getNext(context1
);
815 ITmfContext context2
= fExperiment
.seekEvent(location
);
816 ITmfEvent event2
= fExperiment
.getNext(context2
);
817 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
821 public void testGetNextEndLocation() {
822 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
823 fExperiment
.getNext(context1
);
824 ITmfLocation location
= context1
.getLocation();
825 ITmfContext context2
= fExperiment
.seekEvent(location
);
826 ITmfEvent event
= fExperiment
.getNext(context2
);
827 assertNull("Event", event
);
830 // ------------------------------------------------------------------------
832 // ------------------------------------------------------------------------
835 public void testProcessRequestForNbEvents() throws InterruptedException
{
836 final int blockSize
= 100;
837 final int nbEvents
= 1000;
838 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
840 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
841 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
843 public void handleData(final ITmfEvent event
) {
844 super.handleData(event
);
845 requestedEvents
.add(event
);
848 fExperiment
.sendRequest(request
);
849 request
.waitForCompletion();
851 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
852 assertTrue("isCompleted", request
.isCompleted());
853 assertFalse("isCancelled", request
.isCancelled());
855 // Ensure that we have distinct events.
856 // Don't go overboard: we are not validating the stub!
857 for (int i
= 0; i
< nbEvents
; i
++) {
858 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
863 public void testProcessRequestForNbEvents2() throws InterruptedException
{
864 final int blockSize
= 2 * NB_EVENTS
;
865 final int nbEvents
= 1000;
866 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
868 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
869 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
871 public void handleData(final ITmfEvent event
) {
872 super.handleData(event
);
873 requestedEvents
.add(event
);
876 fExperiment
.sendRequest(request
);
877 request
.waitForCompletion();
879 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
880 assertTrue("isCompleted", request
.isCompleted());
881 assertFalse("isCancelled", request
.isCancelled());
883 // Ensure that we have distinct events.
884 // Don't go overboard: we are not validating the stub!
885 for (int i
= 0; i
< nbEvents
; i
++) {
886 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
891 public void testProcessRequestForAllEvents() throws InterruptedException
{
892 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
893 final int blockSize
= 1;
894 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
895 final long nbExpectedEvents
= NB_EVENTS
;
897 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
898 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
900 public void handleData(final ITmfEvent event
) {
901 super.handleData(event
);
902 requestedEvents
.add(event
);
905 fExperiment
.sendRequest(request
);
906 request
.waitForCompletion();
908 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
909 assertTrue("isCompleted", request
.isCompleted());
910 assertFalse("isCancelled", request
.isCancelled());
912 // Ensure that we have distinct events.
913 // Don't go overboard: we are not validating the stub!
914 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
915 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
919 // ------------------------------------------------------------------------
921 // ------------------------------------------------------------------------
924 public void testCancel() throws InterruptedException
{
925 final int nbEvents
= NB_EVENTS
;
926 final int blockSize
= BLOCK_SIZE
;
927 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
929 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
930 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
933 public void handleData(final ITmfEvent event
) {
934 super.handleData(event
);
935 requestedEvents
.add(event
);
936 if (++nbRead
== blockSize
) {
941 public void handleCancel() {
942 if (requestedEvents
.size() < blockSize
) {
943 System
.out
.println("aie");
947 fExperiment
.sendRequest(request
);
948 request
.waitForCompletion();
950 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
951 assertTrue("isCompleted", request
.isCompleted());
952 assertTrue("isCancelled", request
.isCancelled());