* Francois Chouinard - Initial API and implementation
* Francois Chouinard - Adjusted for new Trace Model
* Alexandre Montplaisir - Port to JUnit4
+ * Patrick Tasse - Fix for concurrency
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.tests.trace;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
+import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
-import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
/**
* Test suite for the TmfExperiment class (multiple traces).
*/
-@SuppressWarnings({"nls","javadoc"})
+@SuppressWarnings("javadoc")
public class TmfMultiTraceExperimentTest {
// ------------------------------------------------------------------------
private static int NB_EVENTS = 20000;
private static int BLOCK_SIZE = 1000;
- private static ITmfTrace[] fTraces;
private static TmfExperimentStub fExperiment;
private static byte SCALE = (byte) -3;
// Housekeeping
// ------------------------------------------------------------------------
- @Before
- public void setUp() {
- setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2);
- if (fExperiment == null) {
- fExperiment = new TmfExperimentStub(EXPERIMENT, fTraces, BLOCK_SIZE);
- fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
- }
+ @BeforeClass
+ public static void setUp() {
+ ITmfTrace[] traces = setupTraces();
+ fExperiment = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
+ fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
}
- private synchronized static ITmfTrace[] setupTrace(final String path1, final String path2) {
- if (fTraces == null) {
- fTraces = new ITmfTrace[2];
- try {
- URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path1), null);
- File test = new File(FileLocator.toFileURL(location).toURI());
- final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true);
- trace1.setInitialRangeOffset(new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE));
-
- fTraces[0] = trace1;
- location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path2), null);
- test = new File(FileLocator.toFileURL(location).toURI());
- final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true);
- trace2.setInitialRangeOffset(new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE));
- fTraces[1] = trace2;
- } catch (final TmfTraceException e) {
- e.printStackTrace();
- } catch (final URISyntaxException e) {
- e.printStackTrace();
- } catch (final IOException e) {
- e.printStackTrace();
- }
+ @AfterClass
+ public static void tearDown() {
+ fExperiment.dispose();
+ }
+
+ private static ITmfTrace[] setupTraces() {
+ final String path1 = DIRECTORY + File.separator + TEST_STREAM1;
+ final String path2 = DIRECTORY + File.separator + TEST_STREAM2;
+ try {
+ ITmfTrace[] traces = new ITmfTrace[2];
+
+ URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path1), null);
+ File test = new File(FileLocator.toFileURL(location).toURI());
+ final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true);
+ traces[0] = trace1;
+
+ location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path2), null);
+ test = new File(FileLocator.toFileURL(location).toURI());
+ final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true);
+ traces[1] = trace2;
+
+ return traces;
+ } catch (final TmfTraceException e) {
+ e.printStackTrace();
+ } catch (final URISyntaxException e) {
+ e.printStackTrace();
+ } catch (final IOException e) {
+ e.printStackTrace();
}
- return fTraces;
+ return new ITmfTrace[0];
}
// ------------------------------------------------------------------------
assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
- TmfTimestamp initRange = new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE);
+ TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
assertEquals("getInitialRangeOffset", initRange, fExperiment.getInitialRangeOffset());
- assertEquals("getCurrentTime", fExperiment.getTimeRange().getStartTime(), fExperiment.getCurrentTime());
-
- ITmfTimestamp startTimestamp = fExperiment.getTimeRange().getStartTime();
- long endValue = startTimestamp.getValue() + initRange.normalize(0, startTimestamp.getScale()).getValue();
- ITmfTimestamp endTimestamp = new TmfTimestamp(endValue, startTimestamp.getScale());
- TmfTimeRange expectedRange = new TmfTimeRange(startTimestamp, endTimestamp);
- assertEquals("getCurrentRange", expectedRange, fExperiment.getCurrentRange());
}
// ------------------------------------------------------------------------
@Test
public void testProcessRequestForNbEvents() throws InterruptedException {
- final int blockSize = 100;
final int nbEvents = 1000;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
- @Override
- public void handleData(final ITmfEvent event) {
- super.handleData(event);
- requestedEvents.add(event);
- }
- };
- fExperiment.sendRequest(request);
- request.waitForCompletion();
-
- assertEquals("nbEvents", nbEvents, requestedEvents.size());
- assertTrue("isCompleted", request.isCompleted());
- assertFalse("isCancelled", request.isCancelled());
-
- // Ensure that we have distinct events.
- // Don't go overboard: we are not validating the stub!
- for (int i = 0; i < nbEvents; i++) {
- assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
- }
- }
-
- @Test
- public void testProcessRequestForNbEvents2() throws InterruptedException {
- final int blockSize = 2 * NB_EVENTS;
- final int nbEvents = 1000;
- final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
-
- final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
@Test
public void testProcessRequestForAllEvents() throws InterruptedException {
final int nbEvents = TmfDataRequest.ALL_DATA;
- final int blockSize = 1;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final long nbExpectedEvents = NB_EVENTS;
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
@Test
public void testCancel() throws InterruptedException {
final int nbEvents = NB_EVENTS;
- final int blockSize = BLOCK_SIZE;
+ final int limit = BLOCK_SIZE;
final Vector<ITmfEvent> requestedEvents = new Vector<ITmfEvent>();
final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
- final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, range, nbEvents, blockSize) {
+ final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
+ range, 0, nbEvents, ExecutionType.FOREGROUND) {
int nbRead = 0;
+
@Override
public void handleData(final ITmfEvent event) {
super.handleData(event);
requestedEvents.add(event);
- if (++nbRead == blockSize) {
+ if (++nbRead == limit) {
cancel();
}
}
- @Override
- public void handleCancel() {
- if (requestedEvents.size() < blockSize) {
- System.out.println("aie");
- }
- }
};
fExperiment.sendRequest(request);
request.waitForCompletion();
- assertEquals("nbEvents", blockSize, requestedEvents.size());
+ assertEquals("nbEvents", limit, requestedEvents.size());
assertTrue("isCompleted", request.isCompleted());
assertTrue("isCancelled", request.isCancelled());
}
@Test
public void testDefaultCurrentTimeValues() {
- // reset to default initial range offset
- ((TmfTraceStub)fTraces[0]).setInitialRangeOffset(null);
- ((TmfTraceStub)fTraces[1]).setInitialRangeOffset(null);
-
- TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, fTraces, BLOCK_SIZE);
+ ITmfTrace[] traces = setupTraces();
+ TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
// verify initial values
TmfTimestamp initRange = new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset());
- assertEquals("getCurrentTime", TmfTimestamp.ZERO, exp.getCurrentTime());
- assertEquals("getCurrentRange", TmfTimeRange.NULL_RANGE, exp.getCurrentRange());
+
+ exp.dispose();
+ }
+
+ @Test
+ public void testInitialRangeOffset() {
+ ITmfTrace[] traces = setupTraces();
+ ((TmfTraceStub) traces[0]).setInitialRangeOffset(new TmfTimestamp(5, ITmfTimestamp.MILLISECOND_SCALE));
+ ((TmfTraceStub) traces[1]).setInitialRangeOffset(new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE));
+ TmfExperimentStub exp = new TmfExperimentStub(EXPERIMENT, traces, BLOCK_SIZE);
+
+ TmfTimestamp initRange = new TmfTimestamp(2, ITmfTimestamp.MILLISECOND_SCALE);
+ assertEquals("getInitialRangeOffset", initRange, exp.getInitialRangeOffset());
+
+ exp.dispose();
}
}