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
.ITmfTimestamp
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
38 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
39 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.Test
;
45 * Test suite for the TmfCheckpointIndexer class (events with same
46 * timestamp around checkpoint).
48 @SuppressWarnings("javadoc")
49 public class TmfCheckpointIndexTest2
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private static final int BLOCK_SIZE
= 100;
56 private static final int NB_EVENTS
= 702;
57 private static TestTrace fTrace
= null;
58 private static EmptyTestTrace fEmptyTrace
= null;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
66 // Trace has 3 events at t=101 at rank 99, 100, 101
67 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
68 setupTrace(TmfTestTrace
.A_TEST_10K2
.getFullPath());
72 public void tearDown() {
75 fEmptyTrace
.dispose();
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
83 private static class TestIndexer
extends TmfCheckpointIndexer
implements ITestIndexer
{
84 public TestIndexer(TestTrace testTrace
) {
85 super(testTrace
, BLOCK_SIZE
);
88 public TestIndexer(EmptyTestTrace testTrace
) {
89 super(testTrace
, BLOCK_SIZE
);
93 public ITmfCheckpointIndex
getCheckpoints() {
94 return getTraceIndex();
98 private class TestTrace
extends TmfTraceStub
{
99 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
100 super(path
, blockSize
, false, null);
104 protected ITmfTraceIndexer
createIndexer(int interval
) {
105 return new TestIndexer(this);
109 public ITestIndexer
getIndexer() {
110 return (ITestIndexer
) super.getIndexer();
114 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
116 public EmptyTestTrace(String path
) throws TmfTraceException
{
121 protected ITmfTraceIndexer
createIndexer(int interval
) {
122 return new TestIndexer(this);
126 public ITestIndexer
getIndexer() {
127 return (ITestIndexer
) super.getIndexer();
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
135 private synchronized void setupTrace(final String path
) {
136 if (fTrace
== null) {
138 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
139 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
140 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
141 fTrace
.indexTrace(true);
142 } catch (final TmfTraceException e
) {
144 } catch (final URISyntaxException e
) {
146 } catch (final IOException e
) {
151 if (fEmptyTrace
== null) {
153 File file
= File
.createTempFile("empty", "txt");
154 fEmptyTrace
= new EmptyTestTrace(file
.getAbsolutePath());
155 } catch (TmfTraceException
| IOException e
) {
161 // ------------------------------------------------------------------------
162 // Verify checkpoints
163 // ------------------------------------------------------------------------
166 public void testTmfTraceMultiTimestamps() {
167 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
168 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
169 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
170 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
171 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
172 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
174 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
175 assertTrue("Checkpoints exist", checkpoints
!= null);
176 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
178 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
180 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
181 // and not the event at checkpoint boundary
182 ITmfTimestamp seekTs
= TmfTimestamp
.create(101, -3);
183 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
184 ITmfEvent event
= fTrace
.getNext(ctx
);
186 assertEquals(99, ctx
.getRank());
187 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
189 event
= fTrace
.getNext(ctx
);
191 assertEquals(100, ctx
.getRank());
192 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
194 event
= fTrace
.getNext(ctx
);
196 assertEquals(101, ctx
.getRank());
197 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
199 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
200 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
201 // and not the event at checkpoint boundary
202 seekTs
= TmfTimestamp
.create(102, -3);
203 ctx
= fTrace
.seekEvent(seekTs
);
204 event
= fTrace
.getNext(ctx
);
206 assertEquals(102, ctx
.getRank());
207 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
209 // Verify seek to first checkpoint
210 seekTs
= TmfTimestamp
.create(1, -3);
211 ctx
= fTrace
.seekEvent(seekTs
);
212 event
= fTrace
.getNext(ctx
);
214 assertEquals(1, ctx
.getRank());
215 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
217 // Verify seek to timestamp before first event
218 seekTs
= TmfTimestamp
.create(0, -3);
219 ctx
= fTrace
.seekEvent(seekTs
);
220 event
= fTrace
.getNext(ctx
);
222 assertEquals(1, ctx
.getRank());
223 assertEquals(0, TmfTimestamp
.create(1, -3).compareTo(event
.getTimestamp()));
225 // Verify seek to timestamp between first and second checkpoint
226 seekTs
= TmfTimestamp
.create(50, -3);
227 ctx
= fTrace
.seekEvent(seekTs
);
228 event
= fTrace
.getNext(ctx
);
230 assertEquals(50, ctx
.getRank());
231 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
233 // Verify seek to timestamp after last event in trace
234 seekTs
= TmfTimestamp
.create(103, -3);
235 ctx
= fTrace
.seekEvent(seekTs
);
236 event
= fTrace
.getNext(ctx
);
238 assertEquals(-1, ctx
.getRank());