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