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