Fix for bug 381411: Implement ranked location in experiment.
[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.TmfExperimentLocation;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
30 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
31 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
32 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
34 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
36 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
38 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
39 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
40 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
41 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
42
43 /**
44 * Test suite for the TmfExperiment class (single trace).
45 */
46 @SuppressWarnings({ "nls" })
47 public class TmfExperimentTest extends TestCase {
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52
53 private static final String DIRECTORY = "testfiles";
54 private static final String TEST_STREAM = "A-Test-10K";
55 private static final String EXPERIMENT = "MyExperiment";
56 private static int NB_EVENTS = 10000;
57 private static int BLOCK_SIZE = 1000;
58
59 private ITmfTrace<TmfEvent>[] fTestTraces;
60 private TmfExperimentStub<ITmfEvent> fExperiment;
61
62 private static byte SCALE = (byte) -3;
63
64 // ------------------------------------------------------------------------
65 // Housekeeping
66 // ------------------------------------------------------------------------
67
68 @SuppressWarnings("unchecked")
69 private synchronized ITmfTrace<?>[] setupTrace(final String path) {
70 if (fTestTraces == null) {
71 fTestTraces = new ITmfTrace[1];
72 try {
73 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
74 final File test = new File(FileLocator.toFileURL(location).toURI());
75 final TmfTraceStub trace = new TmfTraceStub(test.getPath(), 0, true);
76 fTestTraces[0] = trace;
77 } catch (final TmfTraceException e) {
78 e.printStackTrace();
79 } catch (final URISyntaxException e) {
80 e.printStackTrace();
81 } catch (final IOException e) {
82 e.printStackTrace();
83 }
84 }
85 return fTestTraces;
86 }
87
88 private synchronized void setupExperiment() {
89 if (fExperiment == null) {
90 fExperiment = new TmfExperimentStub<ITmfEvent>(EXPERIMENT, fTestTraces, BLOCK_SIZE);
91 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
92 }
93 }
94
95 public TmfExperimentTest(final String name) throws Exception {
96 super(name);
97 }
98
99 @Override
100 protected void setUp() throws Exception {
101 super.setUp();
102 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
103 setupExperiment();
104 }
105
106 @Override
107 protected void tearDown() throws Exception {
108 super.tearDown();
109 }
110
111 // ------------------------------------------------------------------------
112 // Constructor
113 // ------------------------------------------------------------------------
114
115 public void testSimpleTmfExperimentConstructor() {
116
117 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, fTestTraces);
118 assertEquals("GetId", EXPERIMENT, experiment.getName());
119 assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize());
120 experiment.dispose();
121
122 experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
123 experiment.dispose();
124 }
125
126 public void testNormalTmfExperimentConstructor() {
127
128 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
129 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
130
131 final long nbExperimentEvents = fExperiment.getNbEvents();
132 assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents);
133
134 final long nbTraceEvents = fExperiment.getTraces()[0].getNbEvents();
135 assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents);
136
137 final TmfTimeRange timeRange = fExperiment.getTimeRange();
138 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
139 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
140 }
141
142 @SuppressWarnings("static-access")
143 public void testSetCurrentExperiment() {
144
145 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, fTestTraces);
146 experiment.setCurrentExperiment(experiment);
147 assertEquals("getCurrentExperiment", experiment, experiment.getCurrentExperiment());
148
149 TmfExperiment<TmfEvent> experiment2 = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
150 experiment.setCurrentExperiment(experiment2);
151 assertEquals("getCurrentExperiment", experiment2, experiment.getCurrentExperiment());
152
153 experiment.dispose();
154 experiment2.dispose();
155 }
156
157 // ------------------------------------------------------------------------
158 // getTimestamp
159 // ------------------------------------------------------------------------
160
161 public void testGetTimestamp() throws Exception {
162 assertTrue("getTimestamp", fExperiment.getTimestamp( 0).equals(new TmfTimestamp( 1, (byte) -3)));
163 assertTrue("getTimestamp", fExperiment.getTimestamp( 10).equals(new TmfTimestamp( 11, (byte) -3)));
164 assertTrue("getTimestamp", fExperiment.getTimestamp( 100).equals(new TmfTimestamp( 101, (byte) -3)));
165 assertTrue("getTimestamp", fExperiment.getTimestamp( 1000).equals(new TmfTimestamp(1001, (byte) -3)));
166 assertTrue("getTimestamp", fExperiment.getTimestamp( 2000).equals(new TmfTimestamp(2001, (byte) -3)));
167 assertTrue("getTimestamp", fExperiment.getTimestamp( 2500).equals(new TmfTimestamp(2501, (byte) -3)));
168 assertNull("getTimestamp", fExperiment.getTimestamp(10000));
169 }
170
171 // ------------------------------------------------------------------------
172 // Bookmarks file handling
173 // ------------------------------------------------------------------------
174
175 public void testBookmarks() throws Exception {
176 assertNull("GetBookmarksFile", fExperiment.getBookmarksFile());
177 IFile bookmarks = (IFile) fTestTraces[0].getResource();
178 fExperiment.setBookmarksFile(bookmarks);
179 assertEquals("GetBookmarksFile", bookmarks, fExperiment.getBookmarksFile());
180 }
181
182 // ------------------------------------------------------------------------
183 // seekEvent by location
184 // ------------------------------------------------------------------------
185
186 public void testSeekBadLocation() throws Exception {
187 ITmfContext context = fExperiment.seekEvent((ITmfLocation<?>) new TmfLocation<Long>(0L));
188 assertNull("seekEvent", context);
189 }
190
191 public void testSeekNoTrace() throws Exception {
192 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
193 ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
194 assertNull("seekEvent", context);
195 experiment.dispose();
196 }
197
198 // ------------------------------------------------------------------------
199 // seekEvent on ratio
200 // ------------------------------------------------------------------------
201
202 public void testSeekEventOnRatio() throws Exception {
203
204 // First event
205 ITmfContext context = fExperiment.seekEvent(0.0);
206 assertEquals("Context rank", 0, context.getRank());
207 ITmfEvent event = fExperiment.parseEvent(context);
208 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
209 assertEquals("Context rank", 0, context.getRank());
210
211 // Middle event
212 int midTrace = NB_EVENTS / 2;
213 context = fExperiment.seekEvent(0.5);
214 assertEquals("Context rank", midTrace, context.getRank());
215 event = fExperiment.parseEvent(context);
216 assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue());
217 assertEquals("Context rank", midTrace, context.getRank());
218
219 // Last event
220 context = fExperiment.seekEvent(1.0);
221 assertEquals("Context rank", NB_EVENTS, context.getRank());
222 event = fExperiment.parseEvent(context);
223 assertNull("Event timestamp", event);
224 assertEquals("Context rank", NB_EVENTS, context.getRank());
225
226 // Beyond last event
227 context = fExperiment.seekEvent(1.1);
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 // Negative ratio
234 context = fExperiment.seekEvent(-0.5);
235 assertEquals("Context rank", 0, context.getRank());
236 event = fExperiment.parseEvent(context);
237 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
238 assertEquals("Context rank", 0, context.getRank());
239 }
240
241 @SuppressWarnings("rawtypes")
242 public void testGetLocationRatio() throws Exception {
243
244 // First event
245 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
246 double ratio = fExperiment.getLocationRatio(context.getLocation());
247 assertEquals("getLocationRatio", 0.0, ratio);
248
249 // Middle event
250 context = fExperiment.seekEvent(NB_EVENTS / 2);
251 ratio = fExperiment.getLocationRatio(context.getLocation());
252 assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio);
253
254 // Last event
255 context = fExperiment.seekEvent(NB_EVENTS - 1);
256 ratio = fExperiment.getLocationRatio(context.getLocation());
257 assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio);
258 }
259
260 // @SuppressWarnings({ "unchecked", "rawtypes" })
261 // public void testGetCurrentLocation() throws Exception {
262 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
263 // ITmfLocation location = fExperiment.getCurrentLocation();
264 // assertEquals("getCurrentLocation", location, context.getLocation());
265 // }
266
267 // ------------------------------------------------------------------------
268 // seekEvent on rank
269 // ------------------------------------------------------------------------
270
271 public void testSeekRankOnCacheBoundary() throws Exception {
272
273 long cacheSize = fExperiment.getCacheSize();
274
275 // On lower bound, returns the first event (TS = 1)
276 ITmfContext context = fExperiment.seekEvent(0);
277 assertEquals("Context rank", 0, context.getRank());
278
279 ITmfEvent event = fExperiment.getNext(context);
280 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
281 assertEquals("Context rank", 1, context.getRank());
282
283 // Position trace at event rank [cacheSize]
284 context = fExperiment.seekEvent(cacheSize);
285 assertEquals("Context rank", cacheSize, context.getRank());
286
287 event = fExperiment.getNext(context);
288 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
289 assertEquals("Context rank", cacheSize + 1, context.getRank());
290
291 // Position trace at event rank [4 * cacheSize]
292 context = fExperiment.seekEvent(4 * cacheSize);
293 assertEquals("Context rank", 4 * cacheSize, context.getRank());
294
295 event = fExperiment.getNext(context);
296 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
297 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
298 }
299
300 public void testSeekRankNotOnCacheBoundary() throws Exception {
301
302 long cacheSize = fExperiment.getCacheSize();
303
304 // Position trace at event rank 9
305 ITmfContext context = fExperiment.seekEvent(9);
306 assertEquals("Context rank", 9, context.getRank());
307
308 ITmfEvent event = fExperiment.getNext(context);
309 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
310 assertEquals("Context rank", 10, context.getRank());
311
312 // Position trace at event rank [cacheSize - 1]
313 context = fExperiment.seekEvent(cacheSize - 1);
314 assertEquals("Context rank", cacheSize - 1, context.getRank());
315
316 event = fExperiment.getNext(context);
317 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
318 assertEquals("Context rank", cacheSize, context.getRank());
319
320 // Position trace at event rank [cacheSize + 1]
321 context = fExperiment.seekEvent(cacheSize + 1);
322 assertEquals("Context rank", cacheSize + 1, context.getRank());
323
324 event = fExperiment.getNext(context);
325 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
326 assertEquals("Context rank", cacheSize + 2, context.getRank());
327
328 // Position trace at event rank 4500
329 context = fExperiment.seekEvent(4500);
330 assertEquals("Context rank", 4500, context.getRank());
331
332 event = fExperiment.getNext(context);
333 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
334 assertEquals("Context rank", 4501, context.getRank());
335 }
336
337 public void testSeekRankOutOfScope() throws Exception {
338
339 // Position trace at beginning
340 ITmfContext context = fExperiment.seekEvent(-1);
341 assertEquals("Event rank", 0, context.getRank());
342
343 ITmfEvent event = fExperiment.getNext(context);
344 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
345 assertEquals("Context rank", 1, context.getRank());
346
347 // Position trace at event passed the end
348 context = fExperiment.seekEvent(NB_EVENTS);
349 assertEquals("Context rank", NB_EVENTS, context.getRank());
350
351 event = fExperiment.getNext(context);
352 assertNull("Event", event);
353 assertEquals("Context rank", NB_EVENTS, context.getRank());
354 }
355
356 // ------------------------------------------------------------------------
357 // seekEvent on timestamp
358 // ------------------------------------------------------------------------
359
360 public void testSeekTimestampOnCacheBoundary() throws Exception {
361
362 long cacheSize = fExperiment.getCacheSize();
363
364 // Position trace at event rank 0
365 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
366 assertEquals("Context rank", 0, context.getRank());
367
368 ITmfEvent event = fExperiment.getNext(context);
369 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
370 assertEquals("Context rank", 1, context.getRank());
371
372 // Position trace at event rank [cacheSize]
373 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0));
374 assertEquals("Event rank", cacheSize, context.getRank());
375
376 event = fExperiment.getNext(context);
377 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
378 assertEquals("Context rank", cacheSize + 1, context.getRank());
379
380 // Position trace at event rank [4 * cacheSize]
381 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0));
382 assertEquals("Context rank", 4 * cacheSize, context.getRank());
383
384 event = fExperiment.getNext(context);
385 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
386 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
387 }
388
389 public void testSeekTimestampNotOnCacheBoundary() throws Exception {
390
391 // Position trace at event rank 1 (TS = 2)
392 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
393 assertEquals("Context rank", 1, context.getRank());
394
395 ITmfEvent event = fExperiment.getNext(context);
396 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
397 assertEquals("Context rank", 2, context.getRank());
398
399 // Position trace at event rank 9 (TS = 10)
400 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
401 assertEquals("Context rank", 9, context.getRank());
402
403 event = fExperiment.getNext(context);
404 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
405 assertEquals("Context rank", 10, context.getRank());
406
407 // Position trace at event rank 999 (TS = 1000)
408 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
409 assertEquals("Context rank", 999, context.getRank());
410
411 event = fExperiment.getNext(context);
412 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
413 assertEquals("Context rank", 1000, context.getRank());
414
415 // Position trace at event rank 1001 (TS = 1002)
416 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
417 assertEquals("Context rank", 1001, context.getRank());
418
419 event = fExperiment.getNext(context);
420 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
421 assertEquals("Context rank", 1002, context.getRank());
422
423 // Position trace at event rank 4500 (TS = 4501)
424 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
425 assertEquals("Context rank", 4500, context.getRank());
426
427 event = fExperiment.getNext(context);
428 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
429 assertEquals("Context rank", 4501, context.getRank());
430 }
431
432 public void testSeekTimestampOutOfScope() throws Exception {
433
434 // Position trace at beginning
435 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
436 assertEquals("Event rank", 0, context.getRank());
437
438 ITmfEvent event = fExperiment.getNext(context);
439 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
440 assertEquals("Event rank", 1, context.getRank());
441
442 // Position trace at event passed the end
443 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
444 event = fExperiment.getNext(context);
445 assertNull("Event location", event);
446 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
447 }
448
449 // ------------------------------------------------------------------------
450 // seekEvent by location (context rank is undefined)
451 // ------------------------------------------------------------------------
452
453 public void testSeekLocationOnCacheBoundary() throws Exception {
454
455 long cacheSize = fExperiment.getCacheSize();
456
457 // Position trace at event rank 0
458 ITmfContext tmpContext = fExperiment.seekEvent(0);
459 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
460
461 ITmfEvent event = fExperiment.getNext(context);
462 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
463
464 event = fExperiment.getNext(context);
465 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
466
467 // Position trace at event rank 'cacheSize'
468 tmpContext = fExperiment.seekEvent(cacheSize);
469 context = fExperiment.seekEvent(tmpContext.getLocation());
470
471 event = fExperiment.getNext(context);
472 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
473
474 event = fExperiment.getNext(context);
475 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
476
477 // Position trace at event rank 4 * 'cacheSize'
478 tmpContext = fExperiment.seekEvent(4 * cacheSize);
479 context = fExperiment.seekEvent(tmpContext.getLocation());
480
481 event = fExperiment.getNext(context);
482 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
483
484 event = fExperiment.getNext(context);
485 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
486 }
487
488 public void testSeekLocationNotOnCacheBoundary() throws Exception {
489
490 long cacheSize = fExperiment.getCacheSize();
491
492 // Position trace at event 'cacheSize' - 1
493 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
494 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
495
496 ITmfEvent event = fExperiment.getNext(context);
497 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
498
499 event = fExperiment.getNext(context);
500 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
501
502 // Position trace at event rank 2 * 'cacheSize' - 1
503 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
504 context = fExperiment.seekEvent(tmpContext.getLocation());
505 context = fExperiment.seekEvent(2 * cacheSize - 1);
506
507 event = fExperiment.getNext(context);
508 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
509
510 event = fExperiment.getNext(context);
511 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
512
513 // Position trace at event rank 4500
514 tmpContext = fExperiment.seekEvent(4500);
515 context = fExperiment.seekEvent(tmpContext.getLocation());
516
517 event = fExperiment.getNext(context);
518 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
519
520 event = fExperiment.getNext(context);
521 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
522 }
523
524 public void testSeekLocationOutOfScope() throws Exception {
525
526 // Position trace at beginning
527 ITmfContext context = fExperiment.seekEvent((ITmfLocation<?>) null);
528
529 ITmfEvent event = fExperiment.getNext(context);
530 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
531 }
532
533 // ------------------------------------------------------------------------
534 // getNext - updates the context
535 // ------------------------------------------------------------------------
536
537 public void testGetNext() throws Exception {
538
539 // On lower bound, returns the first event (ts = 0)
540 final ITmfContext context = fExperiment.seekEvent(0);
541 ITmfEvent event = fExperiment.getNext(context);
542 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
543
544 for (int i = 2; i < 20; i++) {
545 event = fExperiment.getNext(context);
546 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
547 }
548 }
549
550 public void testGetNextLocation() throws Exception {
551
552 ITmfContext context1 = fExperiment.seekEvent(0);
553 fExperiment.getNext(context1);
554 ITmfLocation<?> location = context1.getLocation().clone();
555 ITmfEvent event1 = fExperiment.getNext(context1);
556 ITmfContext context2 = fExperiment.seekEvent(location);
557 ITmfEvent event2 = fExperiment.getNext(context2);
558 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
559 }
560
561 public void testGetNextEndLocation() throws Exception {
562 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
563 fExperiment.getNext(context1);
564 ITmfLocation<?> location = context1.getLocation().clone();
565 ITmfContext context2 = fExperiment.seekEvent(location);
566 ITmfEvent event = fExperiment.getNext(context2);
567 assertNull("Event", event);
568 }
569
570 // ------------------------------------------------------------------------
571 // processRequest
572 // ------------------------------------------------------------------------
573
574 public void testProcessRequestForNbEvents() throws Exception {
575
576 final int blockSize = 100;
577 final int nbEvents = 1000;
578 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
579
580 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
581 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
582 @Override
583 public void handleData(final TmfEvent event) {
584 super.handleData(event);
585 requestedEvents.add(event);
586 }
587 };
588 fExperiment.sendRequest(request);
589 request.waitForCompletion();
590
591 assertEquals("nbEvents", nbEvents, requestedEvents.size());
592 assertTrue("isCompleted", request.isCompleted());
593 assertFalse("isCancelled", request.isCancelled());
594
595 // Ensure that we have distinct events.
596 // Don't go overboard: we are not validating the stub!
597 for (int i = 0; i < nbEvents; i++) {
598 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
599 }
600 }
601
602 public void testProcessRequestForNbEvents2() throws Exception {
603
604 final int blockSize = 2 * NB_EVENTS;
605 final int nbEvents = 1000;
606 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
607
608 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
609 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
610 @Override
611 public void handleData(final TmfEvent event) {
612 super.handleData(event);
613 requestedEvents.add(event);
614 }
615 };
616 fExperiment.sendRequest(request);
617 request.waitForCompletion();
618
619 assertEquals("nbEvents", nbEvents, requestedEvents.size());
620 assertTrue("isCompleted", request.isCompleted());
621 assertFalse("isCancelled", request.isCancelled());
622
623 // Ensure that we have distinct events.
624 // Don't go overboard: we are not validating the stub!
625 for (int i = 0; i < nbEvents; i++) {
626 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
627 }
628 }
629
630 public void testProcessRequestForAllEvents() throws Exception {
631
632 final int nbEvents = TmfEventRequest.ALL_DATA;
633 final int blockSize = 1;
634 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
635 final long nbExpectedEvents = NB_EVENTS;
636
637 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
638 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
639 @Override
640 public void handleData(final TmfEvent event) {
641 super.handleData(event);
642 requestedEvents.add(event);
643 }
644 };
645 fExperiment.sendRequest(request);
646 request.waitForCompletion();
647
648 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
649 assertTrue("isCompleted", request.isCompleted());
650 assertFalse("isCancelled", request.isCancelled());
651
652 // Ensure that we have distinct events.
653 // Don't go overboard: we are not validating the stub!
654 for (int i = 0; i < nbExpectedEvents; i++) {
655 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
656 }
657 }
658
659 // ------------------------------------------------------------------------
660 // cancel
661 // ------------------------------------------------------------------------
662
663 public void testCancel() throws Exception {
664
665 final int nbEvents = NB_EVENTS;
666 final int blockSize = BLOCK_SIZE;
667 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
668
669 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
670 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
671 int nbRead = 0;
672 @Override
673 public void handleData(final TmfEvent event) {
674 super.handleData(event);
675 requestedEvents.add(event);
676 if (++nbRead == blockSize) {
677 cancel();
678 }
679 }
680 @Override
681 public void handleCancel() {
682 if (requestedEvents.size() < blockSize) {
683 System.out.println("aie");
684 }
685 }
686 };
687 fExperiment.sendRequest(request);
688 request.waitForCompletion();
689
690 assertEquals("nbEvents", blockSize, requestedEvents.size());
691 assertTrue("isCompleted", request.isCompleted());
692 assertTrue("isCancelled", request.isCancelled());
693 }
694
695 }
This page took 0.045656 seconds and 6 git commands to generate.