diff options
author | Patrick Tasse | 2014-10-21 18:17:24 +0000 |
---|---|---|
committer | Patrick Tasse | 2014-10-24 22:08:33 +0000 |
commit | e4a8412cbe9ce3a161101bc368d001e4f30b53bf (patch) | |
tree | 7e2b6b8c930f462e8997c02087e828b400b59c81 | |
parent | 9034597bc83bb0d60ff2605e7b1c0fafae886e32 (diff) | |
download | org.eclipse.linuxtools-e4a8412cbe9ce3a161101bc368d001e4f30b53bf.tar.gz org.eclipse.linuxtools-e4a8412cbe9ce3a161101bc368d001e4f30b53bf.tar.xz org.eclipse.linuxtools-e4a8412cbe9ce3a161101bc368d001e4f30b53bf.zip |
tmf: Add unit tests for time offset
Change-Id: Ib44c6acb6f8bb0e3d23a583e30e6fec7071686b3
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/35271
Tested-by: Hudson CI
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
4 files changed, 205 insertions, 3 deletions
diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/AllTests.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/AllTests.java index 283a914677..18e5442e6a 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/AllTests.java +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/AllTests.java @@ -21,7 +21,8 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ TsTransformTest.class, SyncTest.class, - TsTransformFactoryTest.class }) + TsTransformFactoryTest.class, + TimeOffsetTest.class }) public class AllTests { } diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/TimeOffsetTest.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/TimeOffsetTest.java new file mode 100644 index 0000000000..ba10d661d7 --- /dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/synchronization/TimeOffsetTest.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Copyright (c) 2014 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Patrick Tasse - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.core.tests.synchronization; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; +import java.net.URL; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; +import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; +import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; +import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; +import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory; +import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; +import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.core.trace.TmfContext; +import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.rules.Timeout; + +/** + * Test suite for time offset of traces. + */ +@SuppressWarnings("javadoc") +public class TimeOffsetTest { + + /** Time-out tests after 20 seconds */ + @Rule + public TestRule globalTimeout= new Timeout(20000); + + // ------------------------------------------------------------------------ + // Variables + // ------------------------------------------------------------------------ + + private static final TmfTestTrace TEST_TRACE = TmfTestTrace.A_TEST_10K; + private static final String PROJECT = "Test Project"; + private static final String RESOURCE = "Test Resource"; + private static final long ONE_MS = 1000000L; + private IFile fResource; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + @Before + public void setUp() throws CoreException { + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT); + if (!project.exists()) { + project.create(null); + } + project.open(null); + fResource = project.getFile(RESOURCE); + if (!fResource.exists()) { + final InputStream source = new ByteArrayInputStream(new byte[0]); + fResource.create(source, true, null); + } + fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, fResource.getParent().getLocationURI().getPath()); + } + + @After + public void tearDown() throws CoreException { + if (fResource != null && fResource.exists()) { + fResource.getProject().delete(true, null); + } + } + + private ITmfTrace createAndIndexTrace() throws URISyntaxException, IOException, TmfTraceException { + final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE.getFullPath()), null); + File testfile = new File(FileLocator.toFileURL(location).toURI()); + TmfTraceStub trace = new TmfTraceStub(fResource, testfile.toURI().getPath(), ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null); + trace.indexTrace(true); + return trace; + } + + // ------------------------------------------------------------------------ + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testNoOffset() throws URISyntaxException, IOException, TmfTraceException { + ITmfTrace trace = createAndIndexTrace(); + final TmfContext context = (TmfContext) trace.seekEvent(0); + + ITmfEvent event = trace.getNext(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + event = trace.getNext(context); + assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); + + trace.dispose(); + } + + @Test + public void testPositiveOffset() throws URISyntaxException, IOException, TmfTraceException { + ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(ONE_MS); + TimestampTransformFactory.setTimestampTransform(fResource, tt); + + ITmfTrace trace = createAndIndexTrace(); + final TmfContext context = (TmfContext) trace.seekEvent(0); + + ITmfEvent event = trace.getNext(context); + assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); + event = trace.getNext(context); + assertEquals("Event timestamp", 3, event.getTimestamp().getValue()); + + trace.dispose(); + } + + @Test + public void testNegativeOffset() throws URISyntaxException, IOException, TmfTraceException { + ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(-ONE_MS); + TimestampTransformFactory.setTimestampTransform(fResource, tt); + + ITmfTrace trace = createAndIndexTrace(); + final TmfContext context = (TmfContext) trace.seekEvent(0); + + ITmfEvent event = trace.getNext(context); + assertEquals("Event timestamp", 0, event.getTimestamp().getValue()); + event = trace.getNext(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + + trace.dispose(); + } + + @Test + public void testClearOffset() throws URISyntaxException, IOException, TmfTraceException { + ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(ONE_MS); + TimestampTransformFactory.setTimestampTransform(fResource, tt); + TimestampTransformFactory.setTimestampTransform(fResource, null); + + ITmfTrace trace = createAndIndexTrace(); + final TmfContext context = (TmfContext) trace.seekEvent(0); + + ITmfEvent event = trace.getNext(context); + assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); + event = trace.getNext(context); + assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); + + trace.dispose(); + } +} diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEventParserStub.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEventParserStub.java index 86def21e4f..8a692d5a78 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEventParserStub.java +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfEventParserStub.java @@ -22,7 +22,6 @@ import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; import org.eclipse.linuxtools.tmf.core.event.TmfEvent; import org.eclipse.linuxtools.tmf.core.event.TmfEventField; import org.eclipse.linuxtools.tmf.core.event.TmfEventType; -import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; @@ -112,7 +111,7 @@ public class TmfEventParserStub implements ITmfEventParser { final TmfEventField root = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content.toString(), null); final ITmfEvent event = new TmfEvent(fEventStream, - new TmfTimestamp(ts, -3, 0), // millisecs + fEventStream.createTimestamp(ts * 1000000L), source, fTypes[typeIndex], root, String.valueOf(reference)); return event; } catch (final EOFException e) { diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java index 12845edca0..9459bf8f94 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java @@ -126,6 +126,37 @@ public class TmfTraceStub extends TmfTrace implements ITmfEventParser, ITmfPersi } /** + * Constructor to specify the resource, parser and indexer. The streaming + * interval will be 0. + * + * @param resource + * The trace resource + * @param path + * The path to the trace file + * @param cacheSize + * The cache size + * @param waitForCompletion + * Do we block the caller until the trace is indexed, or not. + * @param parser + * The trace parser. If left 'null', it will use a + * {@link TmfEventParserStub}. + * @throws TmfTraceException + * If an error occurred opening the trace + */ + public TmfTraceStub(final IResource resource, + final String path, + final int cacheSize, + final boolean waitForCompletion, + final ITmfEventParser parser) throws TmfTraceException { + super(resource, ITmfEvent.class, path, cacheSize, 0, null); + setupTrace(path); + setParser((parser != null) ? parser : new TmfEventParserStub(this)); + if (waitForCompletion) { + indexTrace(true); + } + } + + /** * Copy constructor * * @param trace @@ -303,6 +334,11 @@ public class TmfTraceStub extends TmfTrace implements ITmfEventParser, ITmfPersi } @Override + public ITmfTimestamp createTimestamp(long ts) { + return new TmfTimestamp(getTimestampTransform().transform(ts) / 1000000L, ITmfTimestamp.MILLISECOND_SCALE); + } + + @Override public synchronized void setNbEvents(final long nbEvents) { super.setNbEvents(nbEvents); } |