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
.Vector
;
22 import junit
.framework
.TestCase
;
24 import org
.eclipse
.core
.resources
.IFile
;
25 import org
.eclipse
.core
.runtime
.FileLocator
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
42 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
43 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
46 * Test suite for the TmfExperiment class (single trace).
48 @SuppressWarnings({"nls","javadoc"})
49 public class TmfExperimentTest
extends TestCase
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private static final String DIRECTORY
= "testfiles";
56 private static final String TEST_STREAM
= "A-Test-10K";
57 private static final String EXPERIMENT
= "MyExperiment";
58 private static int NB_EVENTS
= 10000;
59 private static int BLOCK_SIZE
= 1000;
61 private ITmfTrace
<TmfEvent
>[] fTestTraces
;
62 private TmfExperimentStub
<ITmfEvent
> fExperiment
;
64 private static byte SCALE
= (byte) -3;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 @SuppressWarnings("unchecked")
71 private synchronized ITmfTrace
<?
>[] setupTrace(final String path
) {
72 if (fTestTraces
== null) {
73 fTestTraces
= new ITmfTrace
[1];
75 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
76 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
77 final TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 0, true);
78 fTestTraces
[0] = trace
;
79 } catch (final TmfTraceException e
) {
81 } catch (final URISyntaxException e
) {
83 } catch (final IOException e
) {
90 private synchronized void setupExperiment() {
91 if (fExperiment
== null) {
92 fExperiment
= new TmfExperimentStub
<ITmfEvent
>(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
93 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
98 * @param name the test name
100 public TmfExperimentTest(final String name
) {
105 protected void setUp() throws Exception
{
107 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
112 protected void tearDown() throws Exception
{
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
120 public void testSimpleTmfExperimentConstructor() {
122 TmfExperiment
<TmfEvent
> experiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, fTestTraces
);
123 assertEquals("GetId", EXPERIMENT
, experiment
.getName());
124 assertEquals("GetCacheSize", TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, experiment
.getCacheSize());
125 experiment
.dispose();
127 experiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, null);
128 experiment
.dispose();
131 public void testNormalTmfExperimentConstructor() {
133 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
134 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
136 final long nbExperimentEvents
= fExperiment
.getNbEvents();
137 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
139 final long nbTraceEvents
= fExperiment
.getTraces()[0].getNbEvents();
140 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
142 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
143 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
144 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
147 @SuppressWarnings("static-access")
148 public void testSetCurrentExperiment() {
150 TmfExperiment
<TmfEvent
> experiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, fTestTraces
);
151 experiment
.setCurrentExperiment(experiment
);
152 assertEquals("getCurrentExperiment", experiment
, experiment
.getCurrentExperiment());
154 TmfExperiment
<TmfEvent
> experiment2
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, null);
155 experiment
.setCurrentExperiment(experiment2
);
156 assertEquals("getCurrentExperiment", experiment2
, experiment
.getCurrentExperiment());
158 experiment
.dispose();
159 experiment2
.dispose();
162 // ------------------------------------------------------------------------
164 // ------------------------------------------------------------------------
166 public void testGetTimestamp() {
167 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment
.getTimestamp( 0));
168 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment
.getTimestamp( 1));
169 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment
.getTimestamp( 10));
170 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment
.getTimestamp( 100));
171 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment
.getTimestamp(1000));
172 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment
.getTimestamp(2000));
173 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment
.getTimestamp(2500));
174 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment
.getTimestamp(9999));
175 assertNull("getTimestamp", fExperiment
.getTimestamp(10000));
178 // ------------------------------------------------------------------------
179 // Bookmarks file handling
180 // ------------------------------------------------------------------------
182 public void testBookmarks() {
183 assertNull("GetBookmarksFile", fExperiment
.getBookmarksFile());
184 IFile bookmarks
= (IFile
) fTestTraces
[0].getResource();
185 fExperiment
.setBookmarksFile(bookmarks
);
186 assertEquals("GetBookmarksFile", bookmarks
, fExperiment
.getBookmarksFile());
189 // ------------------------------------------------------------------------
190 // seekEvent by location
191 // ------------------------------------------------------------------------
193 public void testSeekBadLocation() {
194 ITmfContext context
= fExperiment
.seekEvent(new TmfLocation
<Long
>(0L));
195 assertNull("seekEvent", context
);
198 public void testSeekNoTrace() {
199 TmfExperiment
<TmfEvent
> experiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, null);
200 ITmfContext context
= experiment
.seekEvent((TmfExperimentLocation
) null);
201 assertNull("seekEvent", context
);
202 experiment
.dispose();
205 // ------------------------------------------------------------------------
206 // seekEvent on ratio
207 // ------------------------------------------------------------------------
209 public void testSeekEventOnRatio() {
212 ITmfContext context
= fExperiment
.seekEvent(0.0);
213 assertEquals("Context rank", 0, context
.getRank());
214 ITmfEvent event
= fExperiment
.parseEvent(context
);
215 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
216 assertEquals("Context rank", 0, context
.getRank());
219 int midTrace
= NB_EVENTS
/ 2;
220 context
= fExperiment
.seekEvent(0.5);
221 assertEquals("Context rank", midTrace
, context
.getRank());
222 event
= fExperiment
.parseEvent(context
);
223 assertEquals("Event timestamp", midTrace
+ 1, event
.getTimestamp().getValue());
224 assertEquals("Context rank", midTrace
, context
.getRank());
227 context
= fExperiment
.seekEvent(1.0);
228 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
229 event
= fExperiment
.parseEvent(context
);
230 assertNull("Event timestamp", event
);
231 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
234 context
= fExperiment
.seekEvent(1.1);
235 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
236 event
= fExperiment
.parseEvent(context
);
237 assertNull("Event timestamp", event
);
238 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
241 context
= fExperiment
.seekEvent(-0.5);
242 assertEquals("Context rank", 0, context
.getRank());
243 event
= fExperiment
.parseEvent(context
);
244 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
245 assertEquals("Context rank", 0, context
.getRank());
248 @SuppressWarnings("rawtypes")
249 public void testGetLocationRatio() {
252 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
253 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
254 context
= fExperiment
.seekEvent(ratio
);
255 double ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
256 assertEquals("getLocationRatio", ratio
, ratio2
);
259 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
260 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
261 context
= fExperiment
.seekEvent(ratio
);
262 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
263 assertEquals("getLocationRatio", ratio
, ratio2
);
266 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
267 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
268 context
= fExperiment
.seekEvent(ratio
);
269 ratio2
= fExperiment
.getLocationRatio(context
.getLocation());
270 assertEquals("getLocationRatio", ratio
, ratio2
);
273 // @SuppressWarnings("rawtypes")
274 // public void testGetCurrentLocation() {
275 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
276 // ITmfLocation location = fExperiment.getCurrentLocation();
277 // assertEquals("getCurrentLocation", location, context.getLocation());
280 // ------------------------------------------------------------------------
282 // ------------------------------------------------------------------------
284 public void testSeekRankOnCacheBoundary() {
286 long cacheSize
= fExperiment
.getCacheSize();
288 // On lower bound, returns the first event (TS = 1)
289 ITmfContext context
= fExperiment
.seekEvent(0);
290 assertEquals("Context rank", 0, context
.getRank());
292 ITmfEvent event
= fExperiment
.getNext(context
);
293 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
294 assertEquals("Context rank", 1, context
.getRank());
296 // Position trace at event rank [cacheSize]
297 context
= fExperiment
.seekEvent(cacheSize
);
298 assertEquals("Context rank", cacheSize
, context
.getRank());
300 event
= fExperiment
.getNext(context
);
301 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
302 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
304 // Position trace at event rank [4 * cacheSize]
305 context
= fExperiment
.seekEvent(4 * cacheSize
);
306 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
308 event
= fExperiment
.getNext(context
);
309 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
310 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
313 public void testSeekRankNotOnCacheBoundary() {
315 long cacheSize
= fExperiment
.getCacheSize();
317 // Position trace at event rank 9
318 ITmfContext context
= fExperiment
.seekEvent(9);
319 assertEquals("Context rank", 9, context
.getRank());
321 ITmfEvent event
= fExperiment
.getNext(context
);
322 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
323 assertEquals("Context rank", 10, context
.getRank());
325 // Position trace at event rank [cacheSize - 1]
326 context
= fExperiment
.seekEvent(cacheSize
- 1);
327 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
329 event
= fExperiment
.getNext(context
);
330 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
331 assertEquals("Context rank", cacheSize
, 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
+ 2, event
.getTimestamp().getValue());
339 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
341 // Position trace at event rank 4500
342 context
= fExperiment
.seekEvent(4500);
343 assertEquals("Context rank", 4500, context
.getRank());
345 event
= fExperiment
.getNext(context
);
346 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
347 assertEquals("Context rank", 4501, context
.getRank());
350 public void testSeekRankOutOfScope() {
352 // Position trace at beginning
353 ITmfContext context
= fExperiment
.seekEvent(-1);
354 assertEquals("Event rank", 0, context
.getRank());
356 ITmfEvent event
= fExperiment
.getNext(context
);
357 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
358 assertEquals("Context rank", 1, context
.getRank());
360 // Position trace at event passed the end
361 context
= fExperiment
.seekEvent(NB_EVENTS
);
362 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
364 event
= fExperiment
.getNext(context
);
365 assertNull("Event", event
);
366 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
369 // ------------------------------------------------------------------------
370 // seekEvent on timestamp
371 // ------------------------------------------------------------------------
373 public void testSeekTimestampOnCacheBoundary() {
375 long cacheSize
= fExperiment
.getCacheSize();
377 // Position trace at event rank 0
378 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
379 assertEquals("Context rank", 0, context
.getRank());
381 ITmfEvent event
= fExperiment
.getNext(context
);
382 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
383 assertEquals("Context rank", 1, context
.getRank());
385 // Position trace at event rank [cacheSize]
386 context
= fExperiment
.seekEvent(new TmfTimestamp(cacheSize
+ 1, SCALE
, 0));
387 assertEquals("Event rank", cacheSize
, context
.getRank());
389 event
= fExperiment
.getNext(context
);
390 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
391 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
393 // Position trace at event rank [4 * cacheSize]
394 context
= fExperiment
.seekEvent(new TmfTimestamp(4 * cacheSize
+ 1, SCALE
, 0));
395 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
397 event
= fExperiment
.getNext(context
);
398 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
399 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
402 public void testSeekTimestampNotOnCacheBoundary() {
404 // Position trace at event rank 1 (TS = 2)
405 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(2, SCALE
, 0));
406 assertEquals("Context rank", 1, context
.getRank());
408 ITmfEvent event
= fExperiment
.getNext(context
);
409 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
410 assertEquals("Context rank", 2, context
.getRank());
412 // Position trace at event rank 9 (TS = 10)
413 context
= fExperiment
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
414 assertEquals("Context rank", 9, context
.getRank());
416 event
= fExperiment
.getNext(context
);
417 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
418 assertEquals("Context rank", 10, context
.getRank());
420 // Position trace at event rank 999 (TS = 1000)
421 context
= fExperiment
.seekEvent(new TmfTimestamp(1000, SCALE
, 0));
422 assertEquals("Context rank", 999, context
.getRank());
424 event
= fExperiment
.getNext(context
);
425 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
426 assertEquals("Context rank", 1000, context
.getRank());
428 // Position trace at event rank 1001 (TS = 1002)
429 context
= fExperiment
.seekEvent(new TmfTimestamp(1002, SCALE
, 0));
430 assertEquals("Context rank", 1001, context
.getRank());
432 event
= fExperiment
.getNext(context
);
433 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
434 assertEquals("Context rank", 1002, context
.getRank());
436 // Position trace at event rank 4500 (TS = 4501)
437 context
= fExperiment
.seekEvent(new TmfTimestamp(4501, SCALE
, 0));
438 assertEquals("Context rank", 4500, context
.getRank());
440 event
= fExperiment
.getNext(context
);
441 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
442 assertEquals("Context rank", 4501, context
.getRank());
445 public void testSeekTimestampOutOfScope() {
447 // Position trace at beginning
448 ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
449 assertEquals("Event rank", 0, context
.getRank());
451 ITmfEvent event
= fExperiment
.getNext(context
);
452 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
453 assertEquals("Event rank", 1, context
.getRank());
455 // Position trace at event passed the end
456 context
= fExperiment
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
457 event
= fExperiment
.getNext(context
);
458 assertNull("Event location", event
);
459 assertEquals("Event rank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
462 // ------------------------------------------------------------------------
463 // seekEvent by location (context rank is undefined)
464 // ------------------------------------------------------------------------
466 public void testSeekLocationOnCacheBoundary() {
468 long cacheSize
= fExperiment
.getCacheSize();
470 // Position trace at event rank 0
471 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
472 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
474 ITmfEvent event
= fExperiment
.getNext(context
);
475 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
477 event
= fExperiment
.getNext(context
);
478 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
480 // Position trace at event rank 'cacheSize'
481 tmpContext
= fExperiment
.seekEvent(cacheSize
);
482 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
484 event
= fExperiment
.getNext(context
);
485 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
487 event
= fExperiment
.getNext(context
);
488 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
490 // Position trace at event rank 4 * 'cacheSize'
491 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
492 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
494 event
= fExperiment
.getNext(context
);
495 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
497 event
= fExperiment
.getNext(context
);
498 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
501 public void testSeekLocationNotOnCacheBoundary() {
503 long cacheSize
= fExperiment
.getCacheSize();
505 // Position trace at event 'cacheSize' - 1
506 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
507 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
509 ITmfEvent event
= fExperiment
.getNext(context
);
510 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
512 event
= fExperiment
.getNext(context
);
513 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
515 // Position trace at event rank 2 * 'cacheSize' - 1
516 tmpContext
= fExperiment
.seekEvent(2 * cacheSize
- 1);
517 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
518 context
= fExperiment
.seekEvent(2 * cacheSize
- 1);
520 event
= fExperiment
.getNext(context
);
521 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
523 event
= fExperiment
.getNext(context
);
524 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
526 // Position trace at event rank 4500
527 tmpContext
= fExperiment
.seekEvent(4500);
528 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
530 event
= fExperiment
.getNext(context
);
531 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
533 event
= fExperiment
.getNext(context
);
534 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
537 public void testSeekLocationOutOfScope() {
539 // Position trace at beginning
540 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
<?
>) null);
542 ITmfEvent event
= fExperiment
.getNext(context
);
543 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
546 // ------------------------------------------------------------------------
547 // getNext - updates the context
548 // ------------------------------------------------------------------------
550 private static void validateContextRanks(ITmfContext context
) {
551 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
552 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
554 int nbTraces
= ctx
.getContexts().length
;
556 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
557 long expRank
= -nbTraces
+ ((ctx
.getLastTrace() != TmfExperimentContext
.NO_TRACE
) ?
1 : 0);
558 for (int i
= 0; i
< nbTraces
; i
++) {
559 long rank
= ctx
.getContexts()[i
].getRank();
566 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
569 public void testGetNextAfteSeekingOnTS_1() {
571 final long INITIAL_TS
= 1;
572 final int NB_READS
= 20;
574 // On lower bound, returns the first event (ts = 1)
575 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
577 validateContextRanks(context
);
581 for (int i
= 0; i
< NB_READS
; i
++) {
582 event
= fExperiment
.getNext(context
);
583 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
584 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
587 // Make sure we stay positioned
588 event
= fExperiment
.parseEvent(context
);
589 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
590 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
592 validateContextRanks(context
);
595 public void testGetNextAfteSeekingOnTS_2() {
597 final long INITIAL_TS
= 2;
598 final int NB_READS
= 20;
600 // On lower bound, returns the first event (ts = 2)
601 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
603 validateContextRanks(context
);
607 for (int i
= 0; i
< NB_READS
; i
++) {
608 event
= fExperiment
.getNext(context
);
609 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
610 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
613 // Make sure we stay positioned
614 event
= fExperiment
.parseEvent(context
);
615 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
616 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
618 validateContextRanks(context
);
621 public void testGetNextAfteSeekingOnTS_3() {
623 final long INITIAL_TS
= 500;
624 final int NB_READS
= 20;
626 // On lower bound, returns the first event (ts = 500)
627 final ITmfContext context
= fExperiment
.seekEvent(new TmfTimestamp(INITIAL_TS
, SCALE
, 0));
629 validateContextRanks(context
);
633 for (int i
= 0; i
< NB_READS
; i
++) {
634 event
= fExperiment
.getNext(context
);
635 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
636 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
639 // Make sure we stay positioned
640 event
= fExperiment
.parseEvent(context
);
641 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
642 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
644 validateContextRanks(context
);
647 public void testGetNextAfterSeekingOnRank_1() {
649 final long INITIAL_RANK
= 0L;
650 final int NB_READS
= 20;
652 // On lower bound, returns the first event (rank = 0)
653 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
655 validateContextRanks(context
);
659 for (int i
= 0; i
< NB_READS
; i
++) {
660 event
= fExperiment
.getNext(context
);
661 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
662 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
665 // Make sure we stay positioned
666 event
= fExperiment
.parseEvent(context
);
667 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
668 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
670 validateContextRanks(context
);
673 public void testGetNextAfterSeekingOnRank_2() {
675 final long INITIAL_RANK
= 1L;
676 final int NB_READS
= 20;
678 // On lower bound, returns the first event (rank = 0)
679 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
681 validateContextRanks(context
);
685 for (int i
= 0; i
< NB_READS
; i
++) {
686 event
= fExperiment
.getNext(context
);
687 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
688 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
691 // Make sure we stay positioned
692 event
= fExperiment
.parseEvent(context
);
693 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
694 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
696 validateContextRanks(context
);
699 public void testGetNextAfterSeekingOnRank_3() {
701 final long INITIAL_RANK
= 500L;
702 final int NB_READS
= 20;
704 // On lower bound, returns the first event (rank = 0)
705 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
707 validateContextRanks(context
);
711 for (int i
= 0; i
< NB_READS
; i
++) {
712 event
= fExperiment
.getNext(context
);
713 assertEquals("Event timestamp", INITIAL_RANK
+ i
+ 1, event
.getTimestamp().getValue());
714 assertEquals("Event rank", INITIAL_RANK
+ i
+ 1, context
.getRank());
717 // Make sure we stay positioned
718 event
= fExperiment
.parseEvent(context
);
719 assertEquals("Event timestamp", INITIAL_RANK
+ NB_READS
+ 1, event
.getTimestamp().getValue());
720 assertEquals("Event rank", INITIAL_RANK
+ NB_READS
, context
.getRank());
722 validateContextRanks(context
);
725 public void testGetNextAfterSeekingOnLocation_1() {
727 final ITmfLocation
<?
> INITIAL_LOC
= null;
728 final long INITIAL_TS
= 1;
729 final int NB_READS
= 20;
731 // On lower bound, returns the first event (ts = 1)
732 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
734 validateContextRanks(context
);
738 for (int i
= 0; i
< NB_READS
; i
++) {
739 event
= fExperiment
.getNext(context
);
740 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
741 assertEquals("Event rank", INITIAL_TS
+ i
, context
.getRank());
744 // Make sure we stay positioned
745 event
= fExperiment
.parseEvent(context
);
746 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
747 assertEquals("Event rank", INITIAL_TS
+ NB_READS
- 1, context
.getRank());
749 validateContextRanks(context
);
752 public void testGetNextAfterSeekingOnLocation_2() {
754 final ITmfLocation
<?
> INITIAL_LOC
= fExperiment
.seekEvent(1L).getLocation();
755 final long INITIAL_TS
= 2;
756 final int NB_READS
= 20;
758 // On lower bound, returns the first event (ts = 2)
759 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
761 validateContextRanks(context
);
765 for (int i
= 0; i
< NB_READS
; i
++) {
766 event
= fExperiment
.getNext(context
);
767 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
770 // Make sure we stay positioned
771 event
= fExperiment
.parseEvent(context
);
772 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
774 validateContextRanks(context
);
777 public void testGetNextAfterSeekingOnLocation_3() {
779 final ITmfLocation
<?
> INITIAL_LOC
= fExperiment
.seekEvent(500L).getLocation();
780 final long INITIAL_TS
= 501;
781 final int NB_READS
= 20;
783 // On lower bound, returns the first event (ts = 501)
784 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
786 validateContextRanks(context
);
790 for (int i
= 0; i
< NB_READS
; i
++) {
791 event
= fExperiment
.getNext(context
);
792 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
795 // Make sure we stay positioned
796 event
= fExperiment
.parseEvent(context
);
797 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
799 validateContextRanks(context
);
802 public void testGetNextLocation() {
803 ITmfContext context1
= fExperiment
.seekEvent(0);
804 fExperiment
.getNext(context1
);
805 ITmfLocation
<?
> location
= context1
.getLocation().clone();
806 ITmfEvent event1
= fExperiment
.getNext(context1
);
807 ITmfContext context2
= fExperiment
.seekEvent(location
);
808 ITmfEvent event2
= fExperiment
.getNext(context2
);
809 assertEquals("Event timestamp", event1
.getTimestamp().getValue(), event2
.getTimestamp().getValue());
812 public void testGetNextEndLocation() {
813 ITmfContext context1
= fExperiment
.seekEvent(fExperiment
.getNbEvents() - 1);
814 fExperiment
.getNext(context1
);
815 ITmfLocation
<?
> location
= context1
.getLocation().clone();
816 ITmfContext context2
= fExperiment
.seekEvent(location
);
817 ITmfEvent event
= fExperiment
.getNext(context2
);
818 assertNull("Event", event
);
821 // ------------------------------------------------------------------------
823 // ------------------------------------------------------------------------
825 public void testProcessRequestForNbEvents() throws InterruptedException
{
827 final int blockSize
= 100;
828 final int nbEvents
= 1000;
829 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
831 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
832 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
834 public void handleData(final TmfEvent event
) {
835 super.handleData(event
);
836 requestedEvents
.add(event
);
839 fExperiment
.sendRequest(request
);
840 request
.waitForCompletion();
842 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
843 assertTrue("isCompleted", request
.isCompleted());
844 assertFalse("isCancelled", request
.isCancelled());
846 // Ensure that we have distinct events.
847 // Don't go overboard: we are not validating the stub!
848 for (int i
= 0; i
< nbEvents
; i
++) {
849 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
853 public void testProcessRequestForNbEvents2() throws InterruptedException
{
855 final int blockSize
= 2 * NB_EVENTS
;
856 final int nbEvents
= 1000;
857 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
859 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
860 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
862 public void handleData(final TmfEvent event
) {
863 super.handleData(event
);
864 requestedEvents
.add(event
);
867 fExperiment
.sendRequest(request
);
868 request
.waitForCompletion();
870 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
871 assertTrue("isCompleted", request
.isCompleted());
872 assertFalse("isCancelled", request
.isCancelled());
874 // Ensure that we have distinct events.
875 // Don't go overboard: we are not validating the stub!
876 for (int i
= 0; i
< nbEvents
; i
++) {
877 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
881 public void testProcessRequestForAllEvents() throws InterruptedException
{
883 final int nbEvents
= TmfDataRequest
.ALL_DATA
;
884 final int blockSize
= 1;
885 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
886 final long nbExpectedEvents
= NB_EVENTS
;
888 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
889 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
891 public void handleData(final TmfEvent event
) {
892 super.handleData(event
);
893 requestedEvents
.add(event
);
896 fExperiment
.sendRequest(request
);
897 request
.waitForCompletion();
899 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
900 assertTrue("isCompleted", request
.isCompleted());
901 assertFalse("isCancelled", request
.isCancelled());
903 // Ensure that we have distinct events.
904 // Don't go overboard: we are not validating the stub!
905 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
906 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
910 // ------------------------------------------------------------------------
912 // ------------------------------------------------------------------------
914 public void testCancel() throws InterruptedException
{
916 final int nbEvents
= NB_EVENTS
;
917 final int blockSize
= BLOCK_SIZE
;
918 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
920 final TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BIG_BANG
, TmfTimestamp
.BIG_CRUNCH
);
921 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
924 public void handleData(final TmfEvent event
) {
925 super.handleData(event
);
926 requestedEvents
.add(event
);
927 if (++nbRead
== blockSize
) {
932 public void handleCancel() {
933 if (requestedEvents
.size() < blockSize
) {
934 System
.out
.println("aie");
938 fExperiment
.sendRequest(request
);
939 request
.waitForCompletion();
941 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
942 assertTrue("isCompleted", request
.isCompleted());
943 assertTrue("isCancelled", request
.isCancelled());
This page took 0.057544 seconds and 6 git commands to generate.