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
.tests
.shared
.TmfTestTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
36 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
43 * Test suite for the TmfCheckpointIndexer class (events with same
44 * timestamp around checkpoint).
46 @SuppressWarnings("javadoc")
47 public class TmfCheckpointIndexTest2
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private static final int BLOCK_SIZE
= 100;
54 private static final int NB_EVENTS
= 702;
55 private static TestTrace fTrace
= null;
56 private static EmptyTestTrace fEmptyTrace
= null;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
64 // Trace has 3 events at t=101 at rank 99, 100, 101
65 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
66 setupTrace(TmfTestTrace
.A_TEST_10K2
.getFullPath());
70 public void tearDown() {
73 fEmptyTrace
.dispose();
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 private static class TestIndexer
extends TmfCheckpointIndexer
{
82 @SuppressWarnings({ })
83 public TestIndexer(TestTrace testTrace
) {
84 super(testTrace
, BLOCK_SIZE
);
86 @SuppressWarnings({ })
87 public TestIndexer(EmptyTestTrace testTrace
) {
88 super(testTrace
, BLOCK_SIZE
);
90 public List
<ITmfCheckpoint
> getCheckpoints() {
91 return getTraceIndex();
95 private class TestTrace
extends TmfTraceStub
{
96 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
97 super(path
, blockSize
, false, null, null);
98 setIndexer(new TestIndexer(this));
101 public TestIndexer
getIndexer() {
102 return (TestIndexer
) super.getIndexer();
106 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
107 public EmptyTestTrace() {
109 setIndexer(new TestIndexer(this));
112 public TestIndexer
getIndexer() {
113 return (TestIndexer
) super.getIndexer();
117 // ------------------------------------------------------------------------
119 // ------------------------------------------------------------------------
121 private synchronized void setupTrace(final String path
) {
122 if (fTrace
== null) {
124 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
125 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
126 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
127 fTrace
.indexTrace(true);
128 } catch (final TmfTraceException e
) {
130 } catch (final URISyntaxException e
) {
132 } catch (final IOException e
) {
137 if (fEmptyTrace
== null) {
138 fEmptyTrace
= new EmptyTestTrace();
139 fEmptyTrace
.indexTrace(true);
143 // ------------------------------------------------------------------------
144 // Verify checkpoints
145 // ------------------------------------------------------------------------
148 public void testTmfTraceMultiTimestamps() {
149 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
150 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
151 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
152 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
153 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
154 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
156 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
157 assertTrue("Checkpoints exist", checkpoints
!= null);
158 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
160 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
162 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
163 // and not the event at checkpoint boundary
164 TmfTimestamp seekTs
= new TmfTimestamp(101, -3, 0);
165 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
166 ITmfEvent event
= fTrace
.getNext(ctx
);
168 assertEquals(99, ctx
.getRank());
169 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
171 event
= fTrace
.getNext(ctx
);
173 assertEquals(100, ctx
.getRank());
174 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
176 event
= fTrace
.getNext(ctx
);
178 assertEquals(101, ctx
.getRank());
179 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
181 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
182 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
183 // and not the event at checkpoint boundary
184 seekTs
= new TmfTimestamp(102, -3, 0);
185 ctx
= fTrace
.seekEvent(seekTs
);
186 event
= fTrace
.getNext(ctx
);
188 assertEquals(102, ctx
.getRank());
189 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
191 // Verify seek to first checkpoint
192 seekTs
= new TmfTimestamp(1, -3, 0);
193 ctx
= fTrace
.seekEvent(seekTs
);
194 event
= fTrace
.getNext(ctx
);
196 assertEquals(1, ctx
.getRank());
197 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
199 // Verify seek to timestamp before first event
200 seekTs
= new TmfTimestamp(0, -3, 0);
201 ctx
= fTrace
.seekEvent(seekTs
);
202 event
= fTrace
.getNext(ctx
);
204 assertEquals(1, ctx
.getRank());
205 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event
.getTimestamp(), false));
207 // Verify seek to timestamp between first and second checkpoint
208 seekTs
= new TmfTimestamp(50, -3, 0);
209 ctx
= fTrace
.seekEvent(seekTs
);
210 event
= fTrace
.getNext(ctx
);
212 assertEquals(50, ctx
.getRank());
213 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
215 // Verify seek to timestamp after last event in trace
216 seekTs
= new TmfTimestamp(103, -3, 0);
217 ctx
= fTrace
.seekEvent(seekTs
);
218 event
= fTrace
.getNext(ctx
);
220 assertEquals(-1, ctx
.getRank());