| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 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 | * Francois Chouinard - Adjusted for new Trace Model |
| 12 | * Alexandre Montplaisir - Port to JUnit4 |
| 13 | * Patrick Tasse - Fix for concurrency |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.IOException; |
| 25 | import java.net.URISyntaxException; |
| 26 | import java.net.URL; |
| 27 | import java.util.Vector; |
| 28 | |
| 29 | import org.eclipse.core.runtime.FileLocator; |
| 30 | import org.eclipse.core.runtime.Path; |
| 31 | import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext; |
| 32 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 33 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 34 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 35 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType; |
| 36 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 37 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
| 38 | import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace; |
| 39 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 40 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 41 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 42 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 43 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 44 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; |
| 45 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; |
| 46 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
| 47 | import org.junit.AfterClass; |
| 48 | import org.junit.BeforeClass; |
| 49 | import org.junit.Test; |
| 50 | |
| 51 | /** |
| 52 | * Test suite for the TmfExperiment class (multiple traces). |
| 53 | */ |
| 54 | @SuppressWarnings("javadoc") |
| 55 | public class TmfMultiTraceExperimentTest { |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // Attributes |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | private static final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec |
| 62 | private static final String EXPERIMENT = "MyExperiment"; |
| 63 | private static int NB_EVENTS = 20000; |
| 64 | private static int BLOCK_SIZE = 1000; |
| 65 | |
| 66 | private static TmfExperimentStub fExperiment; |
| 67 | |
| 68 | private static byte SCALE = (byte) -3; |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | // Housekeeping |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | @BeforeClass |
| 75 | public static void setUp() { |
| 76 | ITmfTrace[] traces = setupTraces(); |
| 77 | fExperiment = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE); |
| 78 | fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); |
| 79 | } |
| 80 | |
| 81 | @AfterClass |
| 82 | public static void tearDown() { |
| 83 | fExperiment.dispose(); |
| 84 | } |
| 85 | |
| 86 | private static ITmfTrace[] setupTraces() { |
| 87 | try { |
| 88 | ITmfTrace[] traces = new ITmfTrace[2]; |
| 89 | |
| 90 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TmfTestTrace.O_TEST_10K.getFullPath()), null); |
| 91 | File test = new File(FileLocator.toFileURL(location).toURI()); |
| 92 | final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true, null); |
| 93 | traces[0] = trace1; |
| 94 | |
| 95 | location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TmfTestTrace.E_TEST_10K.getFullPath()), null); |
| 96 | test = new File(FileLocator.toFileURL(location).toURI()); |
| 97 | final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true, null); |
| 98 | traces[1] = trace2; |
| 99 | |
| 100 | return traces; |
| 101 | } catch (final TmfTraceException e) { |
| 102 | e.printStackTrace(); |
| 103 | } catch (final URISyntaxException e) { |
| 104 | e.printStackTrace(); |
| 105 | } catch (final IOException e) { |
| 106 | e.printStackTrace(); |
| 107 | } |
| 108 | return new ITmfTrace[0]; |
| 109 | } |
| 110 | |
| 111 | // ------------------------------------------------------------------------ |
| 112 | // Constructor |
| 113 | // ------------------------------------------------------------------------ |
| 114 | |
| 115 | @Test |
| 116 | public void testBasicTmfExperimentConstructor() { |
| 117 | assertEquals("GetId", EXPERIMENT, fExperiment.getName()); |
| 118 | assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents()); |
| 119 | |
| 120 | final TmfTimeRange timeRange = fExperiment.getTimeRange(); |
| 121 | assertEquals("getStartTime", 1, timeRange.getStartTime().getValue()); |
| 122 | assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue()); |
| 123 | |
| 124 | TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); |
| 125 | assertEquals("getInitialRangeOffset", initRange, fExperiment.getInitialRangeOffset()); |
| 126 | } |
| 127 | |
| 128 | // ------------------------------------------------------------------------ |
| 129 | // seekEvent on rank |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | @Test |
| 133 | public void testSeekRankOnCacheBoundary() { |
| 134 | long cacheSize = fExperiment.getCacheSize(); |
| 135 | |
| 136 | // On lower bound, returns the first event (TS = 1) |
| 137 | ITmfContext context = fExperiment.seekEvent(0); |
| 138 | assertEquals("Context rank", 0, context.getRank()); |
| 139 | |
| 140 | ITmfEvent event = fExperiment.getNext(context); |
| 141 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 142 | assertEquals("Context rank", 1, context.getRank()); |
| 143 | |
| 144 | // Position trace at event rank [cacheSize] |
| 145 | context = fExperiment.seekEvent(cacheSize); |
| 146 | assertEquals("Context rank", cacheSize, context.getRank()); |
| 147 | |
| 148 | event = fExperiment.getNext(context); |
| 149 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
| 150 | assertEquals("Context rank", cacheSize + 1, context.getRank()); |
| 151 | |
| 152 | // Position trace at event rank [4 * cacheSize] |
| 153 | context = fExperiment.seekEvent(4 * cacheSize); |
| 154 | assertEquals("Context rank", 4 * cacheSize, context.getRank()); |
| 155 | |
| 156 | event = fExperiment.getNext(context); |
| 157 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
| 158 | assertEquals("Context rank", 4 * cacheSize + 1, context.getRank()); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testSeekRankNotOnCacheBoundary() { |
| 163 | long cacheSize = fExperiment.getCacheSize(); |
| 164 | |
| 165 | // Position trace at event rank 9 |
| 166 | ITmfContext context = fExperiment.seekEvent(9); |
| 167 | assertEquals("Context rank", 9, context.getRank()); |
| 168 | |
| 169 | ITmfEvent event = fExperiment.getNext(context); |
| 170 | assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); |
| 171 | assertEquals("Context rank", 10, context.getRank()); |
| 172 | |
| 173 | // Position trace at event rank [cacheSize - 1] |
| 174 | context = fExperiment.seekEvent(cacheSize - 1); |
| 175 | assertEquals("Context rank", cacheSize - 1, context.getRank()); |
| 176 | |
| 177 | event = fExperiment.getNext(context); |
| 178 | assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue()); |
| 179 | assertEquals("Context rank", cacheSize, context.getRank()); |
| 180 | |
| 181 | // Position trace at event rank [cacheSize + 1] |
| 182 | context = fExperiment.seekEvent(cacheSize + 1); |
| 183 | assertEquals("Context rank", cacheSize + 1, context.getRank()); |
| 184 | |
| 185 | event = fExperiment.getNext(context); |
| 186 | assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue()); |
| 187 | assertEquals("Context rank", cacheSize + 2, context.getRank()); |
| 188 | |
| 189 | // Position trace at event rank 4500 |
| 190 | context = fExperiment.seekEvent(4500); |
| 191 | assertEquals("Context rank", 4500, context.getRank()); |
| 192 | |
| 193 | event = fExperiment.getNext(context); |
| 194 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
| 195 | assertEquals("Context rank", 4501, context.getRank()); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testSeekRankOutOfScope() { |
| 200 | // Position trace at beginning |
| 201 | ITmfContext context = fExperiment.seekEvent(-1); |
| 202 | assertEquals("Event rank", 0, context.getRank()); |
| 203 | |
| 204 | ITmfEvent event = fExperiment.getNext(context); |
| 205 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 206 | assertEquals("Context rank", 1, context.getRank()); |
| 207 | |
| 208 | // Position trace at event passed the end |
| 209 | context = fExperiment.seekEvent(NB_EVENTS); |
| 210 | assertEquals("Context rank", NB_EVENTS, context.getRank()); |
| 211 | |
| 212 | event = fExperiment.getNext(context); |
| 213 | assertNull("Event", event); |
| 214 | assertEquals("Context rank", NB_EVENTS, context.getRank()); |
| 215 | } |
| 216 | |
| 217 | // ------------------------------------------------------------------------ |
| 218 | // seekEvent on timestamp |
| 219 | // ------------------------------------------------------------------------ |
| 220 | |
| 221 | @Test |
| 222 | public void testSeekTimestampOnCacheBoundary() { |
| 223 | long cacheSize = fExperiment.getCacheSize(); |
| 224 | |
| 225 | // Position trace at event rank 0 |
| 226 | ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0)); |
| 227 | assertEquals("Context rank", 0, context.getRank()); |
| 228 | |
| 229 | ITmfEvent event = fExperiment.getNext(context); |
| 230 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 231 | assertEquals("Context rank", 1, context.getRank()); |
| 232 | |
| 233 | // Position trace at event rank [cacheSize] |
| 234 | context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0)); |
| 235 | assertEquals("Event rank", cacheSize, context.getRank()); |
| 236 | |
| 237 | event = fExperiment.getNext(context); |
| 238 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
| 239 | assertEquals("Context rank", cacheSize + 1, context.getRank()); |
| 240 | |
| 241 | // Position trace at event rank [4 * cacheSize] |
| 242 | context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0)); |
| 243 | assertEquals("Context rank", 4 * cacheSize, context.getRank()); |
| 244 | |
| 245 | event = fExperiment.getNext(context); |
| 246 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
| 247 | assertEquals("Context rank", 4 * cacheSize + 1, context.getRank()); |
| 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void testSeekTimestampNotOnCacheBoundary() { |
| 252 | // Position trace at event rank 1 (TS = 2) |
| 253 | ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0)); |
| 254 | assertEquals("Context rank", 1, context.getRank()); |
| 255 | |
| 256 | ITmfEvent event = fExperiment.getNext(context); |
| 257 | assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); |
| 258 | assertEquals("Context rank", 2, context.getRank()); |
| 259 | |
| 260 | // Position trace at event rank 9 (TS = 10) |
| 261 | context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0)); |
| 262 | assertEquals("Context rank", 9, context.getRank()); |
| 263 | |
| 264 | event = fExperiment.getNext(context); |
| 265 | assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); |
| 266 | assertEquals("Context rank", 10, context.getRank()); |
| 267 | |
| 268 | // Position trace at event rank 999 (TS = 1000) |
| 269 | context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0)); |
| 270 | assertEquals("Context rank", 999, context.getRank()); |
| 271 | |
| 272 | event = fExperiment.getNext(context); |
| 273 | assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); |
| 274 | assertEquals("Context rank", 1000, context.getRank()); |
| 275 | |
| 276 | // Position trace at event rank 1001 (TS = 1002) |
| 277 | context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0)); |
| 278 | assertEquals("Context rank", 1001, context.getRank()); |
| 279 | |
| 280 | event = fExperiment.getNext(context); |
| 281 | assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); |
| 282 | assertEquals("Context rank", 1002, context.getRank()); |
| 283 | |
| 284 | // Position trace at event rank 4500 (TS = 4501) |
| 285 | context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0)); |
| 286 | assertEquals("Context rank", 4500, context.getRank()); |
| 287 | |
| 288 | event = fExperiment.getNext(context); |
| 289 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
| 290 | assertEquals("Context rank", 4501, context.getRank()); |
| 291 | } |
| 292 | |
| 293 | @Test |
| 294 | public void testSeekTimestampOutOfScope() { |
| 295 | // Position trace at beginning |
| 296 | ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0)); |
| 297 | assertEquals("Event rank", 0, context.getRank()); |
| 298 | |
| 299 | ITmfEvent event = fExperiment.getNext(context); |
| 300 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 301 | assertEquals("Event rank", 1, context.getRank()); |
| 302 | |
| 303 | // Position trace at event passed the end |
| 304 | context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0)); |
| 305 | event = fExperiment.getNext(context); |
| 306 | assertNull("Event location", event); |
| 307 | assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); |
| 308 | } |
| 309 | |
| 310 | // ------------------------------------------------------------------------ |
| 311 | // seekEvent by location (context rank is undefined) |
| 312 | // ------------------------------------------------------------------------ |
| 313 | |
| 314 | @Test |
| 315 | public void testSeekLocationOnCacheBoundary() { |
| 316 | long cacheSize = fExperiment.getCacheSize(); |
| 317 | |
| 318 | // Position trace at event rank 0 |
| 319 | ITmfContext tmpContext = fExperiment.seekEvent(0); |
| 320 | ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 321 | |
| 322 | ITmfEvent event = fExperiment.getNext(context); |
| 323 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 324 | |
| 325 | event = fExperiment.getNext(context); |
| 326 | assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); |
| 327 | |
| 328 | // Position trace at event rank 'cacheSize' |
| 329 | tmpContext = fExperiment.seekEvent(cacheSize); |
| 330 | context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 331 | |
| 332 | event = fExperiment.getNext(context); |
| 333 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
| 334 | |
| 335 | event = fExperiment.getNext(context); |
| 336 | assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue()); |
| 337 | |
| 338 | // Position trace at event rank 4 * 'cacheSize' |
| 339 | tmpContext = fExperiment.seekEvent(4 * cacheSize); |
| 340 | context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 341 | |
| 342 | event = fExperiment.getNext(context); |
| 343 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
| 344 | |
| 345 | event = fExperiment.getNext(context); |
| 346 | assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue()); |
| 347 | } |
| 348 | |
| 349 | @Test |
| 350 | public void testSeekLocationNotOnCacheBoundary() { |
| 351 | long cacheSize = fExperiment.getCacheSize(); |
| 352 | |
| 353 | // Position trace at event 'cacheSize' - 1 |
| 354 | ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1); |
| 355 | ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 356 | |
| 357 | ITmfEvent event = fExperiment.getNext(context); |
| 358 | assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue()); |
| 359 | |
| 360 | event = fExperiment.getNext(context); |
| 361 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
| 362 | |
| 363 | // Position trace at event rank 2 * 'cacheSize' - 1 |
| 364 | tmpContext = fExperiment.seekEvent(2 * cacheSize - 1); |
| 365 | context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 366 | context = fExperiment.seekEvent(2 * cacheSize - 1); |
| 367 | |
| 368 | event = fExperiment.getNext(context); |
| 369 | assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue()); |
| 370 | |
| 371 | event = fExperiment.getNext(context); |
| 372 | assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue()); |
| 373 | |
| 374 | // Position trace at event rank 4500 |
| 375 | tmpContext = fExperiment.seekEvent(4500); |
| 376 | context = fExperiment.seekEvent(tmpContext.getLocation()); |
| 377 | |
| 378 | event = fExperiment.getNext(context); |
| 379 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
| 380 | |
| 381 | event = fExperiment.getNext(context); |
| 382 | assertEquals("Event timestamp", 4502, event.getTimestamp().getValue()); |
| 383 | } |
| 384 | |
| 385 | @Test |
| 386 | public void testSeekLocationOutOfScope() { |
| 387 | // Position trace at beginning |
| 388 | ITmfContext context = fExperiment.seekEvent((ITmfLocation) null); |
| 389 | |
| 390 | ITmfEvent event = fExperiment.getNext(context); |
| 391 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
| 392 | } |
| 393 | |
| 394 | // ------------------------------------------------------------------------ |
| 395 | // getNext - updates the context |
| 396 | // ------------------------------------------------------------------------ |
| 397 | |
| 398 | private static void validateContextRanks(ITmfContext context) { |
| 399 | assertTrue("Experiment context type", context instanceof TmfExperimentContext); |
| 400 | TmfExperimentContext ctx = (TmfExperimentContext) context; |
| 401 | |
| 402 | int nbTraces = ctx.getContexts().length; |
| 403 | |
| 404 | // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE) |
| 405 | long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0); |
| 406 | for (int i = 0; i < nbTraces; i++) { |
| 407 | long rank = ctx.getContexts()[i].getRank(); |
| 408 | if (rank == -1) { |
| 409 | expRank = -1; |
| 410 | break; |
| 411 | } |
| 412 | expRank += rank; |
| 413 | } |
| 414 | assertEquals("Experiment context rank", expRank, ctx.getRank()); |
| 415 | } |
| 416 | |
| 417 | @Test |
| 418 | public void testGetNextAfteSeekingOnTS_1() { |
| 419 | final long INITIAL_TS = 1; |
| 420 | final int NB_READS = 20; |
| 421 | |
| 422 | // On lower bound, returns the first event (ts = 1) |
| 423 | final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); |
| 424 | |
| 425 | validateContextRanks(context); |
| 426 | |
| 427 | // Read NB_EVENTS |
| 428 | ITmfEvent event; |
| 429 | for (int i = 0; i < NB_READS; i++) { |
| 430 | event = fExperiment.getNext(context); |
| 431 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 432 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); |
| 433 | } |
| 434 | |
| 435 | // Make sure we stay positioned |
| 436 | event = fExperiment.parseEvent(context); |
| 437 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 438 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); |
| 439 | |
| 440 | validateContextRanks(context); |
| 441 | } |
| 442 | |
| 443 | @Test |
| 444 | public void testGetNextAfteSeekingOnTS_2() { |
| 445 | final long INITIAL_TS = 2; |
| 446 | final int NB_READS = 20; |
| 447 | |
| 448 | // On lower bound, returns the first event (ts = 2) |
| 449 | final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); |
| 450 | |
| 451 | validateContextRanks(context); |
| 452 | |
| 453 | // Read NB_EVENTS |
| 454 | ITmfEvent event; |
| 455 | for (int i = 0; i < NB_READS; i++) { |
| 456 | event = fExperiment.getNext(context); |
| 457 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 458 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); |
| 459 | } |
| 460 | |
| 461 | // Make sure we stay positioned |
| 462 | event = fExperiment.parseEvent(context); |
| 463 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 464 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); |
| 465 | |
| 466 | validateContextRanks(context); |
| 467 | } |
| 468 | |
| 469 | @Test |
| 470 | public void testGetNextAfteSeekingOnTS_3() { |
| 471 | final long INITIAL_TS = 500; |
| 472 | final int NB_READS = 20; |
| 473 | |
| 474 | // On lower bound, returns the first event (ts = 500) |
| 475 | final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0)); |
| 476 | |
| 477 | validateContextRanks(context); |
| 478 | |
| 479 | // Read NB_EVENTS |
| 480 | ITmfEvent event; |
| 481 | for (int i = 0; i < NB_READS; i++) { |
| 482 | event = fExperiment.getNext(context); |
| 483 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 484 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); |
| 485 | } |
| 486 | |
| 487 | // Make sure we stay positioned |
| 488 | event = fExperiment.parseEvent(context); |
| 489 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 490 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); |
| 491 | |
| 492 | validateContextRanks(context); |
| 493 | } |
| 494 | |
| 495 | @Test |
| 496 | public void testGetNextAfterSeekingOnRank_1() { |
| 497 | final long INITIAL_RANK = 0L; |
| 498 | final int NB_READS = 20; |
| 499 | |
| 500 | // On lower bound, returns the first event (rank = 0) |
| 501 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); |
| 502 | |
| 503 | validateContextRanks(context); |
| 504 | |
| 505 | // Read NB_EVENTS |
| 506 | ITmfEvent event; |
| 507 | for (int i = 0; i < NB_READS; i++) { |
| 508 | event = fExperiment.getNext(context); |
| 509 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); |
| 510 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); |
| 511 | } |
| 512 | |
| 513 | // Make sure we stay positioned |
| 514 | event = fExperiment.parseEvent(context); |
| 515 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); |
| 516 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); |
| 517 | |
| 518 | validateContextRanks(context); |
| 519 | } |
| 520 | |
| 521 | @Test |
| 522 | public void testGetNextAfterSeekingOnRank_2() { |
| 523 | final long INITIAL_RANK = 1L; |
| 524 | final int NB_READS = 20; |
| 525 | |
| 526 | // On lower bound, returns the first event (rank = 0) |
| 527 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); |
| 528 | |
| 529 | validateContextRanks(context); |
| 530 | |
| 531 | // Read NB_EVENTS |
| 532 | ITmfEvent event; |
| 533 | for (int i = 0; i < NB_READS; i++) { |
| 534 | event = fExperiment.getNext(context); |
| 535 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); |
| 536 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); |
| 537 | } |
| 538 | |
| 539 | // Make sure we stay positioned |
| 540 | event = fExperiment.parseEvent(context); |
| 541 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); |
| 542 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); |
| 543 | |
| 544 | validateContextRanks(context); |
| 545 | } |
| 546 | |
| 547 | @Test |
| 548 | public void testGetNextAfterSeekingOnRank_3() { |
| 549 | final long INITIAL_RANK = 500L; |
| 550 | final int NB_READS = 20; |
| 551 | |
| 552 | // On lower bound, returns the first event (rank = 0) |
| 553 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); |
| 554 | |
| 555 | validateContextRanks(context); |
| 556 | |
| 557 | // Read NB_EVENTS |
| 558 | ITmfEvent event; |
| 559 | for (int i = 0; i < NB_READS; i++) { |
| 560 | event = fExperiment.getNext(context); |
| 561 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); |
| 562 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); |
| 563 | } |
| 564 | |
| 565 | // Make sure we stay positioned |
| 566 | event = fExperiment.parseEvent(context); |
| 567 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); |
| 568 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); |
| 569 | |
| 570 | validateContextRanks(context); |
| 571 | } |
| 572 | |
| 573 | @Test |
| 574 | public void testGetNextAfterSeekingOnLocation_1() { |
| 575 | final ITmfLocation INITIAL_LOC = null; |
| 576 | final long INITIAL_TS = 1; |
| 577 | final int NB_READS = 20; |
| 578 | |
| 579 | // On lower bound, returns the first event (ts = 1) |
| 580 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); |
| 581 | |
| 582 | validateContextRanks(context); |
| 583 | |
| 584 | // Read NB_EVENTS |
| 585 | ITmfEvent event; |
| 586 | for (int i = 0; i < NB_READS; i++) { |
| 587 | event = fExperiment.getNext(context); |
| 588 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 589 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); |
| 590 | } |
| 591 | |
| 592 | // Make sure we stay positioned |
| 593 | event = fExperiment.parseEvent(context); |
| 594 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 595 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); |
| 596 | |
| 597 | validateContextRanks(context); |
| 598 | } |
| 599 | |
| 600 | @Test |
| 601 | public void testGetNextAfterSeekingOnLocation_2() { |
| 602 | final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation(); |
| 603 | final long INITIAL_TS = 2; |
| 604 | final int NB_READS = 20; |
| 605 | |
| 606 | // On lower bound, returns the first event (ts = 2) |
| 607 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); |
| 608 | |
| 609 | validateContextRanks(context); |
| 610 | |
| 611 | // Read NB_EVENTS |
| 612 | ITmfEvent event; |
| 613 | for (int i = 0; i < NB_READS; i++) { |
| 614 | event = fExperiment.getNext(context); |
| 615 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 616 | } |
| 617 | |
| 618 | // Make sure we stay positioned |
| 619 | event = fExperiment.parseEvent(context); |
| 620 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 621 | |
| 622 | validateContextRanks(context); |
| 623 | } |
| 624 | |
| 625 | @Test |
| 626 | public void testGetNextAfterSeekingOnLocation_3() { |
| 627 | final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation(); |
| 628 | final long INITIAL_TS = 501; |
| 629 | final int NB_READS = 20; |
| 630 | |
| 631 | // On lower bound, returns the first event (ts = 501) |
| 632 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); |
| 633 | |
| 634 | validateContextRanks(context); |
| 635 | |
| 636 | // Read NB_EVENTS |
| 637 | ITmfEvent event; |
| 638 | for (int i = 0; i < NB_READS; i++) { |
| 639 | event = fExperiment.getNext(context); |
| 640 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
| 641 | } |
| 642 | |
| 643 | // Make sure we stay positioned |
| 644 | event = fExperiment.parseEvent(context); |
| 645 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); |
| 646 | |
| 647 | validateContextRanks(context); |
| 648 | } |
| 649 | |
| 650 | @Test |
| 651 | public void testGetNextLocation() { |
| 652 | ITmfContext context1 = fExperiment.seekEvent(0); |
| 653 | fExperiment.getNext(context1); |
| 654 | ITmfLocation location = context1.getLocation(); |
| 655 | ITmfEvent event1 = fExperiment.getNext(context1); |
| 656 | ITmfContext context2 = fExperiment.seekEvent(location); |
| 657 | ITmfEvent event2 = fExperiment.getNext(context2); |
| 658 | assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue()); |
| 659 | } |
| 660 | |
| 661 | @Test |
| 662 | public void testGetNextEndLocation() { |
| 663 | ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1); |
| 664 | fExperiment.getNext(context1); |
| 665 | ITmfLocation location = context1.getLocation(); |
| 666 | ITmfContext context2 = fExperiment.seekEvent(location); |
| 667 | ITmfEvent event = fExperiment.getNext(context2); |
| 668 | assertNull("Event", event); |
| 669 | } |
| 670 | |
| 671 | // ------------------------------------------------------------------------ |
| 672 | // processRequest |
| 673 | // ------------------------------------------------------------------------ |
| 674 | |
| 675 | @Test |
| 676 | public void testProcessRequestForNbEvents() throws InterruptedException { |
| 677 | final int nbEvents = 1000; |
| 678 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
| 679 | |
| 680 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
| 681 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
| 682 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
| 683 | @Override |
| 684 | public void handleData(final ITmfEvent event) { |
| 685 | super.handleData(event); |
| 686 | requestedEvents.add(event); |
| 687 | } |
| 688 | }; |
| 689 | fExperiment.sendRequest(request); |
| 690 | request.waitForCompletion(); |
| 691 | |
| 692 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); |
| 693 | assertTrue("isCompleted", request.isCompleted()); |
| 694 | assertFalse("isCancelled", request.isCancelled()); |
| 695 | |
| 696 | // Ensure that we have distinct events. |
| 697 | // Don't go overboard: we are not validating the stub! |
| 698 | for (int i = 0; i < nbEvents; i++) { |
| 699 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | @Test |
| 704 | public void testProcessRequestForAllEvents() throws InterruptedException { |
| 705 | final int nbEvents = ITmfEventRequest.ALL_DATA; |
| 706 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
| 707 | final long nbExpectedEvents = NB_EVENTS; |
| 708 | |
| 709 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
| 710 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
| 711 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
| 712 | @Override |
| 713 | public void handleData(final ITmfEvent event) { |
| 714 | super.handleData(event); |
| 715 | requestedEvents.add(event); |
| 716 | } |
| 717 | }; |
| 718 | fExperiment.sendRequest(request); |
| 719 | request.waitForCompletion(); |
| 720 | |
| 721 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); |
| 722 | assertTrue("isCompleted", request.isCompleted()); |
| 723 | assertFalse("isCancelled", request.isCancelled()); |
| 724 | |
| 725 | // Ensure that we have distinct events. |
| 726 | // Don't go overboard: we are not validating the stub! |
| 727 | for (int i = 0; i < nbExpectedEvents; i++) { |
| 728 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | // ------------------------------------------------------------------------ |
| 733 | // cancel |
| 734 | // ------------------------------------------------------------------------ |
| 735 | |
| 736 | @Test |
| 737 | public void testCancel() throws InterruptedException { |
| 738 | final int nbEvents = NB_EVENTS; |
| 739 | final int limit = BLOCK_SIZE; |
| 740 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
| 741 | |
| 742 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
| 743 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
| 744 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
| 745 | int nbRead = 0; |
| 746 | |
| 747 | @Override |
| 748 | public void handleData(final ITmfEvent event) { |
| 749 | super.handleData(event); |
| 750 | requestedEvents.add(event); |
| 751 | if (++nbRead == limit) { |
| 752 | cancel(); |
| 753 | } |
| 754 | } |
| 755 | }; |
| 756 | fExperiment.sendRequest(request); |
| 757 | request.waitForCompletion(); |
| 758 | |
| 759 | assertEquals("nbEvents", limit, requestedEvents.size()); |
| 760 | assertTrue("isCompleted", request.isCompleted()); |
| 761 | assertTrue("isCancelled", request.isCancelled()); |
| 762 | } |
| 763 | |
| 764 | // ------------------------------------------------------------------------ |
| 765 | // getTimestamp |
| 766 | // ------------------------------------------------------------------------ |
| 767 | |
| 768 | @Test |
| 769 | public void testGetTimestamp() { |
| 770 | assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment.getTimestamp( 0)); |
| 771 | assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment.getTimestamp( 1)); |
| 772 | assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment.getTimestamp( 10)); |
| 773 | assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment.getTimestamp( 100)); |
| 774 | assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment.getTimestamp( 1000)); |
| 775 | assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment.getTimestamp( 2000)); |
| 776 | assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment.getTimestamp( 2500)); |
| 777 | assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment.getTimestamp( 9999)); |
| 778 | assertEquals("getTimestamp", new TmfTimestamp(20000, (byte) -3), fExperiment.getTimestamp(19999)); |
| 779 | assertNull("getTimestamp", fExperiment.getTimestamp(20000)); |
| 780 | } |
| 781 | |
| 782 | // ------------------------------------------------------------------------ |
| 783 | // getInitialRangeOffset, getCurrentRange, getCurrentTime |
| 784 | // ------------------------------------------------------------------------ |
| 785 | |
| 786 | @Test |
| 787 | public void testDefaultCurrentTimeValues() { |
| 788 | ITmfTrace[] traces = setupTraces(); |
| 789 | TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE); |
| 790 | |
| 791 | // verify initial values |
| 792 | TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE); |
| 793 | assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset()); |
| 794 | |
| 795 | exp.dispose(); |
| 796 | } |
| 797 | |
| 798 | @Test |
| 799 | public void testInitialRangeOffset() { |
| 800 | ITmfTrace[] traces = setupTraces(); |
| 801 | ((TmfTraceStub) traces[0]).setInitialRangeOffset(new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE)); |
| 802 | ((TmfTraceStub) traces[1]).setInitialRangeOffset(new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE)); |
| 803 | TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE); |
| 804 | |
| 805 | TmfTimestamp initRange = new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE); |
| 806 | assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset()); |
| 807 | |
| 808 | exp.dispose(); |
| 809 | } |
| 810 | |
| 811 | } |