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