1 /*******************************************************************************
2 * Copyright (c) 2012 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.List
;
21 import junit
.framework
.TestCase
;
23 import org
.eclipse
.core
.runtime
.FileLocator
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfCheckpoint
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpointIndexer
;
32 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
33 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
36 * Test suite for the TmfCheckpointIndexer class (events with same
37 * timestamp around checkpoint).
39 @SuppressWarnings({"nls","javadoc"})
40 public class TmfCheckpointIndexTest2
extends TestCase
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
46 private static final String DIRECTORY
= "testfiles";
47 // Trace has 3 events at t=101 at rank 99, 100, 101
48 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
49 private static final String TEST_STREAM
= "A-Test-10K-2";
50 private static final int BLOCK_SIZE
= 100;
51 private static final int NB_EVENTS
= 702;
52 private static TestTrace fTrace
= null;
53 private static EmptyTestTrace fEmptyTrace
= null;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * @param name the test name
62 public TmfCheckpointIndexTest2(final String name
) {
67 protected void setUp() throws Exception
{
69 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
73 protected void tearDown() throws Exception
{
77 fEmptyTrace
.dispose();
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
85 private static class TestIndexer
extends TmfCheckpointIndexer
{
86 @SuppressWarnings({ })
87 public TestIndexer(TestTrace testTrace
) {
88 super(testTrace
, BLOCK_SIZE
);
90 @SuppressWarnings({ })
91 public TestIndexer(EmptyTestTrace testTrace
) {
92 super(testTrace
, BLOCK_SIZE
);
94 public List
<ITmfCheckpoint
> getCheckpoints() {
95 return getTraceIndex();
99 private class TestTrace
extends TmfTraceStub
{
100 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
101 super(path
, blockSize
);
102 setIndexer(new TestIndexer(this));
105 public TestIndexer
getIndexer() {
106 return (TestIndexer
) super.getIndexer();
110 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
111 public EmptyTestTrace() {
113 setIndexer(new TestIndexer(this));
116 public TestIndexer
getIndexer() {
117 return (TestIndexer
) super.getIndexer();
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
125 private synchronized void setupTrace(final String path
) {
126 if (fTrace
== null) {
128 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
129 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
130 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
132 } catch (final TmfTraceException e
) {
134 } catch (final URISyntaxException e
) {
136 } catch (final IOException e
) {
141 if (fEmptyTrace
== null) {
142 fEmptyTrace
= new EmptyTestTrace();
143 fEmptyTrace
.indexTrace();
147 // ------------------------------------------------------------------------
148 // Verify checkpoints
149 // ------------------------------------------------------------------------
151 @SuppressWarnings("null")
152 public void testTmfTraceMultiTimestamps() {
153 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
154 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
155 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
156 assertEquals("getRange-end", 102, fTrace
.getTimeRange().getEndTime().getValue());
157 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
158 assertEquals("getEndTime", 102, fTrace
.getEndTime().getValue());
160 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
161 assertTrue("Checkpoints exist", checkpoints
!= null);
162 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
+ 1, checkpoints
.size());
164 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
166 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
167 // and not the event at checkpoint boundary
168 TmfTimestamp seekTs
= new TmfTimestamp(101, -3, 0);
169 ITmfContext ctx
= fTrace
.seekEvent(seekTs
);
170 ITmfEvent event
= fTrace
.getNext(ctx
);
172 assertEquals(99, ctx
.getRank());
173 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
175 event
= fTrace
.getNext(ctx
);
177 assertEquals(100, ctx
.getRank());
178 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
180 event
= fTrace
.getNext(ctx
);
182 assertEquals(101, ctx
.getRank());
183 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
185 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
186 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
187 // and not the event at checkpoint boundary
188 seekTs
= new TmfTimestamp(102, -3, 0);
189 ctx
= fTrace
.seekEvent(seekTs
);
190 event
= fTrace
.getNext(ctx
);
192 assertEquals(102, ctx
.getRank());
193 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
195 // Verify seek to first checkpoint
196 seekTs
= new TmfTimestamp(1, -3, 0);
197 ctx
= fTrace
.seekEvent(seekTs
);
198 event
= fTrace
.getNext(ctx
);
200 assertEquals(1, ctx
.getRank());
201 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
203 // Verify seek to timestamp before first event
204 seekTs
= new TmfTimestamp(0, -3, 0);
205 ctx
= fTrace
.seekEvent(seekTs
);
206 event
= fTrace
.getNext(ctx
);
208 assertEquals(1, ctx
.getRank());
209 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event
.getTimestamp(), false));
211 // Verify seek to timestamp between first and second checkpoint
212 seekTs
= new TmfTimestamp(50, -3, 0);
213 ctx
= fTrace
.seekEvent(seekTs
);
214 event
= fTrace
.getNext(ctx
);
216 assertEquals(50, ctx
.getRank());
217 assertEquals(0, seekTs
.compareTo(event
.getTimestamp(), false));
219 // Verify seek to timestamp after last event in trace
220 seekTs
= new TmfTimestamp(103, -3, 0);
221 ctx
= fTrace
.seekEvent(seekTs
);
222 event
= fTrace
.getNext(ctx
);
224 assertEquals(-1, ctx
.getRank());