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