1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012, 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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertFalse
;
19 import static org
.junit
.Assert
.assertNotNull
;
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
.Collection
;
28 import java
.util
.Vector
;
30 import org
.eclipse
.core
.resources
.IFile
;
31 import org
.eclipse
.core
.runtime
.FileLocator
;
32 import org
.eclipse
.core
.runtime
.Path
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.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({"nls","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
.getStateSystem("something");
192 public void testListStateSystem() {
193 Collection
<String
> sss
= fExperiment
.listStateSystems();
195 assertEquals(0, sss
.size());
198 // ------------------------------------------------------------------------
199 // seekEvent by location
200 // ------------------------------------------------------------------------
203 public void testSeekBadLocation() {
204 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
205 assertNull("seekEvent", context
);
209 public void testSeekNoTrace() {
210 TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null);
211 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
212 assertNull("seekEvent", context
);
213 experiment
.dispose();
216 // ------------------------------------------------------------------------
217 // seekEvent on ratio
218 // ------------------------------------------------------------------------
221 public void testSeekEventOnRatio() {
223 ITmfContext context
= fExperiment
.seekEvent(0.0);
224 assertEquals("Context rank", 0, context
.getRank());
225 ITmfEvent event
= fExperiment
.parseEvent(context
);
226 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
227 assertEquals("Context rank", 0, context
.getRank());
230 int midTrace
= NB_EVENTS
/ 2;
231 context
= fExperiment
.seekEvent(0.5);
232 assertEquals("Context rank", midTrace
, context
.getRank());
233 event
= fExperiment
.parseEvent(context
);
234 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
235 assertEquals("Context rank", midTrace
, context
.getRank());
238 context
= fExperiment
.seekEvent(1.0);
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(1.1);
246 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
247 event
= fExperiment
.parseEvent(context
);
248 assertNull("Event timestamp", event
);
249 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
252 context
= fExperiment
.seekEvent(-0.5);
253 assertEquals("Context rank", 0, context
.getRank());
254 event
= fExperiment
.parseEvent(context
);
255 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
256 assertEquals("Context rank", 0, context
.getRank());
260 public void testGetLocationRatio() {
262 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
263 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
264 context
= fExperiment
.seekEvent(ratio
);
265 double ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
266 assertEquals("getLocationRatio", ratio
, ratio2
, DELTA
);
269 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
270 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
271 context
= fExperiment
.seekEvent(ratio
);
272 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
273 assertEquals("getLocationRatio", ratio
, ratio2
, DELTA
);
276 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
277 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
278 context
= fExperiment
.seekEvent(ratio
);
279 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
280 assertEquals("getLocationRatio", ratio
, ratio2
, DELTA
);
283 // @SuppressWarnings("rawtypes")
284 // public void testGetCurrentLocation() {
285 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
286 // ITmfLocation location = fExperiment.getCurrentLocation();
287 // assertEquals("getCurrentLocation", location, context.getLocation());
290 // ------------------------------------------------------------------------
292 // ------------------------------------------------------------------------
295 public void testSeekRankOnCacheBoundary() {
296 long cacheSize
= fExperiment
.getCacheSize();
298 // On lower bound, returns the first event (TS = 1)
299 ITmfContext context
= fExperiment
.seekEvent(0);
300 assertEquals("Context rank", 0, context
.getRank());
302 ITmfEvent event
= fExperiment
.getNext(context
);
303 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
304 assertEquals("Context rank", 1, context
.getRank());
306 // Position trace at event rank [cacheSize]
307 context
= fExperiment
.seekEvent(cacheSize
);
308 assertEquals("Context rank", cacheSize
, context
.getRank());
310 event
= fExperiment
.getNext(context
);
311 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
312 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
314 // Position trace at event rank [4 * cacheSize]
315 context
= fExperiment
.seekEvent(4 * cacheSize
);
316 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
318 event
= fExperiment
.getNext(context
);
319 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
320 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
324 public void testSeekRankNotOnCacheBoundary() {
325 long cacheSize
= fExperiment
.getCacheSize();
327 // Position trace at event rank 9
328 ITmfContext context
= fExperiment
.seekEvent(9);
329 assertEquals("Context rank", 9, context
.getRank());
331 ITmfEvent event
= fExperiment
.getNext(context
);
332 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
333 assertEquals("Context rank", 10, context
.getRank());
335 // Position trace at event rank [cacheSize - 1]
336 context
= fExperiment
.seekEvent(cacheSize
- 1);
337 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
339 event
= fExperiment
.getNext(context
);
340 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
341 assertEquals("Context rank", cacheSize
, context
.getRank());
343 // Position trace at event rank [cacheSize + 1]
344 context
= fExperiment
.seekEvent(cacheSize
+ 1);
345 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
347 event
= fExperiment
.getNext(context
);
348 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
349 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
351 // Position trace at event rank 4500
352 context
= fExperiment
.seekEvent(4500);
353 assertEquals("Context rank", 4500, context
.getRank());
355 event
= fExperiment
.getNext(context
);
356 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
357 assertEquals("Context rank", 4501, context
.getRank());
361 public void testSeekRankOutOfScope() {
362 // Position trace at beginning
363 ITmfContext context
= fExperiment
.seekEvent(-1);
364 assertEquals("Event rank", 0, context
.getRank());
366 ITmfEvent event
= fExperiment
.getNext(context
);
367 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
368 assertEquals("Context rank", 1, context
.getRank());
370 // Position trace at event passed the end
371 context
= fExperiment
.seekEvent(NB_EVENTS
);
372 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
374 event
= fExperiment
.getNext(context
);
375 assertNull("Event", event
);
376 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
379 // ------------------------------------------------------------------------
380 // seekEvent on timestamp
381 // ------------------------------------------------------------------------
384 public void testSeekTimestampOnCacheBoundary() {
385 long cacheSize
= fExperiment
.getCacheSize();
387 // Position trace at event rank 0
388 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
389 assertEquals("Context rank", 0, context
.getRank());
391 ITmfEvent event
= fExperiment
.getNext(context
);
392 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
393 assertEquals("Context rank", 1, context
.getRank());
395 // Position trace at event rank [cacheSize]
396 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
397 assertEquals("Event rank", cacheSize
, context
.getRank());
399 event
= fExperiment
.getNext(context
);
400 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
401 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
403 // Position trace at event rank [4 * cacheSize]
404 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
405 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
407 event
= fExperiment
.getNext(context
);
408 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
409 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
413 public void testSeekTimestampNotOnCacheBoundary() {
414 // Position trace at event rank 1 (TS = 2)
415 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
416 assertEquals("Context rank", 1, context
.getRank());
418 ITmfEvent event
= fExperiment
.getNext(context
);
419 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
420 assertEquals("Context rank", 2, context
.getRank());
422 // Position trace at event rank 9 (TS = 10)
423 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
424 assertEquals("Context rank", 9, context
.getRank());
426 event
= fExperiment
.getNext(context
);
427 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
428 assertEquals("Context rank", 10, context
.getRank());
430 // Position trace at event rank 999 (TS = 1000)
431 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
432 assertEquals("Context rank", 999, context
.getRank());
434 event
= fExperiment
.getNext(context
);
435 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
436 assertEquals("Context rank", 1000, context
.getRank());
438 // Position trace at event rank 1001 (TS = 1002)
439 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
440 assertEquals("Context rank", 1001, context
.getRank());
442 event
= fExperiment
.getNext(context
);
443 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
444 assertEquals("Context rank", 1002, context
.getRank());
446 // Position trace at event rank 4500 (TS = 4501)
447 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
448 assertEquals("Context rank", 4500, context
.getRank());
450 event
= fExperiment
.getNext(context
);
451 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
452 assertEquals("Context rank", 4501, context
.getRank());
456 public void testSeekTimestampOutOfScope() {
457 // Position trace at beginning
458 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
459 assertEquals("Event rank", 0, context
.getRank());
461 ITmfEvent event
= fExperiment
.getNext(context
);
462 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
463 assertEquals("Event rank", 1, context
.getRank());
465 // Position trace at event passed the end
466 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
467 event
= fExperiment
.getNext(context
);
468 assertNull("Event location", event
);
469 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
472 // ------------------------------------------------------------------------
473 // seekEvent by location (context rank is undefined)
474 // ------------------------------------------------------------------------
477 public void testSeekLocationOnCacheBoundary() {
478 long cacheSize
= fExperiment
.getCacheSize();
480 // Position trace at event rank 0
481 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
482 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
484 ITmfEvent event
= fExperiment
.getNext(context
);
485 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
487 event
= fExperiment
.getNext(context
);
488 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
490 // Position trace at event rank 'cacheSize'
491 tmpContext
= fExperiment
.seekEvent(cacheSize
);
492 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
494 event
= fExperiment
.getNext(context
);
495 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
497 event
= fExperiment
.getNext(context
);
498 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
500 // Position trace at event rank 4 * 'cacheSize'
501 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
502 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
504 event
= fExperiment
.getNext(context
);
505 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
507 event
= fExperiment
.getNext(context
);
508 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
512 public void testSeekLocationNotOnCacheBoundary() {
513 long cacheSize
= fExperiment
.getCacheSize();
515 // Position trace at event 'cacheSize' - 1
516 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
517 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
519 ITmfEvent event
= fExperiment
.getNext(context
);
520 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
522 event
= fExperiment
.getNext(context
);
523 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
525 // Position trace at event rank 2 * 'cacheSize' - 1
526 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
527 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
528 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
530 event
= fExperiment
.getNext(context
);
531 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
533 event
= fExperiment
.getNext(context
);
534 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
536 // Position trace at event rank 4500
537 tmpContext
= fExperiment
.seekEvent(4500);
538 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
540 event
= fExperiment
.getNext(context
);
541 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
543 event
= fExperiment
.getNext(context
);
544 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
548 public void testSeekLocationOutOfScope() {
549 // Position trace at beginning
550 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
552 ITmfEvent event
= fExperiment
.getNext(context
);
553 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
556 // ------------------------------------------------------------------------
557 // getNext - updates the context
558 // ------------------------------------------------------------------------
560 private static void validateContextRanks(ITmfContext context
) {
561 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
562 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
564 int nbTraces
= ctx
.getContexts().length
;
566 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
567 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
568 for (int i
= 0; i
< nbTraces
; i
++) {
569 long rank
= ctx
.getContexts()[i
].getRank();
576 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
580 public void testGetNextAfteSeekingOnTS_1() {
582 final long INITIAL_TS
= 1;
583 final int NB_READS
= 20;
585 // On lower bound, returns the first event (ts = 1)
586 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
588 validateContextRanks(context
);
592 for (int i
= 0; i
< NB_READS
; i
++) {
593 event
= fExperiment
.getNext(context
);
594 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
595 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
598 // Make sure we stay positioned
599 event
= fExperiment
.parseEvent(context
);
600 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
601 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
603 validateContextRanks(context
);
607 public void testGetNextAfteSeekingOnTS_2() {
608 final long INITIAL_TS
= 2;
609 final int NB_READS
= 20;
611 // On lower bound, returns the first event (ts = 2)
612 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
614 validateContextRanks(context
);
618 for (int i
= 0; i
< NB_READS
; i
++) {
619 event
= fExperiment
.getNext(context
);
620 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
621 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
624 // Make sure we stay positioned
625 event
= fExperiment
.parseEvent(context
);
626 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
627 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
629 validateContextRanks(context
);
633 public void testGetNextAfteSeekingOnTS_3() {
635 final long INITIAL_TS
= 500;
636 final int NB_READS
= 20;
638 // On lower bound, returns the first event (ts = 500)
639 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
641 validateContextRanks(context
);
645 for (int i
= 0; i
< NB_READS
; i
++) {
646 event
= fExperiment
.getNext(context
);
647 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
648 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
651 // Make sure we stay positioned
652 event
= fExperiment
.parseEvent(context
);
653 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
654 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
656 validateContextRanks(context
);
660 public void testGetNextAfterSeekingOnRank_1() {
661 final long INITIAL_RANK
= 0L;
662 final int NB_READS
= 20;
664 // On lower bound, returns the first event (rank = 0)
665 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
667 validateContextRanks(context
);
671 for (int i
= 0; i
< NB_READS
; i
++) {
672 event
= fExperiment
.getNext(context
);
673 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
674 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
677 // Make sure we stay positioned
678 event
= fExperiment
.parseEvent(context
);
679 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
680 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
682 validateContextRanks(context
);
686 public void testGetNextAfterSeekingOnRank_2() {
687 final long INITIAL_RANK
= 1L;
688 final int NB_READS
= 20;
690 // On lower bound, returns the first event (rank = 0)
691 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
693 validateContextRanks(context
);
697 for (int i
= 0; i
< NB_READS
; i
++) {
698 event
= fExperiment
.getNext(context
);
699 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
700 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
703 // Make sure we stay positioned
704 event
= fExperiment
.parseEvent(context
);
705 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
706 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
708 validateContextRanks(context
);
712 public void testGetNextAfterSeekingOnRank_3() {
713 final long INITIAL_RANK
= 500L;
714 final int NB_READS
= 20;
716 // On lower bound, returns the first event (rank = 0)
717 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
719 validateContextRanks(context
);
723 for (int i
= 0; i
< NB_READS
; i
++) {
724 event
= fExperiment
.getNext(context
);
725 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
726 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
729 // Make sure we stay positioned
730 event
= fExperiment
.parseEvent(context
);
731 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
732 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
734 validateContextRanks(context
);
738 public void testGetNextAfterSeekingOnLocation_1() {
739 final ITmfLocation INITIAL_LOC
= null;
740 final long INITIAL_TS
= 1;
741 final int NB_READS
= 20;
743 // On lower bound, returns the first event (ts = 1)
744 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
746 validateContextRanks(context
);
750 for (int i
= 0; i
< NB_READS
; i
++) {
751 event
= fExperiment
.getNext(context
);
752 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
753 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
756 // Make sure we stay positioned
757 event
= fExperiment
.parseEvent(context
);
758 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
759 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
761 validateContextRanks(context
);
765 public void testGetNextAfterSeekingOnLocation_2() {
766 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
767 final long INITIAL_TS
= 2;
768 final int NB_READS
= 20;
770 // On lower bound, returns the first event (ts = 2)
771 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
773 validateContextRanks(context
);
777 for (int i
= 0; i
< NB_READS
; i
++) {
778 event
= fExperiment
.getNext(context
);
779 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
782 // Make sure we stay positioned
783 event
= fExperiment
.parseEvent(context
);
784 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
786 validateContextRanks(context
);
790 public void testGetNextAfterSeekingOnLocation_3() {
791 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
792 final long INITIAL_TS
= 501;
793 final int NB_READS
= 20;
795 // On lower bound, returns the first event (ts = 501)
796 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
798 validateContextRanks(context
);
802 for (int i
= 0; i
< NB_READS
; i
++) {
803 event
= fExperiment
.getNext(context
);
804 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
807 // Make sure we stay positioned
808 event
= fExperiment
.parseEvent(context
);
809 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
811 validateContextRanks(context
);
815 public void testGetNextLocation() {
816 ITmfContext context1
= fExperiment
.seekEvent(0);
817 fExperiment
.getNext(context1
);
818 ITmfLocation location
= context1
.getLocation();
819 ITmfEvent event1
= fExperiment
.getNext(context1
);
820 ITmfContext context2
= fExperiment
.seekEvent(location
);
821 ITmfEvent event2
= fExperiment
.getNext(context2
);
822 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
826 public void testGetNextEndLocation() {
827 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
828 fExperiment
.getNext(context1
);
829 ITmfLocation location
= context1
.getLocation();
830 ITmfContext context2
= fExperiment
.seekEvent(location
);
831 ITmfEvent event
= fExperiment
.getNext(context2
);
832 assertNull("Event", event
);
835 // ------------------------------------------------------------------------
837 // ------------------------------------------------------------------------
840 public void testProcessRequestForNbEvents() throws InterruptedException
{
841 final int blockSize
= 100;
842 final int nbEvents
= 1000;
843 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
845 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
846 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
848 public void handleData(final ITmfEvent event
) {
849 super.handleData(event
);
850 requestedEvents
.add(event
);
853 fExperiment
.sendRequest(request
);
854 request
.waitForCompletion();
856 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
857 assertTrue("isCompleted", request
.isCompleted());
858 assertFalse("isCancelled", request
.isCancelled());
860 // Ensure that we have distinct events.
861 // Don't go overboard: we are not validating the stub!
862 for (int i
= 0; i
< nbEvents
; i
++) {
863 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
868 public void testProcessRequestForNbEvents2() throws InterruptedException
{
869 final int blockSize
= 2 * NB_EVENTS
;
870 final int nbEvents
= 1000;
871 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
873 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
874 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
876 public void handleData(final ITmfEvent event
) {
877 super.handleData(event
);
878 requestedEvents
.add(event
);
881 fExperiment
.sendRequest(request
);
882 request
.waitForCompletion();
884 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
885 assertTrue("isCompleted", request
.isCompleted());
886 assertFalse("isCancelled", request
.isCancelled());
888 // Ensure that we have distinct events.
889 // Don't go overboard: we are not validating the stub!
890 for (int i
= 0; i
< nbEvents
; i
++) {
891 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
896 public void testProcessRequestForAllEvents() throws InterruptedException
{
897 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
898 final int blockSize
= 1;
899 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
900 final long nbExpectedEvents
= NB_EVENTS
;
902 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
903 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
905 public void handleData(final ITmfEvent event
) {
906 super.handleData(event
);
907 requestedEvents
.add(event
);
910 fExperiment
.sendRequest(request
);
911 request
.waitForCompletion();
913 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
914 assertTrue("isCompleted", request
.isCompleted());
915 assertFalse("isCancelled", request
.isCancelled());
917 // Ensure that we have distinct events.
918 // Don't go overboard: we are not validating the stub!
919 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
920 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
924 // ------------------------------------------------------------------------
926 // ------------------------------------------------------------------------
929 public void testCancel() throws InterruptedException
{
930 final int nbEvents
= NB_EVENTS
;
931 final int blockSize
= BLOCK_SIZE
;
932 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
934 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
935 final TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class, range
, nbEvents
, blockSize
) {
938 public void handleData(final ITmfEvent event
) {
939 super.handleData(event
);
940 requestedEvents
.add(event
);
941 if (++nbRead
== blockSize
) {
946 public void handleCancel() {
947 if (requestedEvents
.size() < blockSize
) {
948 System
.out
.println("aie");
952 fExperiment
.sendRequest(request
);
953 request
.waitForCompletion();
955 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
956 assertTrue("isCompleted", request
.isCompleted());
957 assertTrue("isCancelled", request
.isCancelled());