Fix unnecessary @SuppressWarning and removed dead code.
[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 context = fExperiment.seekEvent(ratio);
248 double ratio2 = fExperiment.getLocationRatio(context.getLocation());
249 assertEquals("getLocationRatio", ratio, ratio2);
250
251 // Middle event
252 context = fExperiment.seekEvent(NB_EVENTS / 2);
253 ratio = fExperiment.getLocationRatio(context.getLocation());
254 context = fExperiment.seekEvent(ratio);
255 ratio2 = fExperiment.getLocationRatio(context.getLocation());
256 assertEquals("getLocationRatio", ratio, ratio2);
257
258 // Last event
259 context = fExperiment.seekEvent(NB_EVENTS - 1);
260 ratio = fExperiment.getLocationRatio(context.getLocation());
261 context = fExperiment.seekEvent(ratio);
262 ratio2 = fExperiment.getLocationRatio(context.getLocation());
263 assertEquals("getLocationRatio", ratio, ratio2);
264 }
265
266 // @SuppressWarnings({ "unchecked", "rawtypes" })
267 // public void testGetCurrentLocation() throws Exception {
268 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
269 // ITmfLocation location = fExperiment.getCurrentLocation();
270 // assertEquals("getCurrentLocation", location, context.getLocation());
271 // }
272
273 // ------------------------------------------------------------------------
274 // seekEvent on rank
275 // ------------------------------------------------------------------------
276
277 public void testSeekRankOnCacheBoundary() throws Exception {
278
279 long cacheSize = fExperiment.getCacheSize();
280
281 // On lower bound, returns the first event (TS = 1)
282 ITmfContext context = fExperiment.seekEvent(0);
283 assertEquals("Context rank", 0, context.getRank());
284
285 ITmfEvent event = fExperiment.getNext(context);
286 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
287 assertEquals("Context rank", 1, context.getRank());
288
289 // Position trace at event rank [cacheSize]
290 context = fExperiment.seekEvent(cacheSize);
291 assertEquals("Context rank", cacheSize, context.getRank());
292
293 event = fExperiment.getNext(context);
294 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
295 assertEquals("Context rank", cacheSize + 1, context.getRank());
296
297 // Position trace at event rank [4 * cacheSize]
298 context = fExperiment.seekEvent(4 * cacheSize);
299 assertEquals("Context rank", 4 * cacheSize, context.getRank());
300
301 event = fExperiment.getNext(context);
302 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
303 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
304 }
305
306 public void testSeekRankNotOnCacheBoundary() throws Exception {
307
308 long cacheSize = fExperiment.getCacheSize();
309
310 // Position trace at event rank 9
311 ITmfContext context = fExperiment.seekEvent(9);
312 assertEquals("Context rank", 9, context.getRank());
313
314 ITmfEvent event = fExperiment.getNext(context);
315 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
316 assertEquals("Context rank", 10, context.getRank());
317
318 // Position trace at event rank [cacheSize - 1]
319 context = fExperiment.seekEvent(cacheSize - 1);
320 assertEquals("Context rank", cacheSize - 1, context.getRank());
321
322 event = fExperiment.getNext(context);
323 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
324 assertEquals("Context rank", cacheSize, context.getRank());
325
326 // Position trace at event rank [cacheSize + 1]
327 context = fExperiment.seekEvent(cacheSize + 1);
328 assertEquals("Context rank", cacheSize + 1, context.getRank());
329
330 event = fExperiment.getNext(context);
331 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
332 assertEquals("Context rank", cacheSize + 2, context.getRank());
333
334 // Position trace at event rank 4500
335 context = fExperiment.seekEvent(4500);
336 assertEquals("Context rank", 4500, context.getRank());
337
338 event = fExperiment.getNext(context);
339 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
340 assertEquals("Context rank", 4501, context.getRank());
341 }
342
343 public void testSeekRankOutOfScope() throws Exception {
344
345 // Position trace at beginning
346 ITmfContext context = fExperiment.seekEvent(-1);
347 assertEquals("Event rank", 0, context.getRank());
348
349 ITmfEvent event = fExperiment.getNext(context);
350 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
351 assertEquals("Context rank", 1, context.getRank());
352
353 // Position trace at event passed the end
354 context = fExperiment.seekEvent(NB_EVENTS);
355 assertEquals("Context rank", NB_EVENTS, context.getRank());
356
357 event = fExperiment.getNext(context);
358 assertNull("Event", event);
359 assertEquals("Context rank", NB_EVENTS, context.getRank());
360 }
361
362 // ------------------------------------------------------------------------
363 // seekEvent on timestamp
364 // ------------------------------------------------------------------------
365
366 public void testSeekTimestampOnCacheBoundary() throws Exception {
367
368 long cacheSize = fExperiment.getCacheSize();
369
370 // Position trace at event rank 0
371 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
372 assertEquals("Context rank", 0, context.getRank());
373
374 ITmfEvent event = fExperiment.getNext(context);
375 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
376 assertEquals("Context rank", 1, context.getRank());
377
378 // Position trace at event rank [cacheSize]
379 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0));
380 assertEquals("Event rank", cacheSize, context.getRank());
381
382 event = fExperiment.getNext(context);
383 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
384 assertEquals("Context rank", cacheSize + 1, context.getRank());
385
386 // Position trace at event rank [4 * cacheSize]
387 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0));
388 assertEquals("Context rank", 4 * cacheSize, context.getRank());
389
390 event = fExperiment.getNext(context);
391 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
392 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
393 }
394
395 public void testSeekTimestampNotOnCacheBoundary() throws Exception {
396
397 // Position trace at event rank 1 (TS = 2)
398 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
399 assertEquals("Context rank", 1, context.getRank());
400
401 ITmfEvent event = fExperiment.getNext(context);
402 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
403 assertEquals("Context rank", 2, context.getRank());
404
405 // Position trace at event rank 9 (TS = 10)
406 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
407 assertEquals("Context rank", 9, context.getRank());
408
409 event = fExperiment.getNext(context);
410 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
411 assertEquals("Context rank", 10, context.getRank());
412
413 // Position trace at event rank 999 (TS = 1000)
414 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
415 assertEquals("Context rank", 999, context.getRank());
416
417 event = fExperiment.getNext(context);
418 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
419 assertEquals("Context rank", 1000, context.getRank());
420
421 // Position trace at event rank 1001 (TS = 1002)
422 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
423 assertEquals("Context rank", 1001, context.getRank());
424
425 event = fExperiment.getNext(context);
426 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
427 assertEquals("Context rank", 1002, context.getRank());
428
429 // Position trace at event rank 4500 (TS = 4501)
430 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
431 assertEquals("Context rank", 4500, context.getRank());
432
433 event = fExperiment.getNext(context);
434 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
435 assertEquals("Context rank", 4501, context.getRank());
436 }
437
438 public void testSeekTimestampOutOfScope() throws Exception {
439
440 // Position trace at beginning
441 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
442 assertEquals("Event rank", 0, context.getRank());
443
444 ITmfEvent event = fExperiment.getNext(context);
445 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
446 assertEquals("Event rank", 1, context.getRank());
447
448 // Position trace at event passed the end
449 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
450 event = fExperiment.getNext(context);
451 assertNull("Event location", event);
452 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
453 }
454
455 // ------------------------------------------------------------------------
456 // seekEvent by location (context rank is undefined)
457 // ------------------------------------------------------------------------
458
459 public void testSeekLocationOnCacheBoundary() throws Exception {
460
461 long cacheSize = fExperiment.getCacheSize();
462
463 // Position trace at event rank 0
464 ITmfContext tmpContext = fExperiment.seekEvent(0);
465 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
466
467 ITmfEvent event = fExperiment.getNext(context);
468 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
469
470 event = fExperiment.getNext(context);
471 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
472
473 // Position trace at event rank 'cacheSize'
474 tmpContext = fExperiment.seekEvent(cacheSize);
475 context = fExperiment.seekEvent(tmpContext.getLocation());
476
477 event = fExperiment.getNext(context);
478 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
479
480 event = fExperiment.getNext(context);
481 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
482
483 // Position trace at event rank 4 * 'cacheSize'
484 tmpContext = fExperiment.seekEvent(4 * cacheSize);
485 context = fExperiment.seekEvent(tmpContext.getLocation());
486
487 event = fExperiment.getNext(context);
488 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
489
490 event = fExperiment.getNext(context);
491 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
492 }
493
494 public void testSeekLocationNotOnCacheBoundary() throws Exception {
495
496 long cacheSize = fExperiment.getCacheSize();
497
498 // Position trace at event 'cacheSize' - 1
499 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
500 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
501
502 ITmfEvent event = fExperiment.getNext(context);
503 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
504
505 event = fExperiment.getNext(context);
506 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
507
508 // Position trace at event rank 2 * 'cacheSize' - 1
509 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
510 context = fExperiment.seekEvent(tmpContext.getLocation());
511 context = fExperiment.seekEvent(2 * cacheSize - 1);
512
513 event = fExperiment.getNext(context);
514 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
515
516 event = fExperiment.getNext(context);
517 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
518
519 // Position trace at event rank 4500
520 tmpContext = fExperiment.seekEvent(4500);
521 context = fExperiment.seekEvent(tmpContext.getLocation());
522
523 event = fExperiment.getNext(context);
524 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
525
526 event = fExperiment.getNext(context);
527 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
528 }
529
530 public void testSeekLocationOutOfScope() throws Exception {
531
532 // Position trace at beginning
533 ITmfContext context = fExperiment.seekEvent((ITmfLocation<?>) null);
534
535 ITmfEvent event = fExperiment.getNext(context);
536 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
537 }
538
539 // ------------------------------------------------------------------------
540 // readtNextEvent - updates the context
541 // ------------------------------------------------------------------------
542
543 public void testReadNextEvent() throws Exception {
544
545 // On lower bound, returns the first event (ts = 0)
546 final ITmfContext context = fExperiment.seekEvent(0);
547 ITmfEvent event = fExperiment.getNext(context);
548 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
549
550 for (int i = 2; i < 20; i++) {
551 event = fExperiment.getNext(context);
552 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
553 }
554 }
555
556 // ------------------------------------------------------------------------
557 // processRequest
558 // ------------------------------------------------------------------------
559
560 public void testProcessRequestForNbEvents() throws Exception {
561
562 final int blockSize = 100;
563 final int nbEvents = 1000;
564 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
565
566 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
567 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
568 @Override
569 public void handleData(final TmfEvent event) {
570 super.handleData(event);
571 requestedEvents.add(event);
572 }
573 };
574 fExperiment.sendRequest(request);
575 request.waitForCompletion();
576
577 assertEquals("nbEvents", nbEvents, requestedEvents.size());
578 assertTrue("isCompleted", request.isCompleted());
579 assertFalse("isCancelled", request.isCancelled());
580
581 // Ensure that we have distinct events.
582 // Don't go overboard: we are not validating the stub!
583 for (int i = 0; i < nbEvents; i++) {
584 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
585 }
586 }
587
588 public void testProcessRequestForNbEvents2() throws Exception {
589
590 final int blockSize = 2 * NB_EVENTS;
591 final int nbEvents = 1000;
592 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
593
594 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
595 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
596 @Override
597 public void handleData(final TmfEvent event) {
598 super.handleData(event);
599 requestedEvents.add(event);
600 }
601 };
602 fExperiment.sendRequest(request);
603 request.waitForCompletion();
604
605 assertEquals("nbEvents", nbEvents, requestedEvents.size());
606 assertTrue("isCompleted", request.isCompleted());
607 assertFalse("isCancelled", request.isCancelled());
608
609 // Ensure that we have distinct events.
610 // Don't go overboard: we are not validating the stub!
611 for (int i = 0; i < nbEvents; i++) {
612 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
613 }
614 }
615
616 public void testProcessRequestForAllEvents() throws Exception {
617
618 final int nbEvents = TmfEventRequest.ALL_DATA;
619 final int blockSize = 1;
620 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
621 final long nbExpectedEvents = NB_EVENTS;
622
623 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
624 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
625 @Override
626 public void handleData(final TmfEvent event) {
627 super.handleData(event);
628 requestedEvents.add(event);
629 }
630 };
631 fExperiment.sendRequest(request);
632 request.waitForCompletion();
633
634 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
635 assertTrue("isCompleted", request.isCompleted());
636 assertFalse("isCancelled", request.isCancelled());
637
638 // Ensure that we have distinct events.
639 // Don't go overboard: we are not validating the stub!
640 for (int i = 0; i < nbExpectedEvents; i++) {
641 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
642 }
643 }
644
645 // ------------------------------------------------------------------------
646 // cancel
647 // ------------------------------------------------------------------------
648
649 public void testCancel() throws Exception {
650
651 final int nbEvents = NB_EVENTS;
652 final int blockSize = BLOCK_SIZE;
653 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
654
655 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
656 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
657 int nbRead = 0;
658 @Override
659 public void handleData(final TmfEvent event) {
660 super.handleData(event);
661 requestedEvents.add(event);
662 if (++nbRead == blockSize) {
663 cancel();
664 }
665 }
666 @Override
667 public void handleCancel() {
668 if (requestedEvents.size() < blockSize) {
669 System.out.println("aie");
670 }
671 }
672 };
673 fExperiment.sendRequest(request);
674 request.waitForCompletion();
675
676 assertEquals("nbEvents", blockSize, requestedEvents.size());
677 assertTrue("isCompleted", request.isCompleted());
678 assertTrue("isCancelled", request.isCancelled());
679 }
680
681 }
This page took 0.091008 seconds and 5 git commands to generate.