1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
17 import java
.io
.IOException
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.Collection
;
21 import java
.util
.Vector
;
23 import junit
.framework
.TestCase
;
25 import org
.eclipse
.core
.resources
.IFile
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
45 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
46 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
49 * Test suite for the TmfExperiment class (single trace).
51 @SuppressWarnings({"nls","javadoc"})
52 public class TmfExperimentTest
extends TestCase
{
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 private static final String DIRECTORY
= "testfiles";
59 private static final String TEST_STREAM
= "A-Test-10K";
60 private static final String EXPERIMENT
= "MyExperiment";
61 private static int NB_EVENTS
= 10000;
62 private static int BLOCK_SIZE
= 1000;
64 private ITmfTrace
[] fTestTraces
;
65 private TmfExperimentStub fExperiment
;
67 private static byte SCALE
= (byte) -3;
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 private synchronized ITmfTrace
[] setupTrace(final String path
) {
74 if (fTestTraces
== null) {
75 fTestTraces
= new ITmfTrace
[1];
77 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
78 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
79 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true);
80 fTestTraces
[0] = trace
;
81 } catch (final TmfTraceException e
) {
83 } catch (final URISyntaxException e
) {
85 } catch (final IOException e
) {
92 private synchronized void setupExperiment() {
93 if (fExperiment
== null) {
94 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
95 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
100 * @param name the test name
102 public TmfExperimentTest(final String name
) {
107 protected void setUp() throws Exception
{
109 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
114 protected void tearDown() throws Exception
{
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
122 public void testSimpleTmfExperimentConstructor() {
124 TmfExperiment experiment
= new TmfExperiment(TmfEvent
.class, EXPERIMENT
, fTestTraces
);
125 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
126 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
127 experiment
.dispose();
129 experiment
= new TmfExperiment(TmfEvent
.class, EXPERIMENT
, null);
130 experiment
.dispose();
133 public void testNormalTmfExperimentConstructor() {
135 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
136 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
138 final long nbExperimentEvents
= fExperiment
.getNbEvents();
139 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
141 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
142 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
144 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
145 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
146 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
153 public void testGetTimestamp() {
154 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
155 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
156 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
157 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
158 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
159 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
160 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
161 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
162 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
165 // ------------------------------------------------------------------------
166 // Bookmarks file handling
167 // ------------------------------------------------------------------------
169 public void testBookmarks() {
170 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
171 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
172 fExperiment
.setBookmarksFile(bookmarks
);
173 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
176 // ------------------------------------------------------------------------
177 // State system and statistics methods
178 // ------------------------------------------------------------------------
180 public void testGetStatistics() {
181 /* There should not be any experiment-specific statistics */
182 ITmfStatistics stats
= fExperiment
.getStatistics();
186 public void testGetStateSystem() {
187 /* There should not be any experiment-specific state system */
188 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 // ------------------------------------------------------------------------
202 public void testSeekBadLocation() {
203 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
204 assertNull("seekEvent", context
);
207 public void testSeekNoTrace() {
208 TmfExperiment experiment
= new TmfExperiment(TmfEvent
.class, EXPERIMENT
, null);
209 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
210 assertNull("seekEvent", context
);
211 experiment
.dispose();
214 // ------------------------------------------------------------------------
215 // seekEvent on ratio
216 // ------------------------------------------------------------------------
218 public void testSeekEventOnRatio() {
221 ITmfContext context
= fExperiment
.seekEvent(0.0);
222 assertEquals("Context rank", 0, context
.getRank());
223 ITmfEvent event
= fExperiment
.parseEvent(context
);
224 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
225 assertEquals("Context rank", 0, context
.getRank());
228 int midTrace
= NB_EVENTS
/ 2;
229 context
= fExperiment
.seekEvent(0.5);
230 assertEquals("Context rank", midTrace
, context
.getRank());
231 event
= fExperiment
.parseEvent(context
);
232 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
233 assertEquals("Context rank", midTrace
, context
.getRank());
236 context
= fExperiment
.seekEvent(1.0);
237 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
238 event
= fExperiment
.parseEvent(context
);
239 assertNull("Event timestamp", event
);
240 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
243 context
= fExperiment
.seekEvent(1.1);
244 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
245 event
= fExperiment
.parseEvent(context
);
246 assertNull("Event timestamp", event
);
247 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
250 context
= fExperiment
.seekEvent(-0.5);
251 assertEquals("Context rank", 0, context
.getRank());
252 event
= fExperiment
.parseEvent(context
);
253 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
254 assertEquals("Context rank", 0, context
.getRank());
257 public void testGetLocationRatio() {
260 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
261 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
262 context
= fExperiment
.seekEvent(ratio
);
263 double ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
264 assertEquals("getLocationRatio", ratio
, ratio2
);
267 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
268 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
269 context
= fExperiment
.seekEvent(ratio
);
270 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
271 assertEquals("getLocationRatio", ratio
, ratio2
);
274 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
275 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
276 context
= fExperiment
.seekEvent(ratio
);
277 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
278 assertEquals("getLocationRatio", ratio
, ratio2
);
281 // @SuppressWarnings("rawtypes")
282 // public void testGetCurrentLocation() {
283 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
284 // ITmfLocation location = fExperiment.getCurrentLocation();
285 // assertEquals("getCurrentLocation", location, context.getLocation());
288 // ------------------------------------------------------------------------
290 // ------------------------------------------------------------------------
292 public void testSeekRankOnCacheBoundary() {
294 long cacheSize
= fExperiment
.getCacheSize();
296 // On lower bound, returns the first event (TS = 1)
297 ITmfContext context
= fExperiment
.seekEvent(0);
298 assertEquals("Context rank", 0, context
.getRank());
300 ITmfEvent event
= fExperiment
.getNext(context
);
301 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
302 assertEquals("Context rank", 1, context
.getRank());
304 // Position trace at event rank [cacheSize]
305 context
= fExperiment
.seekEvent(cacheSize
);
306 assertEquals("Context rank", cacheSize
, context
.getRank());
308 event
= fExperiment
.getNext(context
);
309 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
310 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
312 // Position trace at event rank [4 * cacheSize]
313 context
= fExperiment
.seekEvent(4 * cacheSize
);
314 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
316 event
= fExperiment
.getNext(context
);
317 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
318 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
321 public void testSeekRankNotOnCacheBoundary() {
323 long cacheSize
= fExperiment
.getCacheSize();
325 // Position trace at event rank 9
326 ITmfContext context
= fExperiment
.seekEvent(9);
327 assertEquals("Context rank", 9, context
.getRank());
329 ITmfEvent event
= fExperiment
.getNext(context
);
330 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
331 assertEquals("Context rank", 10, context
.getRank());
333 // Position trace at event rank [cacheSize - 1]
334 context
= fExperiment
.seekEvent(cacheSize
- 1);
335 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
337 event
= fExperiment
.getNext(context
);
338 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
339 assertEquals("Context rank", cacheSize
, context
.getRank());
341 // Position trace at event rank [cacheSize + 1]
342 context
= fExperiment
.seekEvent(cacheSize
+ 1);
343 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
345 event
= fExperiment
.getNext(context
);
346 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
347 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
349 // Position trace at event rank 4500
350 context
= fExperiment
.seekEvent(4500);
351 assertEquals("Context rank", 4500, context
.getRank());
353 event
= fExperiment
.getNext(context
);
354 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
355 assertEquals("Context rank", 4501, context
.getRank());
358 public void testSeekRankOutOfScope() {
360 // Position trace at beginning
361 ITmfContext context
= fExperiment
.seekEvent(-1);
362 assertEquals("Event rank", 0, context
.getRank());
364 ITmfEvent event
= fExperiment
.getNext(context
);
365 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
366 assertEquals("Context rank", 1, context
.getRank());
368 // Position trace at event passed the end
369 context
= fExperiment
.seekEvent(NB_EVENTS
);
370 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
372 event
= fExperiment
.getNext(context
);
373 assertNull("Event", event
);
374 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
377 // ------------------------------------------------------------------------
378 // seekEvent on timestamp
379 // ------------------------------------------------------------------------
381 public void testSeekTimestampOnCacheBoundary() {
383 long cacheSize
= fExperiment
.getCacheSize();
385 // Position trace at event rank 0
386 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
387 assertEquals("Context rank", 0, context
.getRank());
389 ITmfEvent event
= fExperiment
.getNext(context
);
390 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
391 assertEquals("Context rank", 1, context
.getRank());
393 // Position trace at event rank [cacheSize]
394 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
395 assertEquals("Event rank", cacheSize
, context
.getRank());
397 event
= fExperiment
.getNext(context
);
398 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
399 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
401 // Position trace at event rank [4 * cacheSize]
402 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
403 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
405 event
= fExperiment
.getNext(context
);
406 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
407 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
410 public void testSeekTimestampNotOnCacheBoundary() {
412 // Position trace at event rank 1 (TS = 2)
413 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
414 assertEquals("Context rank", 1, context
.getRank());
416 ITmfEvent event
= fExperiment
.getNext(context
);
417 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
418 assertEquals("Context rank", 2, context
.getRank());
420 // Position trace at event rank 9 (TS = 10)
421 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
422 assertEquals("Context rank", 9, context
.getRank());
424 event
= fExperiment
.getNext(context
);
425 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
426 assertEquals("Context rank", 10, context
.getRank());
428 // Position trace at event rank 999 (TS = 1000)
429 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
430 assertEquals("Context rank", 999, context
.getRank());
432 event
= fExperiment
.getNext(context
);
433 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
434 assertEquals("Context rank", 1000, context
.getRank());
436 // Position trace at event rank 1001 (TS = 1002)
437 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
438 assertEquals("Context rank", 1001, context
.getRank());
440 event
= fExperiment
.getNext(context
);
441 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
442 assertEquals("Context rank", 1002, context
.getRank());
444 // Position trace at event rank 4500 (TS = 4501)
445 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
446 assertEquals("Context rank", 4500, context
.getRank());
448 event
= fExperiment
.getNext(context
);
449 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
450 assertEquals("Context rank", 4501, context
.getRank());
453 public void testSeekTimestampOutOfScope() {
455 // Position trace at beginning
456 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
457 assertEquals("Event rank", 0, context
.getRank());
459 ITmfEvent event
= fExperiment
.getNext(context
);
460 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
461 assertEquals("Event rank", 1, context
.getRank());
463 // Position trace at event passed the end
464 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
465 event
= fExperiment
.getNext(context
);
466 assertNull("Event location", event
);
467 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
470 // ------------------------------------------------------------------------
471 // seekEvent by location (context rank is undefined)
472 // ------------------------------------------------------------------------
474 public void testSeekLocationOnCacheBoundary() {
476 long cacheSize
= fExperiment
.getCacheSize();
478 // Position trace at event rank 0
479 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
480 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
482 ITmfEvent event
= fExperiment
.getNext(context
);
483 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
485 event
= fExperiment
.getNext(context
);
486 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
488 // Position trace at event rank 'cacheSize'
489 tmpContext
= fExperiment
.seekEvent(cacheSize
);
490 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
492 event
= fExperiment
.getNext(context
);
493 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
495 event
= fExperiment
.getNext(context
);
496 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
498 // Position trace at event rank 4 * 'cacheSize'
499 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
500 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
502 event
= fExperiment
.getNext(context
);
503 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
505 event
= fExperiment
.getNext(context
);
506 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
509 public void testSeekLocationNotOnCacheBoundary() {
511 long cacheSize
= fExperiment
.getCacheSize();
513 // Position trace at event 'cacheSize' - 1
514 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
515 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
517 ITmfEvent event
= fExperiment
.getNext(context
);
518 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
520 event
= fExperiment
.getNext(context
);
521 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
523 // Position trace at event rank 2 * 'cacheSize' - 1
524 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
525 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
526 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
528 event
= fExperiment
.getNext(context
);
529 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
531 event
= fExperiment
.getNext(context
);
532 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
534 // Position trace at event rank 4500
535 tmpContext
= fExperiment
.seekEvent(4500);
536 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
538 event
= fExperiment
.getNext(context
);
539 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
541 event
= fExperiment
.getNext(context
);
542 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
545 public void testSeekLocationOutOfScope() {
547 // Position trace at beginning
548 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
550 ITmfEvent event
= fExperiment
.getNext(context
);
551 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
554 // ------------------------------------------------------------------------
555 // getNext - updates the context
556 // ------------------------------------------------------------------------
558 private static void validateContextRanks(ITmfContext context
) {
559 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
560 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
562 int nbTraces
= ctx
.getContexts().length
;
564 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
565 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
566 for (int i
= 0; i
< nbTraces
; i
++) {
567 long rank
= ctx
.getContexts()[i
].getRank();
574 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
577 public void testGetNextAfteSeekingOnTS_1() {
579 final long INITIAL_TS
= 1;
580 final int NB_READS
= 20;
582 // On lower bound, returns the first event (ts = 1)
583 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
585 validateContextRanks(context
);
589 for (int i
= 0; i
< NB_READS
; i
++) {
590 event
= fExperiment
.getNext(context
);
591 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
592 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
595 // Make sure we stay positioned
596 event
= fExperiment
.parseEvent(context
);
597 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
598 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
600 validateContextRanks(context
);
603 public void testGetNextAfteSeekingOnTS_2() {
605 final long INITIAL_TS
= 2;
606 final int NB_READS
= 20;
608 // On lower bound, returns the first event (ts = 2)
609 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
611 validateContextRanks(context
);
615 for (int i
= 0; i
< NB_READS
; i
++) {
616 event
= fExperiment
.getNext(context
);
617 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
618 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
621 // Make sure we stay positioned
622 event
= fExperiment
.parseEvent(context
);
623 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
624 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
626 validateContextRanks(context
);
629 public void testGetNextAfteSeekingOnTS_3() {
631 final long INITIAL_TS
= 500;
632 final int NB_READS
= 20;
634 // On lower bound, returns the first event (ts = 500)
635 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
637 validateContextRanks(context
);
641 for (int i
= 0; i
< NB_READS
; i
++) {
642 event
= fExperiment
.getNext(context
);
643 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
644 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
647 // Make sure we stay positioned
648 event
= fExperiment
.parseEvent(context
);
649 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
650 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
652 validateContextRanks(context
);
655 public void testGetNextAfterSeekingOnRank_1() {
657 final long INITIAL_RANK
= 0L;
658 final int NB_READS
= 20;
660 // On lower bound, returns the first event (rank = 0)
661 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
663 validateContextRanks(context
);
667 for (int i
= 0; i
< NB_READS
; i
++) {
668 event
= fExperiment
.getNext(context
);
669 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
670 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
673 // Make sure we stay positioned
674 event
= fExperiment
.parseEvent(context
);
675 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
676 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
678 validateContextRanks(context
);
681 public void testGetNextAfterSeekingOnRank_2() {
683 final long INITIAL_RANK
= 1L;
684 final int NB_READS
= 20;
686 // On lower bound, returns the first event (rank = 0)
687 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
689 validateContextRanks(context
);
693 for (int i
= 0; i
< NB_READS
; i
++) {
694 event
= fExperiment
.getNext(context
);
695 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
696 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
699 // Make sure we stay positioned
700 event
= fExperiment
.parseEvent(context
);
701 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
702 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
704 validateContextRanks(context
);
707 public void testGetNextAfterSeekingOnRank_3() {
709 final long INITIAL_RANK
= 500L;
710 final int NB_READS
= 20;
712 // On lower bound, returns the first event (rank = 0)
713 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
715 validateContextRanks(context
);
719 for (int i
= 0; i
< NB_READS
; i
++) {
720 event
= fExperiment
.getNext(context
);
721 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
722 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
725 // Make sure we stay positioned
726 event
= fExperiment
.parseEvent(context
);
727 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
728 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
730 validateContextRanks(context
);
733 public void testGetNextAfterSeekingOnLocation_1() {
735 final ITmfLocation INITIAL_LOC
= null;
736 final long INITIAL_TS
= 1;
737 final int NB_READS
= 20;
739 // On lower bound, returns the first event (ts = 1)
740 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
742 validateContextRanks(context
);
746 for (int i
= 0; i
< NB_READS
; i
++) {
747 event
= fExperiment
.getNext(context
);
748 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
749 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
752 // Make sure we stay positioned
753 event
= fExperiment
.parseEvent(context
);
754 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
755 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
757 validateContextRanks(context
);
760 public void testGetNextAfterSeekingOnLocation_2() {
762 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
763 final long INITIAL_TS
= 2;
764 final int NB_READS
= 20;
766 // On lower bound, returns the first event (ts = 2)
767 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
769 validateContextRanks(context
);
773 for (int i
= 0; i
< NB_READS
; i
++) {
774 event
= fExperiment
.getNext(context
);
775 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
778 // Make sure we stay positioned
779 event
= fExperiment
.parseEvent(context
);
780 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
782 validateContextRanks(context
);
785 public void testGetNextAfterSeekingOnLocation_3() {
787 final ITmfLocation INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
788 final long INITIAL_TS
= 501;
789 final int NB_READS
= 20;
791 // On lower bound, returns the first event (ts = 501)
792 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
794 validateContextRanks(context
);
798 for (int i
= 0; i
< NB_READS
; i
++) {
799 event
= fExperiment
.getNext(context
);
800 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
803 // Make sure we stay positioned
804 event
= fExperiment
.parseEvent(context
);
805 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
807 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());
820 public void testGetNextEndLocation() {
821 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
822 fExperiment
.getNext(context1
);
823 ITmfLocation location
= context1
.getLocation();
824 ITmfContext context2
= fExperiment
.seekEvent(location
);
825 ITmfEvent event
= fExperiment
.getNext(context2
);
826 assertNull("Event", event
);
829 // ------------------------------------------------------------------------
831 // ------------------------------------------------------------------------
833 public void testProcessRequestForNbEvents() throws InterruptedException
{
835 final int blockSize
= 100;
836 final int nbEvents
= 1000;
837 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
839 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
840 final TmfEventRequest request
= new TmfEventRequest(TmfEvent
.class, range
, nbEvents
, blockSize
) {
842 public void handleData(final ITmfEvent event
) {
843 super.handleData(event
);
844 requestedEvents
.add(event
);
847 fExperiment
.sendRequest(request
);
848 request
.waitForCompletion();
850 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
851 assertTrue("isCompleted", request
.isCompleted());
852 assertFalse("isCancelled", request
.isCancelled());
854 // Ensure that we have distinct events.
855 // Don't go overboard: we are not validating the stub!
856 for (int i
= 0; i
< nbEvents
; i
++) {
857 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
861 public void testProcessRequestForNbEvents2() throws InterruptedException
{
863 final int blockSize
= 2 * NB_EVENTS
;
864 final int nbEvents
= 1000;
865 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
867 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
868 final TmfEventRequest request
= new TmfEventRequest(TmfEvent
.class, range
, nbEvents
, blockSize
) {
870 public void handleData(final ITmfEvent event
) {
871 super.handleData(event
);
872 requestedEvents
.add(event
);
875 fExperiment
.sendRequest(request
);
876 request
.waitForCompletion();
878 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
879 assertTrue("isCompleted", request
.isCompleted());
880 assertFalse("isCancelled", request
.isCancelled());
882 // Ensure that we have distinct events.
883 // Don't go overboard: we are not validating the stub!
884 for (int i
= 0; i
< nbEvents
; i
++) {
885 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
889 public void testProcessRequestForAllEvents() throws InterruptedException
{
891 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
892 final int blockSize
= 1;
893 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
894 final long nbExpectedEvents
= NB_EVENTS
;
896 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
897 final TmfEventRequest request
= new TmfEventRequest(TmfEvent
.class, range
, nbEvents
, blockSize
) {
899 public void handleData(final ITmfEvent event
) {
900 super.handleData(event
);
901 requestedEvents
.add(event
);
904 fExperiment
.sendRequest(request
);
905 request
.waitForCompletion();
907 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
908 assertTrue("isCompleted", request
.isCompleted());
909 assertFalse("isCancelled", request
.isCancelled());
911 // Ensure that we have distinct events.
912 // Don't go overboard: we are not validating the stub!
913 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
914 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
918 // ------------------------------------------------------------------------
920 // ------------------------------------------------------------------------
922 public void testCancel() throws InterruptedException
{
924 final int nbEvents
= NB_EVENTS
;
925 final int blockSize
= BLOCK_SIZE
;
926 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<ITmfEvent
>();
928 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
929 final TmfEventRequest request
= new TmfEventRequest(TmfEvent
.class, range
, nbEvents
, blockSize
) {
932 public void handleData(final ITmfEvent event
) {
933 super.handleData(event
);
934 requestedEvents
.add(event
);
935 if (++nbRead
== blockSize
) {
940 public void handleCancel() {
941 if (requestedEvents
.size() < blockSize
) {
942 System
.out
.println("aie");
946 fExperiment
.sendRequest(request
);
947 request
.waitForCompletion();
949 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
950 assertTrue("isCompleted", request
.isCompleted());
951 assertTrue("isCancelled", request
.isCancelled());