Remove context clone and add trace ranks to experiment location
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfExperimentTest.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Trace Model
12 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for rank in experiment location
14 *******************************************************************************/
15
16package org.eclipse.linuxtools.tmf.core.tests.trace;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23
24import java.io.File;
25import java.io.IOException;
26import java.net.URISyntaxException;
27import java.net.URL;
28import java.util.Collection;
29import java.util.Vector;
30
31import org.eclipse.core.resources.IFile;
32import org.eclipse.core.runtime.FileLocator;
33import org.eclipse.core.runtime.Path;
34import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
35import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
36import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
37import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
38import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
39import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
40import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
41import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
42import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
43import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
44import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
45import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
46import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
47import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
48import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
49import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
50import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
51import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
52import org.junit.Before;
53import org.junit.Test;
54
55/**
56 * Test suite for the TmfExperiment class (single trace).
57 */
58@SuppressWarnings({"nls","javadoc"})
59public class TmfExperimentTest {
60
61 // ------------------------------------------------------------------------
62 // Attributes
63 // ------------------------------------------------------------------------
64
65 private static final String DIRECTORY = "testfiles";
66 private static final String TEST_STREAM = "A-Test-10K";
67 private static final String EXPERIMENT = "MyExperiment";
68 private static int NB_EVENTS = 10000;
69 private static int BLOCK_SIZE = 1000;
70
71 private static final double DELTA = 1e-15;
72
73 private ITmfTrace[] fTestTraces;
74 private TmfExperimentStub fExperiment;
75
76 private static byte SCALE = (byte) -3;
77
78 // ------------------------------------------------------------------------
79 // Housekeeping
80 // ------------------------------------------------------------------------
81
82 private synchronized ITmfTrace[] setupTrace(final String path) {
83 if (fTestTraces == null) {
84 fTestTraces = new ITmfTrace[1];
85 try {
86 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
87 final File test = new File(FileLocator.toFileURL(location).toURI());
88 final TmfTraceStub trace = new TmfTraceStub(test.getPath(), 0, true);
89 fTestTraces[0] = trace;
90 } catch (final TmfTraceException e) {
91 e.printStackTrace();
92 } catch (final URISyntaxException e) {
93 e.printStackTrace();
94 } catch (final IOException e) {
95 e.printStackTrace();
96 }
97 }
98 return fTestTraces;
99 }
100
101 private synchronized void setupExperiment() {
102 if (fExperiment == null) {
103 fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE);
104 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
105 }
106 }
107
108 @Before
109 public void setUp() {
110 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
111 setupExperiment();
112 }
113
114 // ------------------------------------------------------------------------
115 // Constructor
116 // ------------------------------------------------------------------------
117
118 @Test
119 public void testSimpleTmfExperimentConstructor() {
120 TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, fTestTraces);
121 assertEquals("GetId", EXPERIMENT, experiment.getName());
122 assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize());
123 experiment.dispose();
124
125 experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null);
126 experiment.dispose();
127 }
128
129 @Test
130 public void testNormalTmfExperimentConstructor() {
131 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
132 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
133
134 final long nbExperimentEvents = fExperiment.getNbEvents();
135 assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents);
136
137 final long nbTraceEvents = fExperiment.getTraces()[0].getNbEvents();
138 assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents);
139
140 final TmfTimeRange timeRange = fExperiment.getTimeRange();
141 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
142 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
143 }
144
145 // ------------------------------------------------------------------------
146 // getTimestamp
147 // ------------------------------------------------------------------------
148
149 @Test
150 public void testGetTimestamp() {
151 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment.getTimestamp( 0));
152 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment.getTimestamp( 1));
153 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment.getTimestamp( 10));
154 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment.getTimestamp( 100));
155 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment.getTimestamp(1000));
156 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment.getTimestamp(2000));
157 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment.getTimestamp(2500));
158 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment.getTimestamp(9999));
159 assertNull("getTimestamp", fExperiment.getTimestamp(10000));
160 }
161
162 // ------------------------------------------------------------------------
163 // Bookmarks file handling
164 // ------------------------------------------------------------------------
165
166 @Test
167 public void testBookmarks() {
168 assertNull("GetBookmarksFile", fExperiment.getBookmarksFile());
169 IFile bookmarks = (IFile) fTestTraces[0].getResource();
170 fExperiment.setBookmarksFile(bookmarks);
171 assertEquals("GetBookmarksFile", bookmarks, fExperiment.getBookmarksFile());
172 }
173
174 // ------------------------------------------------------------------------
175 // State system and statistics methods
176 // ------------------------------------------------------------------------
177
178 @Test
179 public void testGetStatistics() {
180 /* There should not be any experiment-specific statistics */
181 ITmfStatistics stats = fExperiment.getStatistics();
182 assertNull(stats);
183 }
184
185 @Test
186 public void testGetStateSystem() {
187 /* There should not be any experiment-specific state system */
188 ITmfStateSystem ss = fExperiment.getStateSystem("something");
189 assertNull(ss);
190 }
191
192 @Test
193 public void testListStateSystem() {
194 Collection<String> sss = fExperiment.listStateSystems();
195 assertNotNull(sss);
196 assertEquals(0, sss.size());
197 }
198
199 // ------------------------------------------------------------------------
200 // seekEvent by location
201 // ------------------------------------------------------------------------
202
203 @Test
204 public void testSeekBadLocation() {
205 ITmfContext context = fExperiment.seekEvent(new TmfLongLocation(0L));
206 assertNull("seekEvent", context);
207 }
208
209 @Test
210 public void testSeekNoTrace() {
211 TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null);
212 ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
213 assertNull("seekEvent", context);
214 experiment.dispose();
215 }
216
217 // ------------------------------------------------------------------------
218 // seekEvent on ratio
219 // ------------------------------------------------------------------------
220
221 @Test
222 public void testSeekEventOnRatio() {
223 // First event
224 ITmfContext context = fExperiment.seekEvent(0.0);
225 assertEquals("Context rank", 0, context.getRank());
226 ITmfEvent event = fExperiment.parseEvent(context);
227 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
228 assertEquals("Context rank", 0, context.getRank());
229
230 // Middle event
231 int midTrace = NB_EVENTS / 2;
232 context = fExperiment.seekEvent(0.5);
233 assertEquals("Context rank", midTrace, context.getRank());
234 event = fExperiment.parseEvent(context);
235 assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue());
236 assertEquals("Context rank", midTrace, context.getRank());
237
238 // Last event
239 context = fExperiment.seekEvent(1.0);
240 assertEquals("Context rank", NB_EVENTS, context.getRank());
241 event = fExperiment.parseEvent(context);
242 assertNull("Event timestamp", event);
243 assertEquals("Context rank", NB_EVENTS, context.getRank());
244
245 // Beyond last event
246 context = fExperiment.seekEvent(1.1);
247 assertEquals("Context rank", NB_EVENTS, context.getRank());
248 event = fExperiment.parseEvent(context);
249 assertNull("Event timestamp", event);
250 assertEquals("Context rank", NB_EVENTS, context.getRank());
251
252 // Negative ratio
253 context = fExperiment.seekEvent(-0.5);
254 assertEquals("Context rank", 0, context.getRank());
255 event = fExperiment.parseEvent(context);
256 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
257 assertEquals("Context rank", 0, context.getRank());
258 }
259
260 @Test
261 public void testGetLocationRatio() {
262 // First event
263 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
264 double ratio = fExperiment.getLocationRatio(context.getLocation());
265 assertEquals("getLocationRatio", 0.0, ratio, DELTA);
266
267 // Middle event
268 context = fExperiment.seekEvent(NB_EVENTS / 2);
269 ratio = fExperiment.getLocationRatio(context.getLocation());
270 assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio, DELTA);
271
272 // Last event
273 context = fExperiment.seekEvent(NB_EVENTS - 1);
274 ratio = fExperiment.getLocationRatio(context.getLocation());
275 assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio, DELTA);
276 }
277
278// @SuppressWarnings("rawtypes")
279// public void testGetCurrentLocation() {
280// ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
281// ITmfLocation location = fExperiment.getCurrentLocation();
282// assertEquals("getCurrentLocation", location, context.getLocation());
283// }
284
285 // ------------------------------------------------------------------------
286 // seekEvent on rank
287 // ------------------------------------------------------------------------
288
289 @Test
290 public void testSeekRankOnCacheBoundary() {
291 long cacheSize = fExperiment.getCacheSize();
292
293 // On lower bound, returns the first event (TS = 1)
294 ITmfContext context = fExperiment.seekEvent(0);
295 assertEquals("Context rank", 0, context.getRank());
296
297 ITmfEvent event = fExperiment.getNext(context);
298 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
299 assertEquals("Context rank", 1, context.getRank());
300
301 // Position trace at event rank [cacheSize]
302 context = fExperiment.seekEvent(cacheSize);
303 assertEquals("Context rank", cacheSize, context.getRank());
304
305 event = fExperiment.getNext(context);
306 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
307 assertEquals("Context rank", cacheSize + 1, context.getRank());
308
309 // Position trace at event rank [4 * cacheSize]
310 context = fExperiment.seekEvent(4 * cacheSize);
311 assertEquals("Context rank", 4 * cacheSize, context.getRank());
312
313 event = fExperiment.getNext(context);
314 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
315 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
316 }
317
318 @Test
319 public void testSeekRankNotOnCacheBoundary() {
320 long cacheSize = fExperiment.getCacheSize();
321
322 // Position trace at event rank 9
323 ITmfContext context = fExperiment.seekEvent(9);
324 assertEquals("Context rank", 9, context.getRank());
325
326 ITmfEvent event = fExperiment.getNext(context);
327 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
328 assertEquals("Context rank", 10, context.getRank());
329
330 // Position trace at event rank [cacheSize - 1]
331 context = fExperiment.seekEvent(cacheSize - 1);
332 assertEquals("Context rank", cacheSize - 1, context.getRank());
333
334 event = fExperiment.getNext(context);
335 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
336 assertEquals("Context rank", cacheSize, context.getRank());
337
338 // Position trace at event rank [cacheSize + 1]
339 context = fExperiment.seekEvent(cacheSize + 1);
340 assertEquals("Context rank", cacheSize + 1, context.getRank());
341
342 event = fExperiment.getNext(context);
343 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
344 assertEquals("Context rank", cacheSize + 2, context.getRank());
345
346 // Position trace at event rank 4500
347 context = fExperiment.seekEvent(4500);
348 assertEquals("Context rank", 4500, context.getRank());
349
350 event = fExperiment.getNext(context);
351 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
352 assertEquals("Context rank", 4501, context.getRank());
353 }
354
355 @Test
356 public void testSeekRankOutOfScope() {
357 // Position trace at beginning
358 ITmfContext context = fExperiment.seekEvent(-1);
359 assertEquals("Event rank", 0, context.getRank());
360
361 ITmfEvent event = fExperiment.getNext(context);
362 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
363 assertEquals("Context rank", 1, context.getRank());
364
365 // Position trace at event passed the end
366 context = fExperiment.seekEvent(NB_EVENTS);
367 assertEquals("Context rank", NB_EVENTS, context.getRank());
368
369 event = fExperiment.getNext(context);
370 assertNull("Event", event);
371 assertEquals("Context rank", NB_EVENTS, context.getRank());
372 }
373
374 // ------------------------------------------------------------------------
375 // seekEvent on timestamp
376 // ------------------------------------------------------------------------
377
378 @Test
379 public void testSeekTimestampOnCacheBoundary() {
380 long cacheSize = fExperiment.getCacheSize();
381
382 // Position trace at event rank 0
383 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
384 assertEquals("Context rank", 0, context.getRank());
385
386 ITmfEvent event = fExperiment.getNext(context);
387 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
388 assertEquals("Context rank", 1, context.getRank());
389
390 // Position trace at event rank [cacheSize]
391 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0));
392 assertEquals("Event rank", cacheSize, context.getRank());
393
394 event = fExperiment.getNext(context);
395 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
396 assertEquals("Context rank", cacheSize + 1, context.getRank());
397
398 // Position trace at event rank [4 * cacheSize]
399 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0));
400 assertEquals("Context rank", 4 * cacheSize, context.getRank());
401
402 event = fExperiment.getNext(context);
403 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
404 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
405 }
406
407 @Test
408 public void testSeekTimestampNotOnCacheBoundary() {
409 // Position trace at event rank 1 (TS = 2)
410 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
411 assertEquals("Context rank", 1, context.getRank());
412
413 ITmfEvent event = fExperiment.getNext(context);
414 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
415 assertEquals("Context rank", 2, context.getRank());
416
417 // Position trace at event rank 9 (TS = 10)
418 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
419 assertEquals("Context rank", 9, context.getRank());
420
421 event = fExperiment.getNext(context);
422 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
423 assertEquals("Context rank", 10, context.getRank());
424
425 // Position trace at event rank 999 (TS = 1000)
426 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
427 assertEquals("Context rank", 999, context.getRank());
428
429 event = fExperiment.getNext(context);
430 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
431 assertEquals("Context rank", 1000, context.getRank());
432
433 // Position trace at event rank 1001 (TS = 1002)
434 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
435 assertEquals("Context rank", 1001, context.getRank());
436
437 event = fExperiment.getNext(context);
438 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
439 assertEquals("Context rank", 1002, context.getRank());
440
441 // Position trace at event rank 4500 (TS = 4501)
442 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
443 assertEquals("Context rank", 4500, context.getRank());
444
445 event = fExperiment.getNext(context);
446 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
447 assertEquals("Context rank", 4501, context.getRank());
448 }
449
450 @Test
451 public void testSeekTimestampOutOfScope() {
452 // Position trace at beginning
453 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
454 assertEquals("Event rank", 0, context.getRank());
455
456 ITmfEvent event = fExperiment.getNext(context);
457 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
458 assertEquals("Event rank", 1, context.getRank());
459
460 // Position trace at event passed the end
461 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
462 event = fExperiment.getNext(context);
463 assertNull("Event location", event);
464 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
465 }
466
467 // ------------------------------------------------------------------------
468 // seekEvent by location (context rank is undefined)
469 // ------------------------------------------------------------------------
470
471 @Test
472 public void testSeekLocationOnCacheBoundary() {
473 long cacheSize = fExperiment.getCacheSize();
474
475 // Position trace at event rank 0
476 ITmfContext tmpContext = fExperiment.seekEvent(0);
477 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
478
479 ITmfEvent event = fExperiment.getNext(context);
480 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
481
482 event = fExperiment.getNext(context);
483 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
484
485 // Position trace at event rank 'cacheSize'
486 tmpContext = fExperiment.seekEvent(cacheSize);
487 context = fExperiment.seekEvent(tmpContext.getLocation());
488
489 event = fExperiment.getNext(context);
490 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
491
492 event = fExperiment.getNext(context);
493 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
494
495 // Position trace at event rank 4 * 'cacheSize'
496 tmpContext = fExperiment.seekEvent(4 * cacheSize);
497 context = fExperiment.seekEvent(tmpContext.getLocation());
498
499 event = fExperiment.getNext(context);
500 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
501
502 event = fExperiment.getNext(context);
503 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
504 }
505
506 @Test
507 public void testSeekLocationNotOnCacheBoundary() {
508 long cacheSize = fExperiment.getCacheSize();
509
510 // Position trace at event 'cacheSize' - 1
511 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
512 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
513
514 ITmfEvent event = fExperiment.getNext(context);
515 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
516
517 event = fExperiment.getNext(context);
518 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
519
520 // Position trace at event rank 2 * 'cacheSize' - 1
521 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
522 context = fExperiment.seekEvent(tmpContext.getLocation());
523 context = fExperiment.seekEvent(2 * cacheSize - 1);
524
525 event = fExperiment.getNext(context);
526 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
527
528 event = fExperiment.getNext(context);
529 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
530
531 // Position trace at event rank 4500
532 tmpContext = fExperiment.seekEvent(4500);
533 context = fExperiment.seekEvent(tmpContext.getLocation());
534
535 event = fExperiment.getNext(context);
536 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
537
538 event = fExperiment.getNext(context);
539 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
540 }
541
542 @Test
543 public void testSeekLocationOutOfScope() {
544 // Position trace at beginning
545 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
546
547 ITmfEvent event = fExperiment.getNext(context);
548 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
549 }
550
551 // ------------------------------------------------------------------------
552 // getNext - updates the context
553 // ------------------------------------------------------------------------
554
555 private static void validateContextRanks(ITmfContext context) {
556 assertTrue("Experiment context type", context instanceof TmfExperimentContext);
557 TmfExperimentContext ctx = (TmfExperimentContext) context;
558
559 int nbTraces = ctx.getContexts().length;
560
561 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
562 long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0);
563 for (int i = 0; i < nbTraces; i++) {
564 long rank = ctx.getContexts()[i].getRank();
565 if (rank == -1) {
566 expRank = -1;
567 break;
568 }
569 expRank += rank;
570 }
571 assertEquals("Experiment context rank", expRank, ctx.getRank());
572 }
573
574 @Test
575 public void testGetNextAfteSeekingOnTS_1() {
576
577 final long INITIAL_TS = 1;
578 final int NB_READS = 20;
579
580 // On lower bound, returns the first event (ts = 1)
581 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
582
583 validateContextRanks(context);
584
585 // Read NB_EVENTS
586 ITmfEvent event;
587 for (int i = 0; i < NB_READS; i++) {
588 event = fExperiment.getNext(context);
589 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
590 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
591 }
592
593 // Make sure we stay positioned
594 event = fExperiment.parseEvent(context);
595 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
596 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
597
598 validateContextRanks(context);
599 }
600
601 @Test
602 public void testGetNextAfteSeekingOnTS_2() {
603 final long INITIAL_TS = 2;
604 final int NB_READS = 20;
605
606 // On lower bound, returns the first event (ts = 2)
607 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
608
609 validateContextRanks(context);
610
611 // Read NB_EVENTS
612 ITmfEvent event;
613 for (int i = 0; i < NB_READS; i++) {
614 event = fExperiment.getNext(context);
615 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
616 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
617 }
618
619 // Make sure we stay positioned
620 event = fExperiment.parseEvent(context);
621 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
622 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
623
624 validateContextRanks(context);
625 }
626
627 @Test
628 public void testGetNextAfteSeekingOnTS_3() {
629
630 final long INITIAL_TS = 500;
631 final int NB_READS = 20;
632
633 // On lower bound, returns the first event (ts = 500)
634 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
635
636 validateContextRanks(context);
637
638 // Read NB_EVENTS
639 ITmfEvent event;
640 for (int i = 0; i < NB_READS; i++) {
641 event = fExperiment.getNext(context);
642 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
643 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
644 }
645
646 // Make sure we stay positioned
647 event = fExperiment.parseEvent(context);
648 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
649 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
650
651 validateContextRanks(context);
652 }
653
654 @Test
655 public void testGetNextAfterSeekingOnRank_1() {
656 final long INITIAL_RANK = 0L;
657 final int NB_READS = 20;
658
659 // On lower bound, returns the first event (rank = 0)
660 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
661
662 validateContextRanks(context);
663
664 // Read NB_EVENTS
665 ITmfEvent event;
666 for (int i = 0; i < NB_READS; i++) {
667 event = fExperiment.getNext(context);
668 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
669 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
670 }
671
672 // Make sure we stay positioned
673 event = fExperiment.parseEvent(context);
674 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
675 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
676
677 validateContextRanks(context);
678 }
679
680 @Test
681 public void testGetNextAfterSeekingOnRank_2() {
682 final long INITIAL_RANK = 1L;
683 final int NB_READS = 20;
684
685 // On lower bound, returns the first event (rank = 0)
686 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
687
688 validateContextRanks(context);
689
690 // Read NB_EVENTS
691 ITmfEvent event;
692 for (int i = 0; i < NB_READS; i++) {
693 event = fExperiment.getNext(context);
694 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
695 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
696 }
697
698 // Make sure we stay positioned
699 event = fExperiment.parseEvent(context);
700 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
701 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
702
703 validateContextRanks(context);
704 }
705
706 @Test
707 public void testGetNextAfterSeekingOnRank_3() {
708 final long INITIAL_RANK = 500L;
709 final int NB_READS = 20;
710
711 // On lower bound, returns the first event (rank = 0)
712 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
713
714 validateContextRanks(context);
715
716 // Read NB_EVENTS
717 ITmfEvent event;
718 for (int i = 0; i < NB_READS; i++) {
719 event = fExperiment.getNext(context);
720 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
721 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
722 }
723
724 // Make sure we stay positioned
725 event = fExperiment.parseEvent(context);
726 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
727 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
728
729 validateContextRanks(context);
730 }
731
732 @Test
733 public void testGetNextAfterSeekingOnLocation_1() {
734 final ITmfLocation INITIAL_LOC = null;
735 final long INITIAL_TS = 1;
736 final int NB_READS = 20;
737
738 // On lower bound, returns the first event (ts = 1)
739 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
740
741 validateContextRanks(context);
742
743 // Read NB_EVENTS
744 ITmfEvent event;
745 for (int i = 0; i < NB_READS; i++) {
746 event = fExperiment.getNext(context);
747 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
748 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
749 }
750
751 // Make sure we stay positioned
752 event = fExperiment.parseEvent(context);
753 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
754 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
755
756 validateContextRanks(context);
757 }
758
759 @Test
760 public void testGetNextAfterSeekingOnLocation_2() {
761 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
762 final long INITIAL_TS = 2;
763 final int NB_READS = 20;
764
765 // On lower bound, returns the first event (ts = 2)
766 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
767
768 validateContextRanks(context);
769
770 // Read NB_EVENTS
771 ITmfEvent event;
772 for (int i = 0; i < NB_READS; i++) {
773 event = fExperiment.getNext(context);
774 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
775 }
776
777 // Make sure we stay positioned
778 event = fExperiment.parseEvent(context);
779 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
780
781 validateContextRanks(context);
782 }
783
784 @Test
785 public void testGetNextAfterSeekingOnLocation_3() {
786 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
787 final long INITIAL_TS = 501;
788 final int NB_READS = 20;
789
790 // On lower bound, returns the first event (ts = 501)
791 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
792
793 validateContextRanks(context);
794
795 // Read NB_EVENTS
796 ITmfEvent event;
797 for (int i = 0; i < NB_READS; i++) {
798 event = fExperiment.getNext(context);
799 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
800 }
801
802 // Make sure we stay positioned
803 event = fExperiment.parseEvent(context);
804 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
805
806 validateContextRanks(context);
807 }
808
809 @Test
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());
818 }
819
820 @Test
821 public void testGetNextEndLocation() {
822 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
823 fExperiment.getNext(context1);
824 ITmfLocation location = context1.getLocation();
825 ITmfContext context2 = fExperiment.seekEvent(location);
826 ITmfEvent event = fExperiment.getNext(context2);
827 assertNull("Event", event);
828 }
829
830 // ------------------------------------------------------------------------
831 // processRequest
832 // ------------------------------------------------------------------------
833
834 @Test
835 public void testProcessRequestForNbEvents() throws InterruptedException {
836 final int blockSize = 100;
837 final int nbEvents = 1000;
838 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
839
840 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
841 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
842 @Override
843 public void handleData(final ITmfEvent event) {
844 super.handleData(event);
845 requestedEvents.add(event);
846 }
847 };
848 fExperiment.sendRequest(request);
849 request.waitForCompletion();
850
851 assertEquals("nbEvents", nbEvents, requestedEvents.size());
852 assertTrue("isCompleted", request.isCompleted());
853 assertFalse("isCancelled", request.isCancelled());
854
855 // Ensure that we have distinct events.
856 // Don't go overboard: we are not validating the stub!
857 for (int i = 0; i < nbEvents; i++) {
858 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
859 }
860 }
861
862 @Test
863 public void testProcessRequestForNbEvents2() throws InterruptedException {
864 final int blockSize = 2 * NB_EVENTS;
865 final int nbEvents = 1000;
866 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
867
868 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
869 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
870 @Override
871 public void handleData(final ITmfEvent event) {
872 super.handleData(event);
873 requestedEvents.add(event);
874 }
875 };
876 fExperiment.sendRequest(request);
877 request.waitForCompletion();
878
879 assertEquals("nbEvents", nbEvents, requestedEvents.size());
880 assertTrue("isCompleted", request.isCompleted());
881 assertFalse("isCancelled", request.isCancelled());
882
883 // Ensure that we have distinct events.
884 // Don't go overboard: we are not validating the stub!
885 for (int i = 0; i < nbEvents; i++) {
886 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
887 }
888 }
889
890 @Test
891 public void testProcessRequestForAllEvents() throws InterruptedException {
892 final int nbEvents = TmfDataRequest.ALL_DATA;
893 final int blockSize = 1;
894 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
895 final long nbExpectedEvents = NB_EVENTS;
896
897 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
898 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
899 @Override
900 public void handleData(final ITmfEvent event) {
901 super.handleData(event);
902 requestedEvents.add(event);
903 }
904 };
905 fExperiment.sendRequest(request);
906 request.waitForCompletion();
907
908 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
909 assertTrue("isCompleted", request.isCompleted());
910 assertFalse("isCancelled", request.isCancelled());
911
912 // Ensure that we have distinct events.
913 // Don't go overboard: we are not validating the stub!
914 for (int i = 0; i < nbExpectedEvents; i++) {
915 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
916 }
917 }
918
919 // ------------------------------------------------------------------------
920 // cancel
921 // ------------------------------------------------------------------------
922
923 @Test
924 public void testCancel() throws InterruptedException {
925 final int nbEvents = NB_EVENTS;
926 final int blockSize = BLOCK_SIZE;
927 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
928
929 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
930 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
931 int nbRead = 0;
932 @Override
933 public void handleData(final ITmfEvent event) {
934 super.handleData(event);
935 requestedEvents.add(event);
936 if (++nbRead == blockSize) {
937 cancel();
938 }
939 }
940 @Override
941 public void handleCancel() {
942 if (requestedEvents.size() < blockSize) {
943 System.out.println("aie");
944 }
945 }
946 };
947 fExperiment.sendRequest(request);
948 request.waitForCompletion();
949
950 assertEquals("nbEvents", blockSize, requestedEvents.size());
951 assertTrue("isCompleted", request.isCompleted());
952 assertTrue("isCancelled", request.isCancelled());
953 }
954
955}
This page took 0.030706 seconds and 5 git commands to generate.