Support for multiple events for same timestamp at checkpoint
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfCheckpointIndexTest2.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
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;
34
35 /**
36 * Test suite for the TmfCheckpointIndexer class (events with same
37 * timestamp around checkpoint).
38 */
39 @SuppressWarnings({"nls","javadoc"})
40 public class TmfCheckpointIndexTest2 extends TestCase {
41
42 // ------------------------------------------------------------------------
43 // Variables
44 // ------------------------------------------------------------------------
45
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;
54
55 // ------------------------------------------------------------------------
56 // Housekeeping
57 // ------------------------------------------------------------------------
58
59 /**
60 * @param name the test name
61 */
62 public TmfCheckpointIndexTest2(final String name) {
63 super(name);
64 }
65
66 @Override
67 protected void setUp() throws Exception {
68 super.setUp();
69 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
70 }
71
72 @Override
73 protected void tearDown() throws Exception {
74 super.tearDown();
75 fTrace.dispose();
76 fTrace = null;
77 fEmptyTrace.dispose();
78 fEmptyTrace = null;
79 }
80
81 // ------------------------------------------------------------------------
82 // Helper classes
83 // ------------------------------------------------------------------------
84
85 private static class TestIndexer extends TmfCheckpointIndexer {
86 @SuppressWarnings({ })
87 public TestIndexer(TestTrace testTrace) {
88 super(testTrace, BLOCK_SIZE);
89 }
90 @SuppressWarnings({ })
91 public TestIndexer(EmptyTestTrace testTrace) {
92 super(testTrace, BLOCK_SIZE);
93 }
94 public List<ITmfCheckpoint> getCheckpoints() {
95 return getTraceIndex();
96 }
97 }
98
99 private class TestTrace extends TmfTraceStub {
100 public TestTrace(String path, int blockSize) throws TmfTraceException {
101 super(path, blockSize);
102 setIndexer(new TestIndexer(this));
103 }
104 @Override
105 public TestIndexer getIndexer() {
106 return (TestIndexer) super.getIndexer();
107 }
108 }
109
110 private class EmptyTestTrace extends TmfEmptyTraceStub {
111 public EmptyTestTrace() {
112 super();
113 setIndexer(new TestIndexer(this));
114 }
115 @Override
116 public TestIndexer getIndexer() {
117 return (TestIndexer) super.getIndexer();
118 }
119 }
120
121 // ------------------------------------------------------------------------
122 // Helper functions
123 // ------------------------------------------------------------------------
124
125 private synchronized void setupTrace(final String path) {
126 if (fTrace == null) {
127 try {
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);
131 fTrace.indexTrace();
132 } catch (final TmfTraceException e) {
133 e.printStackTrace();
134 } catch (final URISyntaxException e) {
135 e.printStackTrace();
136 } catch (final IOException e) {
137 e.printStackTrace();
138 }
139 }
140
141 if (fEmptyTrace == null) {
142 fEmptyTrace = new EmptyTestTrace();
143 fEmptyTrace.indexTrace();
144 }
145 }
146
147 // ------------------------------------------------------------------------
148 // Verify checkpoints
149 // ------------------------------------------------------------------------
150
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());
159
160 List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints();
161 assertTrue("Checkpoints exist", checkpoints != null);
162 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE + 1, checkpoints.size());
163
164 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
165
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);
171
172 assertEquals(99, ctx.getRank());
173 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
174
175 event = fTrace.getNext(ctx);
176
177 assertEquals(100, ctx.getRank());
178 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
179
180 event = fTrace.getNext(ctx);
181
182 assertEquals(101, ctx.getRank());
183 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
184
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);
191
192 assertEquals(102, ctx.getRank());
193 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
194
195 // Verify seek to first checkpoint
196 seekTs = new TmfTimestamp(1, -3, 0);
197 ctx = fTrace.seekEvent(seekTs);
198 event = fTrace.getNext(ctx);
199
200 assertEquals(1, ctx.getRank());
201 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
202
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);
207
208 assertEquals(1, ctx.getRank());
209 assertEquals(0, new TmfTimestamp(1, -3, 0).compareTo(event.getTimestamp(), false));
210
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);
215
216 assertEquals(50, ctx.getRank());
217 assertEquals(0, seekTs.compareTo(event.getTimestamp(), false));
218
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);
223
224 assertEquals(-1, ctx.getRank());
225 assertNull(event);
226 }
227 }
This page took 0.0385 seconds and 5 git commands to generate.