1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.indexer
.checkpoint
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertNull
;
18 import static org
.junit
.Assert
.assertTrue
;
21 import java
.io
.IOException
;
22 import java
.net
.URISyntaxException
;
25 import org
.eclipse
.core
.runtime
.FileLocator
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.indexer
.checkpoint
.AbstractIndexTest
.ITestIndexer
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
37 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
38 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
39 import org
.junit
.After
;
40 import org
.junit
.Before
;
41 import org
.junit
.Test
;
44 * Test suite for the TmfCheckpointIndexer class (events with same
45 * timestamp around checkpoint).
47 @SuppressWarnings("javadoc")
48 public class TmfCheckpointIndexTest2
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private static final int BLOCK_SIZE
= 100;
55 private static final int NB_EVENTS
= 702;
56 private static TestTrace fTrace
= null;
57 private static EmptyTestTrace fEmptyTrace
= null;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
65 // Trace has 3 events at t=101 at rank 99, 100, 101
66 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
67 setupTrace(TmfTestTrace
.A_TEST_10K2
.getFullPath());
71 public void tearDown() {
74 fEmptyTrace
.dispose();
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
82 private static class TestIndexer
extends TmfCheckpointIndexer
implements ITestIndexer
{
83 public TestIndexer(TestTrace testTrace
) {
84 super(testTrace
, BLOCK_SIZE
);
87 public TestIndexer(EmptyTestTrace testTrace
) {
88 super(testTrace
, BLOCK_SIZE
);
92 public ITmfCheckpointIndex
getCheckpoints() {
93 return getTraceIndex();
97 private class TestTrace
extends TmfTraceStub
{
98 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
99 super(path
, blockSize
, false, null);
103 protected ITmfTraceIndexer
createIndexer(int interval
) {
104 return new TestIndexer(this);
108 public ITestIndexer
getIndexer() {
109 return (ITestIndexer
) super.getIndexer();
113 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
115 public EmptyTestTrace(String path
) throws TmfTraceException
{
120 protected ITmfTraceIndexer
createIndexer(int interval
) {
121 return new TestIndexer(this);
125 public ITestIndexer
getIndexer() {
126 return (ITestIndexer
) super.getIndexer();
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
134 private synchronized void setupTrace(final String path
) {
135 if (fTrace
== null) {
137 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
138 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
139 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
140 fTrace
.indexTrace(true);
141 } catch (final TmfTraceException e
) {
143 } catch (final URISyntaxException e
) {
145 } catch (final IOException e
) {
150 if (fEmptyTrace
== null) {
152 File file
= File
.createTempFile("empty", "txt");
153 fEmptyTrace
= new EmptyTestTrace(file
.getAbsolutePath());
154 } catch (TmfTraceException
| IOException e
) {
160 // ------------------------------------------------------------------------
161 // Verify checkpoints
162 // ------------------------------------------------------------------------
165 public void testTmfTraceMultiTimestamps() {
166 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
167 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
168 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
169 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
170 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
171 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
173 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
174 assertTrue("Checkpoints exist", checkpoints
!= null);
175 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
177 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
179 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
180 // and not the event at checkpoint boundary
181 TmfTimestamp seekTs
= new TmfTimestamp(101, -3);
182 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
183 ITmfEvent event
= fTrace
.getNext(ctx
);
185 assertEquals(99, ctx
.getRank());
186 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
188 event
= fTrace
.getNext(ctx
);
190 assertEquals(100, ctx
.getRank());
191 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
193 event
= fTrace
.getNext(ctx
);
195 assertEquals(101, ctx
.getRank());
196 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
198 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
199 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
200 // and not the event at checkpoint boundary
201 seekTs
= new TmfTimestamp(102, -3);
202 ctx
= fTrace
.seekEvent(seekTs
);
203 event
= fTrace
.getNext(ctx
);
205 assertEquals(102, ctx
.getRank());
206 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
208 // Verify seek to first checkpoint
209 seekTs
= new TmfTimestamp(1, -3);
210 ctx
= fTrace
.seekEvent(seekTs
);
211 event
= fTrace
.getNext(ctx
);
213 assertEquals(1, ctx
.getRank());
214 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
216 // Verify seek to timestamp before first event
217 seekTs
= new TmfTimestamp(0, -3);
218 ctx
= fTrace
.seekEvent(seekTs
);
219 event
= fTrace
.getNext(ctx
);
221 assertEquals(1, ctx
.getRank());
222 assertEquals(0, new TmfTimestamp(1, -3).compareTo(event
.getTimestamp()));
224 // Verify seek to timestamp between first and second checkpoint
225 seekTs
= new TmfTimestamp(50, -3);
226 ctx
= fTrace
.seekEvent(seekTs
);
227 event
= fTrace
.getNext(ctx
);
229 assertEquals(50, ctx
.getRank());
230 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
232 // Verify seek to timestamp after last event in trace
233 seekTs
= new TmfTimestamp(103, -3);
234 ctx
= fTrace
.seekEvent(seekTs
);
235 event
= fTrace
.getNext(ctx
);
237 assertEquals(-1, ctx
.getRank());