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