analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / TmfMultiTraceExperimentTest.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 - Fix for concurrency
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.experiment.TmfExperimentContext;
33 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
34 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
35 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
36 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
37 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
38 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
39 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
40 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
41 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
42 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
43 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
44 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
45 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
46 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub;
47 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
48 import org.junit.AfterClass;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51
52 /**
53 * Test suite for the TmfExperiment class (multiple traces).
54 */
55 @SuppressWarnings("javadoc")
56 public class TmfMultiTraceExperimentTest {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 private static final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
63 private static final String EXPERIMENT = "MyExperiment";
64 private static int NB_EVENTS = 20000;
65 private static int BLOCK_SIZE = 1000;
66
67 private static TmfExperimentStub fExperiment;
68
69 private static byte SCALE = (byte) -3;
70
71 // ------------------------------------------------------------------------
72 // Housekeeping
73 // ------------------------------------------------------------------------
74
75 @BeforeClass
76 public static void setUp() {
77 ITmfTrace[] traces = setupTraces();
78 fExperiment = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
79 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
80 }
81
82 @AfterClass
83 public static void tearDown() {
84 fExperiment.dispose();
85 }
86
87 private static ITmfTrace[] setupTraces() {
88 try {
89 ITmfTrace[] traces = new ITmfTrace[2];
90
91 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TmfTestTrace.O_TEST_10K.getFullPath()), null);
92 File test = new File(FileLocator.toFileURL(location).toURI());
93 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true, null);
94 traces[0] = trace1;
95
96 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TmfTestTrace.E_TEST_10K.getFullPath()), null);
97 test = new File(FileLocator.toFileURL(location).toURI());
98 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true, null);
99 traces[1] = trace2;
100
101 return traces;
102 } catch (final TmfTraceException e) {
103 e.printStackTrace();
104 } catch (final URISyntaxException e) {
105 e.printStackTrace();
106 } catch (final IOException e) {
107 e.printStackTrace();
108 }
109 return new ITmfTrace[0];
110 }
111
112 // ------------------------------------------------------------------------
113 // Constructor
114 // ------------------------------------------------------------------------
115
116 @Test
117 public void testBasicTmfExperimentConstructor() {
118 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
119 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
120
121 final TmfTimeRange timeRange = fExperiment.getTimeRange();
122 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
123 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
124
125 TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
126 assertEquals("getInitialRangeOffset", initRange, fExperiment.getInitialRangeOffset());
127 }
128
129 // ------------------------------------------------------------------------
130 // seekEvent on rank
131 // ------------------------------------------------------------------------
132
133 @Test
134 public void testSeekRankOnCacheBoundary() {
135 long cacheSize = fExperiment.getCacheSize();
136
137 // On lower bound, returns the first event (TS = 1)
138 ITmfContext context = fExperiment.seekEvent(0);
139 assertEquals("Context rank", 0, context.getRank());
140
141 ITmfEvent event = fExperiment.getNext(context);
142 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
143 assertEquals("Context rank", 1, context.getRank());
144
145 // Position trace at event rank [cacheSize]
146 context = fExperiment.seekEvent(cacheSize);
147 assertEquals("Context rank", cacheSize, context.getRank());
148
149 event = fExperiment.getNext(context);
150 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
151 assertEquals("Context rank", cacheSize + 1, context.getRank());
152
153 // Position trace at event rank [4 * cacheSize]
154 context = fExperiment.seekEvent(4 * cacheSize);
155 assertEquals("Context rank", 4 * cacheSize, context.getRank());
156
157 event = fExperiment.getNext(context);
158 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
159 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
160 }
161
162 @Test
163 public void testSeekRankNotOnCacheBoundary() {
164 long cacheSize = fExperiment.getCacheSize();
165
166 // Position trace at event rank 9
167 ITmfContext context = fExperiment.seekEvent(9);
168 assertEquals("Context rank", 9, context.getRank());
169
170 ITmfEvent event = fExperiment.getNext(context);
171 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
172 assertEquals("Context rank", 10, context.getRank());
173
174 // Position trace at event rank [cacheSize - 1]
175 context = fExperiment.seekEvent(cacheSize - 1);
176 assertEquals("Context rank", cacheSize - 1, context.getRank());
177
178 event = fExperiment.getNext(context);
179 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
180 assertEquals("Context rank", cacheSize, context.getRank());
181
182 // Position trace at event rank [cacheSize + 1]
183 context = fExperiment.seekEvent(cacheSize + 1);
184 assertEquals("Context rank", cacheSize + 1, context.getRank());
185
186 event = fExperiment.getNext(context);
187 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
188 assertEquals("Context rank", cacheSize + 2, context.getRank());
189
190 // Position trace at event rank 4500
191 context = fExperiment.seekEvent(4500);
192 assertEquals("Context rank", 4500, context.getRank());
193
194 event = fExperiment.getNext(context);
195 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
196 assertEquals("Context rank", 4501, context.getRank());
197 }
198
199 @Test
200 public void testSeekRankOutOfScope() {
201 // Position trace at beginning
202 ITmfContext context = fExperiment.seekEvent(-1);
203 assertEquals("Event rank", 0, context.getRank());
204
205 ITmfEvent event = fExperiment.getNext(context);
206 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
207 assertEquals("Context rank", 1, context.getRank());
208
209 // Position trace at event passed the end
210 context = fExperiment.seekEvent(NB_EVENTS);
211 assertEquals("Context rank", NB_EVENTS, context.getRank());
212
213 event = fExperiment.getNext(context);
214 assertNull("Event", event);
215 assertEquals("Context rank", NB_EVENTS, context.getRank());
216 }
217
218 // ------------------------------------------------------------------------
219 // seekEvent on timestamp
220 // ------------------------------------------------------------------------
221
222 @Test
223 public void testSeekTimestampOnCacheBoundary() {
224 long cacheSize = fExperiment.getCacheSize();
225
226 // Position trace at event rank 0
227 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE));
228 assertEquals("Context rank", 0, context.getRank());
229
230 ITmfEvent event = fExperiment.getNext(context);
231 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
232 assertEquals("Context rank", 1, context.getRank());
233
234 // Position trace at event rank [cacheSize]
235 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE));
236 assertEquals("Event rank", cacheSize, context.getRank());
237
238 event = fExperiment.getNext(context);
239 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
240 assertEquals("Context rank", cacheSize + 1, context.getRank());
241
242 // Position trace at event rank [4 * cacheSize]
243 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE));
244 assertEquals("Context rank", 4 * cacheSize, context.getRank());
245
246 event = fExperiment.getNext(context);
247 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
248 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
249 }
250
251 @Test
252 public void testSeekTimestampNotOnCacheBoundary() {
253 // Position trace at event rank 1 (TS = 2)
254 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE));
255 assertEquals("Context rank", 1, context.getRank());
256
257 ITmfEvent event = fExperiment.getNext(context);
258 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
259 assertEquals("Context rank", 2, context.getRank());
260
261 // Position trace at event rank 9 (TS = 10)
262 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE));
263 assertEquals("Context rank", 9, context.getRank());
264
265 event = fExperiment.getNext(context);
266 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
267 assertEquals("Context rank", 10, context.getRank());
268
269 // Position trace at event rank 999 (TS = 1000)
270 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE));
271 assertEquals("Context rank", 999, context.getRank());
272
273 event = fExperiment.getNext(context);
274 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
275 assertEquals("Context rank", 1000, context.getRank());
276
277 // Position trace at event rank 1001 (TS = 1002)
278 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE));
279 assertEquals("Context rank", 1001, context.getRank());
280
281 event = fExperiment.getNext(context);
282 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
283 assertEquals("Context rank", 1002, context.getRank());
284
285 // Position trace at event rank 4500 (TS = 4501)
286 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE));
287 assertEquals("Context rank", 4500, context.getRank());
288
289 event = fExperiment.getNext(context);
290 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
291 assertEquals("Context rank", 4501, context.getRank());
292 }
293
294 @Test
295 public void testSeekTimestampOutOfScope() {
296 // Position trace at beginning
297 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE));
298 assertEquals("Event rank", 0, context.getRank());
299
300 ITmfEvent event = fExperiment.getNext(context);
301 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
302 assertEquals("Event rank", 1, context.getRank());
303
304 // Position trace at event passed the end
305 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE));
306 event = fExperiment.getNext(context);
307 assertNull("Event location", event);
308 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
309 }
310
311 // ------------------------------------------------------------------------
312 // seekEvent by location (context rank is undefined)
313 // ------------------------------------------------------------------------
314
315 @Test
316 public void testSeekLocationOnCacheBoundary() {
317 long cacheSize = fExperiment.getCacheSize();
318
319 // Position trace at event rank 0
320 ITmfContext tmpContext = fExperiment.seekEvent(0);
321 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
322
323 ITmfEvent event = fExperiment.getNext(context);
324 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
325
326 event = fExperiment.getNext(context);
327 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
328
329 // Position trace at event rank 'cacheSize'
330 tmpContext = fExperiment.seekEvent(cacheSize);
331 context = fExperiment.seekEvent(tmpContext.getLocation());
332
333 event = fExperiment.getNext(context);
334 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
335
336 event = fExperiment.getNext(context);
337 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
338
339 // Position trace at event rank 4 * 'cacheSize'
340 tmpContext = fExperiment.seekEvent(4 * cacheSize);
341 context = fExperiment.seekEvent(tmpContext.getLocation());
342
343 event = fExperiment.getNext(context);
344 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
345
346 event = fExperiment.getNext(context);
347 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
348 }
349
350 @Test
351 public void testSeekLocationNotOnCacheBoundary() {
352 long cacheSize = fExperiment.getCacheSize();
353
354 // Position trace at event 'cacheSize' - 1
355 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
356 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
357
358 ITmfEvent event = fExperiment.getNext(context);
359 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
360
361 event = fExperiment.getNext(context);
362 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
363
364 // Position trace at event rank 2 * 'cacheSize' - 1
365 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
366 context = fExperiment.seekEvent(tmpContext.getLocation());
367 context = fExperiment.seekEvent(2 * cacheSize - 1);
368
369 event = fExperiment.getNext(context);
370 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
371
372 event = fExperiment.getNext(context);
373 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
374
375 // Position trace at event rank 4500
376 tmpContext = fExperiment.seekEvent(4500);
377 context = fExperiment.seekEvent(tmpContext.getLocation());
378
379 event = fExperiment.getNext(context);
380 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
381
382 event = fExperiment.getNext(context);
383 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
384 }
385
386 @Test
387 public void testSeekLocationOutOfScope() {
388 // Position trace at beginning
389 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
390
391 ITmfEvent event = fExperiment.getNext(context);
392 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
393 }
394
395 // ------------------------------------------------------------------------
396 // getNext - updates the context
397 // ------------------------------------------------------------------------
398
399 private static void validateContextRanks(ITmfContext context) {
400 assertTrue("Experiment context type", context instanceof TmfExperimentContext);
401 TmfExperimentContext ctx = (TmfExperimentContext) context;
402
403 int nbTraces = ctx.getNbTraces();
404
405 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
406 long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0);
407 for (int i = 0; i < nbTraces; i++) {
408 ITmfContext subContext = ctx.getContext(i);
409 assertNotNull(subContext);
410 long rank = subContext.getRank();
411 if (rank == -1) {
412 expRank = -1;
413 break;
414 }
415 expRank += rank;
416 }
417 assertEquals("Experiment context rank", expRank, ctx.getRank());
418 }
419
420 @Test
421 public void testGetNextAfteSeekingOnTS_1() {
422 final long INITIAL_TS = 1;
423 final int NB_READS = 20;
424
425 // On lower bound, returns the first event (ts = 1)
426 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE));
427
428 validateContextRanks(context);
429
430 // Read NB_EVENTS
431 ITmfEvent event;
432 for (int i = 0; i < NB_READS; i++) {
433 event = fExperiment.getNext(context);
434 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
435 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
436 }
437
438 // Make sure we stay positioned
439 event = fExperiment.parseEvent(context);
440 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
441 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
442
443 validateContextRanks(context);
444 }
445
446 @Test
447 public void testGetNextAfteSeekingOnTS_2() {
448 final long INITIAL_TS = 2;
449 final int NB_READS = 20;
450
451 // On lower bound, returns the first event (ts = 2)
452 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE));
453
454 validateContextRanks(context);
455
456 // Read NB_EVENTS
457 ITmfEvent event;
458 for (int i = 0; i < NB_READS; i++) {
459 event = fExperiment.getNext(context);
460 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
461 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
462 }
463
464 // Make sure we stay positioned
465 event = fExperiment.parseEvent(context);
466 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
467 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
468
469 validateContextRanks(context);
470 }
471
472 @Test
473 public void testGetNextAfteSeekingOnTS_3() {
474 final long INITIAL_TS = 500;
475 final int NB_READS = 20;
476
477 // On lower bound, returns the first event (ts = 500)
478 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE));
479
480 validateContextRanks(context);
481
482 // Read NB_EVENTS
483 ITmfEvent event;
484 for (int i = 0; i < NB_READS; i++) {
485 event = fExperiment.getNext(context);
486 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
487 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
488 }
489
490 // Make sure we stay positioned
491 event = fExperiment.parseEvent(context);
492 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
493 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
494
495 validateContextRanks(context);
496 }
497
498 @Test
499 public void testGetNextAfterSeekingOnRank_1() {
500 final long INITIAL_RANK = 0L;
501 final int NB_READS = 20;
502
503 // On lower bound, returns the first event (rank = 0)
504 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
505
506 validateContextRanks(context);
507
508 // Read NB_EVENTS
509 ITmfEvent event;
510 for (int i = 0; i < NB_READS; i++) {
511 event = fExperiment.getNext(context);
512 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
513 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
514 }
515
516 // Make sure we stay positioned
517 event = fExperiment.parseEvent(context);
518 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
519 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
520
521 validateContextRanks(context);
522 }
523
524 @Test
525 public void testGetNextAfterSeekingOnRank_2() {
526 final long INITIAL_RANK = 1L;
527 final int NB_READS = 20;
528
529 // On lower bound, returns the first event (rank = 0)
530 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
531
532 validateContextRanks(context);
533
534 // Read NB_EVENTS
535 ITmfEvent event;
536 for (int i = 0; i < NB_READS; i++) {
537 event = fExperiment.getNext(context);
538 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
539 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
540 }
541
542 // Make sure we stay positioned
543 event = fExperiment.parseEvent(context);
544 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
545 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
546
547 validateContextRanks(context);
548 }
549
550 @Test
551 public void testGetNextAfterSeekingOnRank_3() {
552 final long INITIAL_RANK = 500L;
553 final int NB_READS = 20;
554
555 // On lower bound, returns the first event (rank = 0)
556 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
557
558 validateContextRanks(context);
559
560 // Read NB_EVENTS
561 ITmfEvent event;
562 for (int i = 0; i < NB_READS; i++) {
563 event = fExperiment.getNext(context);
564 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
565 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
566 }
567
568 // Make sure we stay positioned
569 event = fExperiment.parseEvent(context);
570 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
571 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
572
573 validateContextRanks(context);
574 }
575
576 @Test
577 public void testGetNextAfterSeekingOnLocation_1() {
578 final ITmfLocation INITIAL_LOC = null;
579 final long INITIAL_TS = 1;
580 final int NB_READS = 20;
581
582 // On lower bound, returns the first event (ts = 1)
583 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
584
585 validateContextRanks(context);
586
587 // Read NB_EVENTS
588 ITmfEvent event;
589 for (int i = 0; i < NB_READS; i++) {
590 event = fExperiment.getNext(context);
591 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
592 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
593 }
594
595 // Make sure we stay positioned
596 event = fExperiment.parseEvent(context);
597 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
598 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
599
600 validateContextRanks(context);
601 }
602
603 @Test
604 public void testGetNextAfterSeekingOnLocation_2() {
605 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
606 final long INITIAL_TS = 2;
607 final int NB_READS = 20;
608
609 // On lower bound, returns the first event (ts = 2)
610 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
611
612 validateContextRanks(context);
613
614 // Read NB_EVENTS
615 ITmfEvent event;
616 for (int i = 0; i < NB_READS; i++) {
617 event = fExperiment.getNext(context);
618 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
619 }
620
621 // Make sure we stay positioned
622 event = fExperiment.parseEvent(context);
623 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
624
625 validateContextRanks(context);
626 }
627
628 @Test
629 public void testGetNextAfterSeekingOnLocation_3() {
630 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
631 final long INITIAL_TS = 501;
632 final int NB_READS = 20;
633
634 // On lower bound, returns the first event (ts = 501)
635 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
636
637 validateContextRanks(context);
638
639 // Read NB_EVENTS
640 ITmfEvent event;
641 for (int i = 0; i < NB_READS; i++) {
642 event = fExperiment.getNext(context);
643 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
644 }
645
646 // Make sure we stay positioned
647 event = fExperiment.parseEvent(context);
648 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
649
650 validateContextRanks(context);
651 }
652
653 @Test
654 public void testGetNextLocation() {
655 ITmfContext context1 = fExperiment.seekEvent(0);
656 fExperiment.getNext(context1);
657 ITmfLocation location = context1.getLocation();
658 ITmfEvent event1 = fExperiment.getNext(context1);
659 ITmfContext context2 = fExperiment.seekEvent(location);
660 ITmfEvent event2 = fExperiment.getNext(context2);
661 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
662 }
663
664 @Test
665 public void testGetNextEndLocation() {
666 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
667 fExperiment.getNext(context1);
668 ITmfLocation location = context1.getLocation();
669 ITmfContext context2 = fExperiment.seekEvent(location);
670 ITmfEvent event = fExperiment.getNext(context2);
671 assertNull("Event", event);
672 }
673
674 // ------------------------------------------------------------------------
675 // processRequest
676 // ------------------------------------------------------------------------
677
678 @Test
679 public void testProcessRequestForNbEvents() throws InterruptedException {
680 final int nbEvents = 1000;
681 final Vector<ITmfEvent> requestedEvents = new Vector<>();
682
683 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
684 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
685 range, 0, nbEvents, ExecutionType.FOREGROUND) {
686 @Override
687 public void handleData(final ITmfEvent event) {
688 super.handleData(event);
689 requestedEvents.add(event);
690 }
691 };
692 fExperiment.sendRequest(request);
693 request.waitForCompletion();
694
695 assertEquals("nbEvents", nbEvents, requestedEvents.size());
696 assertTrue("isCompleted", request.isCompleted());
697 assertFalse("isCancelled", request.isCancelled());
698
699 // Ensure that we have distinct events.
700 // Don't go overboard: we are not validating the stub!
701 for (int i = 0; i < nbEvents; i++) {
702 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
703 }
704 }
705
706 @Test
707 public void testProcessRequestForAllEvents() throws InterruptedException {
708 final int nbEvents = ITmfEventRequest.ALL_DATA;
709 final Vector<ITmfEvent> requestedEvents = new Vector<>();
710 final long nbExpectedEvents = NB_EVENTS;
711
712 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
713 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
714 range, 0, nbEvents, ExecutionType.FOREGROUND) {
715 @Override
716 public void handleData(final ITmfEvent event) {
717 super.handleData(event);
718 requestedEvents.add(event);
719 }
720 };
721 fExperiment.sendRequest(request);
722 request.waitForCompletion();
723
724 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
725 assertTrue("isCompleted", request.isCompleted());
726 assertFalse("isCancelled", request.isCancelled());
727
728 // Ensure that we have distinct events.
729 // Don't go overboard: we are not validating the stub!
730 for (int i = 0; i < nbExpectedEvents; i++) {
731 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
732 }
733 }
734
735 // ------------------------------------------------------------------------
736 // cancel
737 // ------------------------------------------------------------------------
738
739 @Test
740 public void testCancel() throws InterruptedException {
741 final int nbEvents = NB_EVENTS;
742 final int limit = BLOCK_SIZE;
743 final Vector<ITmfEvent> requestedEvents = new Vector<>();
744
745 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
746 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
747 range, 0, nbEvents, ExecutionType.FOREGROUND) {
748 int nbRead = 0;
749
750 @Override
751 public void handleData(final ITmfEvent event) {
752 super.handleData(event);
753 requestedEvents.add(event);
754 if (++nbRead == limit) {
755 cancel();
756 }
757 }
758 };
759 fExperiment.sendRequest(request);
760 request.waitForCompletion();
761
762 assertEquals("nbEvents", limit, requestedEvents.size());
763 assertTrue("isCompleted", request.isCompleted());
764 assertTrue("isCancelled", request.isCancelled());
765 }
766
767 // ------------------------------------------------------------------------
768 // getTimestamp
769 // ------------------------------------------------------------------------
770
771 @Test
772 public void testGetTimestamp() {
773 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment.getTimestamp( 0));
774 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment.getTimestamp( 1));
775 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment.getTimestamp( 10));
776 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment.getTimestamp( 100));
777 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment.getTimestamp( 1000));
778 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment.getTimestamp( 2000));
779 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment.getTimestamp( 2500));
780 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment.getTimestamp( 9999));
781 assertEquals("getTimestamp", new TmfTimestamp(20000, (byte) -3), fExperiment.getTimestamp(19999));
782 assertNull("getTimestamp", fExperiment.getTimestamp(20000));
783 }
784
785 // ------------------------------------------------------------------------
786 // getInitialRangeOffset, getCurrentRange, getCurrentTime
787 // ------------------------------------------------------------------------
788
789 @Test
790 public void testDefaultCurrentTimeValues() {
791 ITmfTrace[] traces = setupTraces();
792 TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
793
794 // verify initial values
795 TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
796 assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset());
797
798 exp.dispose();
799 }
800
801 @Test
802 public void testInitialRangeOffset() {
803 ITmfTrace[] traces = setupTraces();
804 ((TmfTraceStub) traces[0]).setInitialRangeOffset(new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE));
805 ((TmfTraceStub) traces[1]).setInitialRangeOffset(new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE));
806 TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
807
808 TmfTimestamp initRange = new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE);
809 assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset());
810
811 exp.dispose();
812 }
813
814 }
This page took 0.053078 seconds and 5 git commands to generate.