tmf: Update tmf.core unit tests to JUnit4
[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, 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 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.tests.trace;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNotNull;
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.Collection;
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.event.ITmfEvent;
36 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
37 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
38 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
39 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
40 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
41 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
42 import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
43 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
44 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
45 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
46 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
47 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
48 import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
49 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
50 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
51 import org.junit.Before;
52 import org.junit.Test;
53
54 /**
55 * Test suite for the TmfExperiment class (single trace).
56 */
57 @SuppressWarnings({"nls","javadoc"})
58 public class TmfExperimentTest {
59
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 private static final String DIRECTORY = "testfiles";
65 private static final String TEST_STREAM = "A-Test-10K";
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);
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(DIRECTORY + File.separator + TEST_STREAM);
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 and statistics 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 testGetStateSystem() {
186 /* There should not be any experiment-specific state system */
187 ITmfStateSystem ss = fExperiment.getStateSystem("something");
188 assertNull(ss);
189 }
190
191 @Test
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 @Test
203 public void testSeekBadLocation() {
204 ITmfContext context = fExperiment.seekEvent(new TmfLongLocation(0L));
205 assertNull("seekEvent", context);
206 }
207
208 @Test
209 public void testSeekNoTrace() {
210 TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null);
211 ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
212 assertNull("seekEvent", context);
213 experiment.dispose();
214 }
215
216 // ------------------------------------------------------------------------
217 // seekEvent on ratio
218 // ------------------------------------------------------------------------
219
220 @Test
221 public void testSeekEventOnRatio() {
222 // First event
223 ITmfContext context = fExperiment.seekEvent(0.0);
224 assertEquals("Context rank", 0, context.getRank());
225 ITmfEvent event = fExperiment.parseEvent(context);
226 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
227 assertEquals("Context rank", 0, context.getRank());
228
229 // Middle event
230 int midTrace = NB_EVENTS / 2;
231 context = fExperiment.seekEvent(0.5);
232 assertEquals("Context rank", midTrace, context.getRank());
233 event = fExperiment.parseEvent(context);
234 assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue());
235 assertEquals("Context rank", midTrace, context.getRank());
236
237 // Last event
238 context = fExperiment.seekEvent(1.0);
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 // Beyond last event
245 context = fExperiment.seekEvent(1.1);
246 assertEquals("Context rank", NB_EVENTS, context.getRank());
247 event = fExperiment.parseEvent(context);
248 assertNull("Event timestamp", event);
249 assertEquals("Context rank", NB_EVENTS, context.getRank());
250
251 // Negative ratio
252 context = fExperiment.seekEvent(-0.5);
253 assertEquals("Context rank", 0, context.getRank());
254 event = fExperiment.parseEvent(context);
255 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
256 assertEquals("Context rank", 0, context.getRank());
257 }
258
259 @Test
260 public void testGetLocationRatio() {
261 // First event
262 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
263 double ratio = fExperiment.getLocationRatio(context.getLocation());
264 context = fExperiment.seekEvent(ratio);
265 double ratio2 = fExperiment.getLocationRatio(context.getLocation());
266 assertEquals("getLocationRatio", ratio, ratio2, DELTA);
267
268 // Middle event
269 context = fExperiment.seekEvent(NB_EVENTS / 2);
270 ratio = fExperiment.getLocationRatio(context.getLocation());
271 context = fExperiment.seekEvent(ratio);
272 ratio2 = fExperiment.getLocationRatio(context.getLocation());
273 assertEquals("getLocationRatio", ratio, ratio2, DELTA);
274
275 // Last event
276 context = fExperiment.seekEvent(NB_EVENTS - 1);
277 ratio = fExperiment.getLocationRatio(context.getLocation());
278 context = fExperiment.seekEvent(ratio);
279 ratio2 = fExperiment.getLocationRatio(context.getLocation());
280 assertEquals("getLocationRatio", ratio, ratio2, DELTA);
281 }
282
283 // @SuppressWarnings("rawtypes")
284 // public void testGetCurrentLocation() {
285 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
286 // ITmfLocation location = fExperiment.getCurrentLocation();
287 // assertEquals("getCurrentLocation", location, context.getLocation());
288 // }
289
290 // ------------------------------------------------------------------------
291 // seekEvent on rank
292 // ------------------------------------------------------------------------
293
294 @Test
295 public void testSeekRankOnCacheBoundary() {
296 long cacheSize = fExperiment.getCacheSize();
297
298 // On lower bound, returns the first event (TS = 1)
299 ITmfContext context = fExperiment.seekEvent(0);
300 assertEquals("Context rank", 0, context.getRank());
301
302 ITmfEvent event = fExperiment.getNext(context);
303 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
304 assertEquals("Context rank", 1, context.getRank());
305
306 // Position trace at event rank [cacheSize]
307 context = fExperiment.seekEvent(cacheSize);
308 assertEquals("Context rank", cacheSize, context.getRank());
309
310 event = fExperiment.getNext(context);
311 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
312 assertEquals("Context rank", cacheSize + 1, context.getRank());
313
314 // Position trace at event rank [4 * cacheSize]
315 context = fExperiment.seekEvent(4 * cacheSize);
316 assertEquals("Context rank", 4 * cacheSize, context.getRank());
317
318 event = fExperiment.getNext(context);
319 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
320 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
321 }
322
323 @Test
324 public void testSeekRankNotOnCacheBoundary() {
325 long cacheSize = fExperiment.getCacheSize();
326
327 // Position trace at event rank 9
328 ITmfContext context = fExperiment.seekEvent(9);
329 assertEquals("Context rank", 9, context.getRank());
330
331 ITmfEvent event = fExperiment.getNext(context);
332 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
333 assertEquals("Context rank", 10, context.getRank());
334
335 // Position trace at event rank [cacheSize - 1]
336 context = fExperiment.seekEvent(cacheSize - 1);
337 assertEquals("Context rank", cacheSize - 1, context.getRank());
338
339 event = fExperiment.getNext(context);
340 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
341 assertEquals("Context rank", cacheSize, context.getRank());
342
343 // Position trace at event rank [cacheSize + 1]
344 context = fExperiment.seekEvent(cacheSize + 1);
345 assertEquals("Context rank", cacheSize + 1, context.getRank());
346
347 event = fExperiment.getNext(context);
348 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
349 assertEquals("Context rank", cacheSize + 2, context.getRank());
350
351 // Position trace at event rank 4500
352 context = fExperiment.seekEvent(4500);
353 assertEquals("Context rank", 4500, context.getRank());
354
355 event = fExperiment.getNext(context);
356 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
357 assertEquals("Context rank", 4501, context.getRank());
358 }
359
360 @Test
361 public void testSeekRankOutOfScope() {
362 // Position trace at beginning
363 ITmfContext context = fExperiment.seekEvent(-1);
364 assertEquals("Event rank", 0, context.getRank());
365
366 ITmfEvent event = fExperiment.getNext(context);
367 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
368 assertEquals("Context rank", 1, context.getRank());
369
370 // Position trace at event passed the end
371 context = fExperiment.seekEvent(NB_EVENTS);
372 assertEquals("Context rank", NB_EVENTS, context.getRank());
373
374 event = fExperiment.getNext(context);
375 assertNull("Event", event);
376 assertEquals("Context rank", NB_EVENTS, context.getRank());
377 }
378
379 // ------------------------------------------------------------------------
380 // seekEvent on timestamp
381 // ------------------------------------------------------------------------
382
383 @Test
384 public void testSeekTimestampOnCacheBoundary() {
385 long cacheSize = fExperiment.getCacheSize();
386
387 // Position trace at event rank 0
388 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
389 assertEquals("Context rank", 0, context.getRank());
390
391 ITmfEvent event = fExperiment.getNext(context);
392 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
393 assertEquals("Context rank", 1, context.getRank());
394
395 // Position trace at event rank [cacheSize]
396 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0));
397 assertEquals("Event rank", cacheSize, context.getRank());
398
399 event = fExperiment.getNext(context);
400 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
401 assertEquals("Context rank", cacheSize + 1, context.getRank());
402
403 // Position trace at event rank [4 * cacheSize]
404 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0));
405 assertEquals("Context rank", 4 * cacheSize, context.getRank());
406
407 event = fExperiment.getNext(context);
408 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
409 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
410 }
411
412 @Test
413 public void testSeekTimestampNotOnCacheBoundary() {
414 // Position trace at event rank 1 (TS = 2)
415 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
416 assertEquals("Context rank", 1, context.getRank());
417
418 ITmfEvent event = fExperiment.getNext(context);
419 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
420 assertEquals("Context rank", 2, context.getRank());
421
422 // Position trace at event rank 9 (TS = 10)
423 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
424 assertEquals("Context rank", 9, context.getRank());
425
426 event = fExperiment.getNext(context);
427 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
428 assertEquals("Context rank", 10, context.getRank());
429
430 // Position trace at event rank 999 (TS = 1000)
431 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
432 assertEquals("Context rank", 999, context.getRank());
433
434 event = fExperiment.getNext(context);
435 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
436 assertEquals("Context rank", 1000, context.getRank());
437
438 // Position trace at event rank 1001 (TS = 1002)
439 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
440 assertEquals("Context rank", 1001, context.getRank());
441
442 event = fExperiment.getNext(context);
443 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
444 assertEquals("Context rank", 1002, context.getRank());
445
446 // Position trace at event rank 4500 (TS = 4501)
447 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
448 assertEquals("Context rank", 4500, context.getRank());
449
450 event = fExperiment.getNext(context);
451 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
452 assertEquals("Context rank", 4501, context.getRank());
453 }
454
455 @Test
456 public void testSeekTimestampOutOfScope() {
457 // Position trace at beginning
458 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
459 assertEquals("Event rank", 0, context.getRank());
460
461 ITmfEvent event = fExperiment.getNext(context);
462 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
463 assertEquals("Event rank", 1, context.getRank());
464
465 // Position trace at event passed the end
466 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
467 event = fExperiment.getNext(context);
468 assertNull("Event location", event);
469 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
470 }
471
472 // ------------------------------------------------------------------------
473 // seekEvent by location (context rank is undefined)
474 // ------------------------------------------------------------------------
475
476 @Test
477 public void testSeekLocationOnCacheBoundary() {
478 long cacheSize = fExperiment.getCacheSize();
479
480 // Position trace at event rank 0
481 ITmfContext tmpContext = fExperiment.seekEvent(0);
482 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
483
484 ITmfEvent event = fExperiment.getNext(context);
485 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
486
487 event = fExperiment.getNext(context);
488 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
489
490 // Position trace at event rank 'cacheSize'
491 tmpContext = fExperiment.seekEvent(cacheSize);
492 context = fExperiment.seekEvent(tmpContext.getLocation());
493
494 event = fExperiment.getNext(context);
495 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
496
497 event = fExperiment.getNext(context);
498 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
499
500 // Position trace at event rank 4 * 'cacheSize'
501 tmpContext = fExperiment.seekEvent(4 * cacheSize);
502 context = fExperiment.seekEvent(tmpContext.getLocation());
503
504 event = fExperiment.getNext(context);
505 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
506
507 event = fExperiment.getNext(context);
508 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
509 }
510
511 @Test
512 public void testSeekLocationNotOnCacheBoundary() {
513 long cacheSize = fExperiment.getCacheSize();
514
515 // Position trace at event 'cacheSize' - 1
516 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
517 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
518
519 ITmfEvent event = fExperiment.getNext(context);
520 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
521
522 event = fExperiment.getNext(context);
523 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
524
525 // Position trace at event rank 2 * 'cacheSize' - 1
526 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
527 context = fExperiment.seekEvent(tmpContext.getLocation());
528 context = fExperiment.seekEvent(2 * cacheSize - 1);
529
530 event = fExperiment.getNext(context);
531 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
532
533 event = fExperiment.getNext(context);
534 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
535
536 // Position trace at event rank 4500
537 tmpContext = fExperiment.seekEvent(4500);
538 context = fExperiment.seekEvent(tmpContext.getLocation());
539
540 event = fExperiment.getNext(context);
541 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
542
543 event = fExperiment.getNext(context);
544 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
545 }
546
547 @Test
548 public void testSeekLocationOutOfScope() {
549 // Position trace at beginning
550 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
551
552 ITmfEvent event = fExperiment.getNext(context);
553 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
554 }
555
556 // ------------------------------------------------------------------------
557 // getNext - updates the context
558 // ------------------------------------------------------------------------
559
560 private static void validateContextRanks(ITmfContext context) {
561 assertTrue("Experiment context type", context instanceof TmfExperimentContext);
562 TmfExperimentContext ctx = (TmfExperimentContext) context;
563
564 int nbTraces = ctx.getContexts().length;
565
566 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
567 long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0);
568 for (int i = 0; i < nbTraces; i++) {
569 long rank = ctx.getContexts()[i].getRank();
570 if (rank == -1) {
571 expRank = -1;
572 break;
573 }
574 expRank += rank;
575 }
576 assertEquals("Experiment context rank", expRank, ctx.getRank());
577 }
578
579 @Test
580 public void testGetNextAfteSeekingOnTS_1() {
581
582 final long INITIAL_TS = 1;
583 final int NB_READS = 20;
584
585 // On lower bound, returns the first event (ts = 1)
586 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
587
588 validateContextRanks(context);
589
590 // Read NB_EVENTS
591 ITmfEvent event;
592 for (int i = 0; i < NB_READS; i++) {
593 event = fExperiment.getNext(context);
594 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
595 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
596 }
597
598 // Make sure we stay positioned
599 event = fExperiment.parseEvent(context);
600 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
601 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
602
603 validateContextRanks(context);
604 }
605
606 @Test
607 public void testGetNextAfteSeekingOnTS_2() {
608 final long INITIAL_TS = 2;
609 final int NB_READS = 20;
610
611 // On lower bound, returns the first event (ts = 2)
612 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
613
614 validateContextRanks(context);
615
616 // Read NB_EVENTS
617 ITmfEvent event;
618 for (int i = 0; i < NB_READS; i++) {
619 event = fExperiment.getNext(context);
620 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
621 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
622 }
623
624 // Make sure we stay positioned
625 event = fExperiment.parseEvent(context);
626 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
627 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
628
629 validateContextRanks(context);
630 }
631
632 @Test
633 public void testGetNextAfteSeekingOnTS_3() {
634
635 final long INITIAL_TS = 500;
636 final int NB_READS = 20;
637
638 // On lower bound, returns the first event (ts = 500)
639 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
640
641 validateContextRanks(context);
642
643 // Read NB_EVENTS
644 ITmfEvent event;
645 for (int i = 0; i < NB_READS; i++) {
646 event = fExperiment.getNext(context);
647 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
648 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
649 }
650
651 // Make sure we stay positioned
652 event = fExperiment.parseEvent(context);
653 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
654 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
655
656 validateContextRanks(context);
657 }
658
659 @Test
660 public void testGetNextAfterSeekingOnRank_1() {
661 final long INITIAL_RANK = 0L;
662 final int NB_READS = 20;
663
664 // On lower bound, returns the first event (rank = 0)
665 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
666
667 validateContextRanks(context);
668
669 // Read NB_EVENTS
670 ITmfEvent event;
671 for (int i = 0; i < NB_READS; i++) {
672 event = fExperiment.getNext(context);
673 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
674 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
675 }
676
677 // Make sure we stay positioned
678 event = fExperiment.parseEvent(context);
679 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
680 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
681
682 validateContextRanks(context);
683 }
684
685 @Test
686 public void testGetNextAfterSeekingOnRank_2() {
687 final long INITIAL_RANK = 1L;
688 final int NB_READS = 20;
689
690 // On lower bound, returns the first event (rank = 0)
691 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
692
693 validateContextRanks(context);
694
695 // Read NB_EVENTS
696 ITmfEvent event;
697 for (int i = 0; i < NB_READS; i++) {
698 event = fExperiment.getNext(context);
699 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
700 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
701 }
702
703 // Make sure we stay positioned
704 event = fExperiment.parseEvent(context);
705 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
706 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
707
708 validateContextRanks(context);
709 }
710
711 @Test
712 public void testGetNextAfterSeekingOnRank_3() {
713 final long INITIAL_RANK = 500L;
714 final int NB_READS = 20;
715
716 // On lower bound, returns the first event (rank = 0)
717 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
718
719 validateContextRanks(context);
720
721 // Read NB_EVENTS
722 ITmfEvent event;
723 for (int i = 0; i < NB_READS; i++) {
724 event = fExperiment.getNext(context);
725 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
726 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
727 }
728
729 // Make sure we stay positioned
730 event = fExperiment.parseEvent(context);
731 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
732 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
733
734 validateContextRanks(context);
735 }
736
737 @Test
738 public void testGetNextAfterSeekingOnLocation_1() {
739 final ITmfLocation INITIAL_LOC = null;
740 final long INITIAL_TS = 1;
741 final int NB_READS = 20;
742
743 // On lower bound, returns the first event (ts = 1)
744 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
745
746 validateContextRanks(context);
747
748 // Read NB_EVENTS
749 ITmfEvent event;
750 for (int i = 0; i < NB_READS; i++) {
751 event = fExperiment.getNext(context);
752 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
753 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
754 }
755
756 // Make sure we stay positioned
757 event = fExperiment.parseEvent(context);
758 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
759 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
760
761 validateContextRanks(context);
762 }
763
764 @Test
765 public void testGetNextAfterSeekingOnLocation_2() {
766 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
767 final long INITIAL_TS = 2;
768 final int NB_READS = 20;
769
770 // On lower bound, returns the first event (ts = 2)
771 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
772
773 validateContextRanks(context);
774
775 // Read NB_EVENTS
776 ITmfEvent event;
777 for (int i = 0; i < NB_READS; i++) {
778 event = fExperiment.getNext(context);
779 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
780 }
781
782 // Make sure we stay positioned
783 event = fExperiment.parseEvent(context);
784 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
785
786 validateContextRanks(context);
787 }
788
789 @Test
790 public void testGetNextAfterSeekingOnLocation_3() {
791 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
792 final long INITIAL_TS = 501;
793 final int NB_READS = 20;
794
795 // On lower bound, returns the first event (ts = 501)
796 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
797
798 validateContextRanks(context);
799
800 // Read NB_EVENTS
801 ITmfEvent event;
802 for (int i = 0; i < NB_READS; i++) {
803 event = fExperiment.getNext(context);
804 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
805 }
806
807 // Make sure we stay positioned
808 event = fExperiment.parseEvent(context);
809 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
810
811 validateContextRanks(context);
812 }
813
814 @Test
815 public void testGetNextLocation() {
816 ITmfContext context1 = fExperiment.seekEvent(0);
817 fExperiment.getNext(context1);
818 ITmfLocation location = context1.getLocation();
819 ITmfEvent event1 = fExperiment.getNext(context1);
820 ITmfContext context2 = fExperiment.seekEvent(location);
821 ITmfEvent event2 = fExperiment.getNext(context2);
822 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
823 }
824
825 @Test
826 public void testGetNextEndLocation() {
827 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
828 fExperiment.getNext(context1);
829 ITmfLocation location = context1.getLocation();
830 ITmfContext context2 = fExperiment.seekEvent(location);
831 ITmfEvent event = fExperiment.getNext(context2);
832 assertNull("Event", event);
833 }
834
835 // ------------------------------------------------------------------------
836 // processRequest
837 // ------------------------------------------------------------------------
838
839 @Test
840 public void testProcessRequestForNbEvents() throws InterruptedException {
841 final int blockSize = 100;
842 final int nbEvents = 1000;
843 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
844
845 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
846 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
847 @Override
848 public void handleData(final ITmfEvent event) {
849 super.handleData(event);
850 requestedEvents.add(event);
851 }
852 };
853 fExperiment.sendRequest(request);
854 request.waitForCompletion();
855
856 assertEquals("nbEvents", nbEvents, requestedEvents.size());
857 assertTrue("isCompleted", request.isCompleted());
858 assertFalse("isCancelled", request.isCancelled());
859
860 // Ensure that we have distinct events.
861 // Don't go overboard: we are not validating the stub!
862 for (int i = 0; i < nbEvents; i++) {
863 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
864 }
865 }
866
867 @Test
868 public void testProcessRequestForNbEvents2() throws InterruptedException {
869 final int blockSize = 2 * NB_EVENTS;
870 final int nbEvents = 1000;
871 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
872
873 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
874 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
875 @Override
876 public void handleData(final ITmfEvent event) {
877 super.handleData(event);
878 requestedEvents.add(event);
879 }
880 };
881 fExperiment.sendRequest(request);
882 request.waitForCompletion();
883
884 assertEquals("nbEvents", nbEvents, requestedEvents.size());
885 assertTrue("isCompleted", request.isCompleted());
886 assertFalse("isCancelled", request.isCancelled());
887
888 // Ensure that we have distinct events.
889 // Don't go overboard: we are not validating the stub!
890 for (int i = 0; i < nbEvents; i++) {
891 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
892 }
893 }
894
895 @Test
896 public void testProcessRequestForAllEvents() throws InterruptedException {
897 final int nbEvents = TmfDataRequest.ALL_DATA;
898 final int blockSize = 1;
899 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
900 final long nbExpectedEvents = NB_EVENTS;
901
902 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
903 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
904 @Override
905 public void handleData(final ITmfEvent event) {
906 super.handleData(event);
907 requestedEvents.add(event);
908 }
909 };
910 fExperiment.sendRequest(request);
911 request.waitForCompletion();
912
913 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
914 assertTrue("isCompleted", request.isCompleted());
915 assertFalse("isCancelled", request.isCancelled());
916
917 // Ensure that we have distinct events.
918 // Don't go overboard: we are not validating the stub!
919 for (int i = 0; i < nbExpectedEvents; i++) {
920 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
921 }
922 }
923
924 // ------------------------------------------------------------------------
925 // cancel
926 // ------------------------------------------------------------------------
927
928 @Test
929 public void testCancel() throws InterruptedException {
930 final int nbEvents = NB_EVENTS;
931 final int blockSize = BLOCK_SIZE;
932 final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
933
934 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
935 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
936 int nbRead = 0;
937 @Override
938 public void handleData(final ITmfEvent event) {
939 super.handleData(event);
940 requestedEvents.add(event);
941 if (++nbRead == blockSize) {
942 cancel();
943 }
944 }
945 @Override
946 public void handleCancel() {
947 if (requestedEvents.size() < blockSize) {
948 System.out.println("aie");
949 }
950 }
951 };
952 fExperiment.sendRequest(request);
953 request.waitForCompletion();
954
955 assertEquals("nbEvents", blockSize, requestedEvents.size());
956 assertTrue("isCompleted", request.isCompleted());
957 assertTrue("isCancelled", request.isCancelled());
958 }
959
960 }
This page took 0.052134 seconds and 5 git commands to generate.