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
;
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
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfCheckpoint
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpointIndexer
;
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({"nls","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
);
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
);
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();
144 // ------------------------------------------------------------------------
145 // Verify checkpoints
146 // ------------------------------------------------------------------------
149 @SuppressWarnings("null")
150 public void testTmfTraceMultiTimestamps() {
151 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
152 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
153 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
154 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
155 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
156 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
158 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
159 assertTrue("Checkpoints exist", checkpoints
!= null);
160 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
162 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
164 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
165 // and not the event at checkpoint boundary
166 TmfTimestamp seekTs
= new TmfTimestamp(101, -3, 0);
167 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
168 ITmfEvent event
= fTrace
.getNext(ctx
);
170 assertEquals(99, ctx
.getRank());
171 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
173 event
= fTrace
.getNext(ctx
);
175 assertEquals(100, ctx
.getRank());
176 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
178 event
= fTrace
.getNext(ctx
);
180 assertEquals(101, ctx
.getRank());
181 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
183 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
184 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
185 // and not the event at checkpoint boundary
186 seekTs
= new TmfTimestamp(102, -3, 0);
187 ctx
= fTrace
.seekEvent(seekTs
);
188 event
= fTrace
.getNext(ctx
);
190 assertEquals(102, ctx
.getRank());
191 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
193 // Verify seek to first checkpoint
194 seekTs
= new TmfTimestamp(1, -3, 0);
195 ctx
= fTrace
.seekEvent(seekTs
);
196 event
= fTrace
.getNext(ctx
);
198 assertEquals(1, ctx
.getRank());
199 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
201 // Verify seek to timestamp before first event
202 seekTs
= new TmfTimestamp(0, -3, 0);
203 ctx
= fTrace
.seekEvent(seekTs
);
204 event
= fTrace
.getNext(ctx
);
206 assertEquals(1, ctx
.getRank());
207 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event
.getTimestamp(), false));
209 // Verify seek to timestamp between first and second checkpoint
210 seekTs
= new TmfTimestamp(50, -3, 0);
211 ctx
= fTrace
.seekEvent(seekTs
);
212 event
= fTrace
.getNext(ctx
);
214 assertEquals(50, ctx
.getRank());
215 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
217 // Verify seek to timestamp after last event in trace
218 seekTs
= new TmfTimestamp(103, -3, 0);
219 ctx
= fTrace
.seekEvent(seekTs
);
220 event
= fTrace
.getNext(ctx
);
222 assertEquals(-1, ctx
.getRank());