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
.linuxtools
.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
;
24 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
35 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
36 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
37 import org
.junit
.After
;
38 import org
.junit
.Before
;
39 import org
.junit
.Test
;
42 * Test suite for the TmfCheckpointIndexer class (events with same
43 * timestamp around checkpoint).
45 @SuppressWarnings("javadoc")
46 public class TmfCheckpointIndexTest2
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static final String DIRECTORY
= "testfiles";
53 // Trace has 3 events at t=101 at rank 99, 100, 101
54 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
55 private static final String TEST_STREAM
= "A-Test-10K-2";
56 private static final int BLOCK_SIZE
= 100;
57 private static final int NB_EVENTS
= 702;
58 private static TestTrace fTrace
= null;
59 private static EmptyTestTrace fEmptyTrace
= null;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
67 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
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 List
<ITmfCheckpoint
> 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, null);
99 setIndexer(new TestIndexer(this));
102 public TestIndexer
getIndexer() {
103 return (TestIndexer
) super.getIndexer();
107 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
108 public EmptyTestTrace() {
110 setIndexer(new TestIndexer(this));
113 public TestIndexer
getIndexer() {
114 return (TestIndexer
) super.getIndexer();
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
122 private synchronized void setupTrace(final String path
) {
123 if (fTrace
== null) {
125 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
126 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
127 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
128 fTrace
.indexTrace(true);
129 } catch (final TmfTraceException e
) {
131 } catch (final URISyntaxException e
) {
133 } catch (final IOException e
) {
138 if (fEmptyTrace
== null) {
139 fEmptyTrace
= new EmptyTestTrace();
140 fEmptyTrace
.indexTrace(true);
144 // ------------------------------------------------------------------------
145 // Verify checkpoints
146 // ------------------------------------------------------------------------
149 public void testTmfTraceMultiTimestamps() {
150 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
151 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
152 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
153 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
154 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
155 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
157 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
158 assertTrue("Checkpoints exist", checkpoints
!= null);
159 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
161 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
163 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
164 // and not the event at checkpoint boundary
165 TmfTimestamp seekTs
= new TmfTimestamp(101, -3, 0);
166 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
167 ITmfEvent event
= fTrace
.getNext(ctx
);
169 assertEquals(99, ctx
.getRank());
170 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
172 event
= fTrace
.getNext(ctx
);
174 assertEquals(100, ctx
.getRank());
175 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
177 event
= fTrace
.getNext(ctx
);
179 assertEquals(101, ctx
.getRank());
180 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
182 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
183 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
184 // and not the event at checkpoint boundary
185 seekTs
= new TmfTimestamp(102, -3, 0);
186 ctx
= fTrace
.seekEvent(seekTs
);
187 event
= fTrace
.getNext(ctx
);
189 assertEquals(102, ctx
.getRank());
190 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
192 // Verify seek to first checkpoint
193 seekTs
= new TmfTimestamp(1, -3, 0);
194 ctx
= fTrace
.seekEvent(seekTs
);
195 event
= fTrace
.getNext(ctx
);
197 assertEquals(1, ctx
.getRank());
198 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
200 // Verify seek to timestamp before first event
201 seekTs
= new TmfTimestamp(0, -3, 0);
202 ctx
= fTrace
.seekEvent(seekTs
);
203 event
= fTrace
.getNext(ctx
);
205 assertEquals(1, ctx
.getRank());
206 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event
.getTimestamp(), false));
208 // Verify seek to timestamp between first and second checkpoint
209 seekTs
= new TmfTimestamp(50, -3, 0);
210 ctx
= fTrace
.seekEvent(seekTs
);
211 event
= fTrace
.getNext(ctx
);
213 assertEquals(50, ctx
.getRank());
214 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
216 // Verify seek to timestamp after last event in trace
217 seekTs
= new TmfTimestamp(103, -3, 0);
218 ctx
= fTrace
.seekEvent(seekTs
);
219 event
= fTrace
.getNext(ctx
);
221 assertEquals(-1, ctx
.getRank());