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