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