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