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