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