1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.event
.TmfEvent
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
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
{
83 @SuppressWarnings({ })
84 public TestIndexer(TestTrace testTrace
) {
85 super(testTrace
, BLOCK_SIZE
);
87 @SuppressWarnings({ })
88 public TestIndexer(EmptyTestTrace testTrace
) {
89 super(testTrace
, BLOCK_SIZE
);
91 public ITmfCheckpointIndex
getCheckpoints() {
92 return getTraceIndex();
96 private class TestTrace
extends TmfTraceStub
{
97 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
98 super(path
, blockSize
, false, null);
102 protected ITmfTraceIndexer
createIndexer(int interval
) {
103 return new TestIndexer(this);
107 public TestIndexer
getIndexer() {
108 return (TestIndexer
) super.getIndexer();
112 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
114 public EmptyTestTrace() {
116 init(getClass().getSimpleName(), TmfEvent
.class);
120 protected ITmfTraceIndexer
createIndexer(int interval
) {
121 return new TestIndexer(this);
125 public TestIndexer
getIndexer() {
126 return (TestIndexer
) 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) {
151 fEmptyTrace
= new EmptyTestTrace();
152 fEmptyTrace
.indexTrace(true);
156 // ------------------------------------------------------------------------
157 // Verify checkpoints
158 // ------------------------------------------------------------------------
161 public void testTmfTraceMultiTimestamps() {
162 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
163 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
164 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
165 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
166 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
167 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
169 ITmfCheckpointIndex checkpoints
= fTrace
.getIndexer().getCheckpoints();
170 assertTrue("Checkpoints exist", checkpoints
!= null);
171 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
173 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
175 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
176 // and not the event at checkpoint boundary
177 TmfTimestamp seekTs
= new TmfTimestamp(101, -3);
178 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
179 ITmfEvent event
= fTrace
.getNext(ctx
);
181 assertEquals(99, ctx
.getRank());
182 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
184 event
= fTrace
.getNext(ctx
);
186 assertEquals(100, ctx
.getRank());
187 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
189 event
= fTrace
.getNext(ctx
);
191 assertEquals(101, ctx
.getRank());
192 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
194 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
195 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
196 // and not the event at checkpoint boundary
197 seekTs
= new TmfTimestamp(102, -3);
198 ctx
= fTrace
.seekEvent(seekTs
);
199 event
= fTrace
.getNext(ctx
);
201 assertEquals(102, ctx
.getRank());
202 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
204 // Verify seek to first checkpoint
205 seekTs
= new TmfTimestamp(1, -3);
206 ctx
= fTrace
.seekEvent(seekTs
);
207 event
= fTrace
.getNext(ctx
);
209 assertEquals(1, ctx
.getRank());
210 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
212 // Verify seek to timestamp before first event
213 seekTs
= new TmfTimestamp(0, -3);
214 ctx
= fTrace
.seekEvent(seekTs
);
215 event
= fTrace
.getNext(ctx
);
217 assertEquals(1, ctx
.getRank());
218 assertEquals(0, new TmfTimestamp(1, -3).compareTo(event
.getTimestamp()));
220 // Verify seek to timestamp between first and second checkpoint
221 seekTs
= new TmfTimestamp(50, -3);
222 ctx
= fTrace
.seekEvent(seekTs
);
223 event
= fTrace
.getNext(ctx
);
225 assertEquals(50, ctx
.getRank());
226 assertEquals(0, seekTs
.compareTo(event
.getTimestamp()));
228 // Verify seek to timestamp after last event in trace
229 seekTs
= new TmfTimestamp(103, -3);
230 ctx
= fTrace
.seekEvent(seekTs
);
231 event
= fTrace
.getNext(ctx
);
233 assertEquals(-1, ctx
.getRank());