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