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