Refactor TmfTrace and dependencies - move parseEvent to ITmfEventParser
[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 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.Vector;
20
21 import junit.framework.TestCase;
22
23 import org.eclipse.core.runtime.FileLocator;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
26 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
27 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
28 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
29 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
30 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperimentContext;
31 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
32 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
34 import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
35 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
36 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
37
38 /**
39 * <b><u>TmfExperimentTest</u></b>
40 * <p>
41 * TODO: Implement me. Please.
42 */
43 @SuppressWarnings("nls")
44 public class TmfExperimentTest extends TestCase {
45
46 private static final String DIRECTORY = "testfiles";
47 private static final String TEST_STREAM = "A-Test-10K";
48 private static final String EXPERIMENT = "MyExperiment";
49 private static int NB_EVENTS = 10000;
50 private static int fDefaultBlockSize = 1000;
51
52 private static ITmfTrace<?>[] fTraces;
53 private static TmfExperiment<TmfEvent> fExperiment;
54
55 private static byte SCALE = (byte) -3;
56
57 // ------------------------------------------------------------------------
58 // Housekeeping
59 // ------------------------------------------------------------------------
60
61 private synchronized ITmfTrace<?>[] setupTrace(final String path) {
62 if (fTraces == null) {
63 fTraces = new ITmfTrace[1];
64 try {
65 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
66 final File test = new File(FileLocator.toFileURL(location).toURI());
67 final TmfTraceStub trace = new TmfTraceStub(test.getPath(), true);
68 fTraces[0] = trace;
69 } catch (final URISyntaxException e) {
70 e.printStackTrace();
71 } catch (final IOException e) {
72 e.printStackTrace();
73 }
74 }
75 return fTraces;
76 }
77
78 @SuppressWarnings("unchecked")
79 private synchronized void setupExperiment() {
80 if (fExperiment == null)
81 fExperiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, (ITmfTrace<TmfEvent>[]) fTraces);
82 // fExperiment.indexExperiment();
83 }
84
85 public TmfExperimentTest(final String name) throws Exception {
86 super(name);
87 }
88
89 @Override
90 protected void setUp() throws Exception {
91 super.setUp();
92 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
93 setupExperiment();
94 }
95
96 @Override
97 protected void tearDown() throws Exception {
98 super.tearDown();
99 }
100
101 // ------------------------------------------------------------------------
102 // Constructor
103 // ------------------------------------------------------------------------
104
105 public void testBasicTmfExperimentConstructor() {
106 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
107 assertEquals("GetEpoch", TmfTimestamp.ZERO, fExperiment.getEpoch());
108 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
109
110 final TmfTimeRange timeRange = fExperiment.getTimeRange();
111 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
112 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
113 }
114
115 // ------------------------------------------------------------------------
116 // Verify checkpoints
117 // Note: seekLocation() does not reliably set the rank
118 // ------------------------------------------------------------------------
119
120 public void testValidateCheckpoints() throws Exception {
121
122 final Vector<TmfCheckpoint> checkpoints = fExperiment.getCheckpoints();
123 // int pageSize = fExperiment.getCacheSize();
124 assertTrue("Checkpoints exist", checkpoints != null);
125
126 // Validate that each checkpoint points to the right event
127 for (int i = 0; i < checkpoints.size(); i++) {
128 final TmfCheckpoint checkpoint = checkpoints.get(i);
129 final TmfExperimentContext context = fExperiment.seekEvent(checkpoint.getLocation());
130 final ITmfEvent event = fExperiment.parseEvent(context);
131 // assertEquals("Event rank", context.getRank(), i * pageSize);
132 assertTrue("Timestamp", (checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
133 }
134 }
135
136 // ------------------------------------------------------------------------
137 // parseEvent - make sure parseEvent doesn't update the context
138 // ------------------------------------------------------------------------
139
140 public void testParseEvent() throws Exception {
141
142 // On lower bound, returns the first event (ts = 0)
143 final TmfContext context = fExperiment.seekEvent(new TmfTimestamp(0, SCALE, 0));
144
145 ITmfEvent event = fExperiment.parseEvent(context);
146 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
147
148 event = fExperiment.parseEvent(context);
149 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
150
151 event = fExperiment.parseEvent(context);
152 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
153
154 for (int i = 1; i < 20; i++) {
155 event = fExperiment.readEvent(context);
156 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
157 }
158
159 event = fExperiment.parseEvent(context);
160 assertEquals("Event timestamp", 20, event.getTimestamp().getValue());
161
162 event = fExperiment.parseEvent(context);
163 assertEquals("Event timestamp", 20, event.getTimestamp().getValue());
164 }
165
166 // ------------------------------------------------------------------------
167 // getNextEvent - updates the context
168 // ------------------------------------------------------------------------
169
170 public void testGetNextEvent() throws Exception {
171
172 // On lower bound, returns the first event (ts = 0)
173 final TmfContext context = fExperiment.seekEvent(new TmfTimestamp(0, SCALE, 0));
174 ITmfEvent event = fExperiment.readEvent(context);
175 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
176
177 for (int i = 2; i < 20; i++) {
178 event = fExperiment.readEvent(context);
179 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
180 }
181 }
182
183 // ------------------------------------------------------------------------
184 // seekLocation
185 // Note: seekLocation() does not reliably set the rank
186 // ------------------------------------------------------------------------
187
188 public void testSeekLocationOnCacheBoundary() throws Exception {
189
190 // Position trace at event rank 0
191 TmfContext context = fExperiment.seekEvent(0);
192 // assertEquals("Event rank", 0, context.getRank());
193 ITmfEvent event = fExperiment.parseEvent(context);
194 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
195 // assertEquals("Event rank", 0, context.getRank());
196 event = fExperiment.readEvent(context);
197 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
198 // assertEquals("Event rank", 1, context.getRank());
199
200 // Position trace at event rank 1000
201 TmfContext tmpContext = fExperiment.seekEvent(new TmfTimestamp(1001, SCALE, 0));
202 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
203 // assertEquals("Event rank", 1000, context.getRank());
204 event = fExperiment.parseEvent(context);
205 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
206 // assertEquals("Event rank", 1000, context.getRank());
207 event = fExperiment.readEvent(context);
208 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
209 // assertEquals("Event rank", 1001, context.getRank());
210
211 // Position trace at event rank 4000
212 tmpContext = fExperiment.seekEvent(new TmfTimestamp(4001, SCALE, 0));
213 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
214 // assertEquals("Event rank", 4000, context.getRank());
215 event = fExperiment.parseEvent(context);
216 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
217 // assertEquals("Event rank", 4000, context.getRank());
218 event = fExperiment.readEvent(context);
219 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
220 // assertEquals("Event rank", 4001, context.getRank());
221 }
222
223 public void testSeekLocationNotOnCacheBoundary() throws Exception {
224
225 // Position trace at event rank 9
226 TmfContext tmpContext = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
227 TmfContext context = fExperiment.seekEvent(tmpContext.getLocation().clone());
228 // assertEquals("Event rank", 9, context.getRank());
229 ITmfEvent event = fExperiment.parseEvent(context);
230 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
231 // assertEquals("Event rank", 9, context.getRank());
232 event = fExperiment.readEvent(context);
233 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
234 // assertEquals("Event rank", 10, context.getRank());
235
236 // Position trace at event rank 999
237 tmpContext = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
238 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
239 // assertEquals("Event rank", 999, context.getRank());
240 event = fExperiment.parseEvent(context);
241 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
242 // assertEquals("Event rank", 999, context.getRank());
243 event = fExperiment.readEvent(context);
244 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
245 // assertEquals("Event rank", 1000, context.getRank());
246
247 // Position trace at event rank 1001
248 tmpContext = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
249 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
250 // assertEquals("Event rank", 1001, context.getRank());
251 event = fExperiment.parseEvent(context);
252 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
253 // assertEquals("Event rank", 1001, context.getRank());
254 event = fExperiment.readEvent(context);
255 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
256 // assertEquals("Event rank", 1002, context.getRank());
257
258 // Position trace at event rank 4500
259 tmpContext = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
260 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
261 // assertEquals("Event rank", 4500, context.getRank());
262 event = fExperiment.parseEvent(context);
263 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
264 // assertEquals("Event rank", 4500, context.getRank());
265 event = fExperiment.readEvent(context);
266 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
267 // assertEquals("Event rank", 4501, context.getRank());
268 }
269
270 public void testSeekLocationOutOfScope() throws Exception {
271
272 // Position trace at beginning
273 TmfContext tmpContext = fExperiment.seekEvent(0);
274 TmfContext context = fExperiment.seekEvent(tmpContext.getLocation().clone());
275 // assertEquals("Event rank", 0, context.getRank());
276 ITmfEvent event = fExperiment.parseEvent(context);
277 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
278 // assertEquals("Event rank", 0, context.getRank());
279 event = fExperiment.readEvent(context);
280 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
281 // assertEquals("Event rank", 1, context.getRank());
282
283 // Position trace at event passed the end
284 tmpContext = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
285 context = fExperiment.seekEvent(tmpContext.getLocation().clone());
286 // assertEquals("Event rank", NB_EVENTS, context.getRank());
287 event = fExperiment.parseEvent(context);
288 assertEquals("Event timestamp", null, event);
289 // assertEquals("Event rank", NB_EVENTS, context.getRank());
290 event = fExperiment.readEvent(context);
291 assertEquals("Event timestamp", null, event);
292 // assertEquals("Event rank", NB_EVENTS, context.getRank());
293 }
294
295 // ------------------------------------------------------------------------
296 // seekEvent on timestamp
297 // ------------------------------------------------------------------------
298
299 public void testSeekEventOnTimestampOnCacheBoundary() throws Exception {
300
301 // Position trace at event rank 0
302 TmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
303 assertEquals("Event rank", 0, context.getRank());
304 ITmfEvent event = fExperiment.parseEvent(context);
305 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
306 assertEquals("Event rank", 0, context.getRank());
307 event = fExperiment.readEvent(context);
308 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
309 assertEquals("Event rank", 1, context.getRank());
310
311 // Position trace at event rank 1000
312 context = fExperiment.seekEvent(new TmfTimestamp(1001, SCALE, 0));
313 assertEquals("Event rank", 1000, context.getRank());
314 event = fExperiment.parseEvent(context);
315 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
316 assertEquals("Event rank", 1000, context.getRank());
317 event = fExperiment.readEvent(context);
318 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
319 assertEquals("Event rank", 1001, context.getRank());
320
321 // Position trace at event rank 4000
322 context = fExperiment.seekEvent(new TmfTimestamp(4001, SCALE, 0));
323 assertEquals("Event rank", 4000, context.getRank());
324 event = fExperiment.parseEvent(context);
325 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
326 assertEquals("Event rank", 4000, context.getRank());
327 event = fExperiment.readEvent(context);
328 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
329 assertEquals("Event rank", 4001, context.getRank());
330 }
331
332 public void testSeekEventOnTimestampNotOnCacheBoundary() throws Exception {
333
334 // Position trace at event rank 1
335 TmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
336 assertEquals("Event rank", 1, context.getRank());
337 ITmfEvent event = fExperiment.parseEvent(context);
338 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
339 assertEquals("Event rank", 1, context.getRank());
340 event = fExperiment.readEvent(context);
341 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
342 assertEquals("Event rank", 2, context.getRank());
343
344 // Position trace at event rank 9
345 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
346 assertEquals("Event rank", 9, context.getRank());
347 event = fExperiment.parseEvent(context);
348 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
349 assertEquals("Event rank", 9, context.getRank());
350 event = fExperiment.readEvent(context);
351 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
352 assertEquals("Event rank", 10, context.getRank());
353
354 // Position trace at event rank 999
355 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
356 assertEquals("Event rank", 999, context.getRank());
357 event = fExperiment.parseEvent(context);
358 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
359 assertEquals("Event rank", 999, context.getRank());
360 event = fExperiment.readEvent(context);
361 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
362 assertEquals("Event rank", 1000, context.getRank());
363
364 // Position trace at event rank 1001
365 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
366 assertEquals("Event rank", 1001, context.getRank());
367 event = fExperiment.parseEvent(context);
368 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
369 assertEquals("Event rank", 1001, context.getRank());
370 event = fExperiment.readEvent(context);
371 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
372 assertEquals("Event rank", 1002, context.getRank());
373
374 // Position trace at event rank 4500
375 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
376 assertEquals("Event rank", 4500, context.getRank());
377 event = fExperiment.parseEvent(context);
378 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
379 assertEquals("Event rank", 4500, context.getRank());
380 event = fExperiment.readEvent(context);
381 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
382 assertEquals("Event rank", 4501, context.getRank());
383 }
384
385 public void testSeekEventOnTimestampoutOfScope() throws Exception {
386
387 // Position trace at beginning
388 TmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
389 assertEquals("Event rank", 0, context.getRank());
390 ITmfEvent event = fExperiment.parseEvent(context);
391 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
392 assertEquals("Event rank", 0, context.getRank());
393 event = fExperiment.readEvent(context);
394 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
395 assertEquals("Event rank", 1, context.getRank());
396
397 // Position trace at event passed the end
398 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
399 assertEquals("Event rank", NB_EVENTS, context.getRank());
400 event = fExperiment.parseEvent(context);
401 assertEquals("Event timestamp", null, event);
402 assertEquals("Event rank", NB_EVENTS, context.getRank());
403 event = fExperiment.readEvent(context);
404 assertEquals("Event timestamp", null, event);
405 assertEquals("Event rank", NB_EVENTS, context.getRank());
406 }
407
408 // ------------------------------------------------------------------------
409 // seekEvent on rank
410 // ------------------------------------------------------------------------
411
412 public void testSeekOnRankOnCacheBoundary() throws Exception {
413
414 // On lower bound, returns the first event (ts = 1)
415 TmfContext context = fExperiment.seekEvent(0);
416 assertEquals("Event rank", 0, context.getRank());
417 ITmfEvent event = fExperiment.parseEvent(context);
418 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
419 assertEquals("Event rank", 0, context.getRank());
420 event = fExperiment.readEvent(context);
421 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
422 assertEquals("Event rank", 1, context.getRank());
423
424 // Position trace at event rank 1000
425 context = fExperiment.seekEvent(1000);
426 assertEquals("Event rank", 1000, context.getRank());
427 event = fExperiment.parseEvent(context);
428 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
429 assertEquals("Event rank", 1000, context.getRank());
430 event = fExperiment.readEvent(context);
431 assertEquals("Event timestamp", 1001, event.getTimestamp().getValue());
432 assertEquals("Event rank", 1001, context.getRank());
433
434 // Position trace at event rank 4000
435 context = fExperiment.seekEvent(4000);
436 assertEquals("Event rank", 4000, context.getRank());
437 event = fExperiment.parseEvent(context);
438 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
439 assertEquals("Event rank", 4000, context.getRank());
440 event = fExperiment.readEvent(context);
441 assertEquals("Event timestamp", 4001, event.getTimestamp().getValue());
442 assertEquals("Event rank", 4001, context.getRank());
443 }
444
445 public void testSeekOnRankNotOnCacheBoundary() throws Exception {
446
447 // Position trace at event rank 9
448 TmfContext context = fExperiment.seekEvent(9);
449 assertEquals("Event rank", 9, context.getRank());
450 ITmfEvent event = fExperiment.parseEvent(context);
451 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
452 assertEquals("Event rank", 9, context.getRank());
453 event = fExperiment.readEvent(context);
454 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
455 assertEquals("Event rank", 10, context.getRank());
456
457 // Position trace at event rank 999
458 context = fExperiment.seekEvent(999);
459 assertEquals("Event rank", 999, context.getRank());
460 event = fExperiment.parseEvent(context);
461 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
462 assertEquals("Event rank", 999, context.getRank());
463 event = fExperiment.readEvent(context);
464 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
465 assertEquals("Event rank", 1000, context.getRank());
466
467 // Position trace at event rank 1001
468 context = fExperiment.seekEvent(1001);
469 assertEquals("Event rank", 1001, context.getRank());
470 event = fExperiment.parseEvent(context);
471 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
472 assertEquals("Event rank", 1001, context.getRank());
473 event = fExperiment.readEvent(context);
474 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
475 assertEquals("Event rank", 1002, context.getRank());
476
477 // Position trace at event rank 4500
478 context = fExperiment.seekEvent(4500);
479 assertEquals("Event rank", 4500, context.getRank());
480 event = fExperiment.parseEvent(context);
481 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
482 assertEquals("Event rank", 4500, context.getRank());
483 event = fExperiment.readEvent(context);
484 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
485 assertEquals("Event rank", 4501, context.getRank());
486 }
487
488 public void testSeekEventOnRankOfScope() throws Exception {
489
490 // Position trace at beginning
491 TmfContext context = fExperiment.seekEvent(-1);
492 assertEquals("Event rank", 0, context.getRank());
493 ITmfEvent event = fExperiment.parseEvent(context);
494 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
495 assertEquals("Event rank", 0, context.getRank());
496 event = fExperiment.readEvent(context);
497 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
498 assertEquals("Event rank", 1, context.getRank());
499
500 // Position trace at event passed the end
501 context = fExperiment.seekEvent(NB_EVENTS);
502 assertEquals("Event rank", NB_EVENTS, context.getRank());
503 event = fExperiment.parseEvent(context);
504 assertEquals("Event timestamp", null, event);
505 assertEquals("Event rank", NB_EVENTS, context.getRank());
506 event = fExperiment.readEvent(context);
507 assertEquals("Event timestamp", null, event);
508 assertEquals("Event rank", NB_EVENTS, context.getRank());
509 }
510
511 // ------------------------------------------------------------------------
512 // processRequest
513 // ------------------------------------------------------------------------
514
515 public void testProcessRequestForNbEvents() throws Exception {
516 final int blockSize = 100;
517 final int nbEvents = 1000;
518 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
519
520 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
521 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
522 @Override
523 public void handleData(final TmfEvent event) {
524 super.handleData(event);
525 requestedEvents.add(event);
526 }
527 };
528 fExperiment.sendRequest(request);
529 request.waitForCompletion();
530
531 assertEquals("nbEvents", nbEvents, requestedEvents.size());
532 assertTrue("isCompleted", request.isCompleted());
533 assertFalse("isCancelled", request.isCancelled());
534
535 // Ensure that we have distinct events.
536 // Don't go overboard: we are not validating the stub!
537 for (int i = 0; i < nbEvents; i++)
538 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
539 }
540
541 public void testProcessRequestForNbEvents2() throws Exception {
542 final int blockSize = 2 * NB_EVENTS;
543 final int nbEvents = 1000;
544 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
545
546 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
547 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
548 @Override
549 public void handleData(final TmfEvent event) {
550 super.handleData(event);
551 requestedEvents.add(event);
552 }
553 };
554 fExperiment.sendRequest(request);
555 request.waitForCompletion();
556
557 assertEquals("nbEvents", nbEvents, requestedEvents.size());
558 assertTrue("isCompleted", request.isCompleted());
559 assertFalse("isCancelled", request.isCancelled());
560
561 // Ensure that we have distinct events.
562 // Don't go overboard: we are not validating the stub!
563 for (int i = 0; i < nbEvents; i++)
564 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
565 }
566
567 public void testProcessRequestForAllEvents() throws Exception {
568 final int nbEvents = TmfEventRequest.ALL_DATA;
569 final int blockSize = 1;
570 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
571 final long nbExpectedEvents = fExperiment.getNbEvents();
572
573 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
574 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
575 @Override
576 public void handleData(final TmfEvent event) {
577 super.handleData(event);
578 requestedEvents.add(event);
579 }
580 };
581 fExperiment.sendRequest(request);
582 request.waitForCompletion();
583
584 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
585 assertTrue("isCompleted", request.isCompleted());
586 assertFalse("isCancelled", request.isCancelled());
587
588 // Ensure that we have distinct events.
589 // Don't go overboard: we are not validating the stub!
590 for (int i = 0; i < nbExpectedEvents; i++)
591 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
592 }
593
594 // ------------------------------------------------------------------------
595 // cancel
596 // ------------------------------------------------------------------------
597
598 public void testCancel() throws Exception {
599 final int nbEvents = NB_EVENTS;
600 final int blockSize = fDefaultBlockSize;
601 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
602
603 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
604 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
605 int nbRead = 0;
606 @Override
607 public void handleData(final TmfEvent event) {
608 super.handleData(event);
609 requestedEvents.add(event);
610 if (++nbRead == blockSize)
611 cancel();
612 }
613 };
614 fExperiment.sendRequest(request);
615 request.waitForCompletion();
616
617 assertEquals("nbEvents", blockSize, requestedEvents.size());
618 assertTrue("isCompleted", request.isCompleted());
619 assertTrue("isCancelled", request.isCancelled());
620 }
621
622 // ------------------------------------------------------------------------
623 // getRank
624 // ------------------------------------------------------------------------
625
626 // public void testGetRank() throws Exception {
627 // assertEquals("getRank", 0, fExperiment.getRank(new TmfTimestamp()));
628 // assertEquals("getRank", 0, fExperiment.getRank(new TmfTimestamp( 1, (byte) -3)));
629 // assertEquals("getRank", 10, fExperiment.getRank(new TmfTimestamp( 11, (byte) -3)));
630 // assertEquals("getRank", 100, fExperiment.getRank(new TmfTimestamp( 101, (byte) -3)));
631 // assertEquals("getRank", 1000, fExperiment.getRank(new TmfTimestamp(1001, (byte) -3)));
632 // assertEquals("getRank", 2000, fExperiment.getRank(new TmfTimestamp(2001, (byte) -3)));
633 // assertEquals("getRank", 2500, fExperiment.getRank(new TmfTimestamp(2501, (byte) -3)));
634 // }
635
636 // ------------------------------------------------------------------------
637 // getTimestamp
638 // ------------------------------------------------------------------------
639
640 public void testGetTimestamp() throws Exception {
641 assertTrue("getTimestamp", fExperiment.getTimestamp( 0).equals(new TmfTimestamp( 1, (byte) -3)));
642 assertTrue("getTimestamp", fExperiment.getTimestamp( 10).equals(new TmfTimestamp( 11, (byte) -3)));
643 assertTrue("getTimestamp", fExperiment.getTimestamp( 100).equals(new TmfTimestamp( 101, (byte) -3)));
644 assertTrue("getTimestamp", fExperiment.getTimestamp(1000).equals(new TmfTimestamp(1001, (byte) -3)));
645 assertTrue("getTimestamp", fExperiment.getTimestamp(2000).equals(new TmfTimestamp(2001, (byte) -3)));
646 assertTrue("getTimestamp", fExperiment.getTimestamp(2500).equals(new TmfTimestamp(2501, (byte) -3)));
647 }
648
649 }
This page took 0.051948 seconds and 6 git commands to generate.