tmf: Use default checkpoint indexer in TmfTrace empty constructor
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / indexer / checkpoint / TmfCheckpointIndexTest2.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.tests.trace.indexer.checkpoint;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
29 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
30 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
31 import org.eclipse.tracecompass.tmf.core.tests.trace.indexer.checkpoint.AbstractIndexTest.ITestIndexer;
32 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
34 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
35 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex;
36 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
37 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfEmptyTraceStub;
38 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 /**
44 * Test suite for the TmfCheckpointIndexer class (events with same
45 * timestamp around checkpoint).
46 */
47 @SuppressWarnings("javadoc")
48 public class TmfCheckpointIndexTest2 {
49
50 // ------------------------------------------------------------------------
51 // Variables
52 // ------------------------------------------------------------------------
53
54 private static final int BLOCK_SIZE = 100;
55 private static final int NB_EVENTS = 702;
56 private static TestTrace fTrace = null;
57 private static EmptyTestTrace fEmptyTrace = null;
58
59 // ------------------------------------------------------------------------
60 // Housekeeping
61 // ------------------------------------------------------------------------
62
63 @Before
64 public void setUp() {
65 // Trace has 3 events at t=101 at rank 99, 100, 101
66 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
67 setupTrace(TmfTestTrace.A_TEST_10K2.getFullPath());
68 }
69
70 @After
71 public void tearDown() {
72 fTrace.dispose();
73 fTrace = null;
74 fEmptyTrace.dispose();
75 fEmptyTrace = null;
76 }
77
78 // ------------------------------------------------------------------------
79 // Helper classes
80 // ------------------------------------------------------------------------
81
82 private static class TestIndexer extends TmfCheckpointIndexer implements ITestIndexer {
83 public TestIndexer(TestTrace testTrace) {
84 super(testTrace, BLOCK_SIZE);
85 }
86
87 public TestIndexer(EmptyTestTrace testTrace) {
88 super(testTrace, BLOCK_SIZE);
89 }
90
91 @Override
92 public ITmfCheckpointIndex getCheckpoints() {
93 return getTraceIndex();
94 }
95 }
96
97 private class TestTrace extends TmfTraceStub {
98 public TestTrace(String path, int blockSize) throws TmfTraceException {
99 super(path, blockSize, false, null);
100 }
101
102 @Override
103 protected ITmfTraceIndexer createIndexer(int interval) {
104 return new TestIndexer(this);
105 }
106
107 @Override
108 public ITestIndexer getIndexer() {
109 return (ITestIndexer) super.getIndexer();
110 }
111 }
112
113 private class EmptyTestTrace extends TmfEmptyTraceStub {
114
115 public EmptyTestTrace(String path) throws TmfTraceException {
116 super(path);
117 }
118
119 @Override
120 protected ITmfTraceIndexer createIndexer(int interval) {
121 return new TestIndexer(this);
122 }
123
124 @Override
125 public ITestIndexer getIndexer() {
126 return (ITestIndexer) super.getIndexer();
127 }
128 }
129
130 // ------------------------------------------------------------------------
131 // Helper functions
132 // ------------------------------------------------------------------------
133
134 private synchronized void setupTrace(final String path) {
135 if (fTrace == null) {
136 try {
137 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
138 final File test = new File(FileLocator.toFileURL(location).toURI());
139 fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
140 fTrace.indexTrace(true);
141 } catch (final TmfTraceException e) {
142 e.printStackTrace();
143 } catch (final URISyntaxException e) {
144 e.printStackTrace();
145 } catch (final IOException e) {
146 e.printStackTrace();
147 }
148 }
149
150 if (fEmptyTrace == null) {
151 try {
152 File file = File.createTempFile("empty", "txt");
153 fEmptyTrace = new EmptyTestTrace(file.getAbsolutePath());
154 } catch (TmfTraceException | IOException e) {
155 e.printStackTrace();
156 }
157 }
158 }
159
160 // ------------------------------------------------------------------------
161 // Verify checkpoints
162 // ------------------------------------------------------------------------
163
164 @Test
165 public void testTmfTraceMultiTimestamps() {
166 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
167 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
168 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
169 assertEquals("getRange-end", 102, fTrace.getTimeRange().getEndTime().getValue());
170 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
171 assertEquals("getEndTime", 102, fTrace.getEndTime().getValue());
172
173 ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
174 assertTrue("Checkpoints exist", checkpoints != null);
175 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE + 1, checkpoints.size());
176
177 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
178
179 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
180 // and not the event at checkpoint boundary
181 TmfTimestamp seekTs = new TmfTimestamp(101, -3);
182 ITmfContext ctx = fTrace.seekEvent(seekTs);
183 ITmfEvent event = fTrace.getNext(ctx);
184
185 assertEquals(99, ctx.getRank());
186 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
187
188 event = fTrace.getNext(ctx);
189
190 assertEquals(100, ctx.getRank());
191 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
192
193 event = fTrace.getNext(ctx);
194
195 assertEquals(101, ctx.getRank());
196 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
197
198 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
199 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
200 // and not the event at checkpoint boundary
201 seekTs = new TmfTimestamp(102, -3);
202 ctx = fTrace.seekEvent(seekTs);
203 event = fTrace.getNext(ctx);
204
205 assertEquals(102, ctx.getRank());
206 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
207
208 // Verify seek to first checkpoint
209 seekTs = new TmfTimestamp(1, -3);
210 ctx = fTrace.seekEvent(seekTs);
211 event = fTrace.getNext(ctx);
212
213 assertEquals(1, ctx.getRank());
214 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
215
216 // Verify seek to timestamp before first event
217 seekTs = new TmfTimestamp(0, -3);
218 ctx = fTrace.seekEvent(seekTs);
219 event = fTrace.getNext(ctx);
220
221 assertEquals(1, ctx.getRank());
222 assertEquals(0, new TmfTimestamp(1, -3).compareTo(event.getTimestamp()));
223
224 // Verify seek to timestamp between first and second checkpoint
225 seekTs = new TmfTimestamp(50, -3);
226 ctx = fTrace.seekEvent(seekTs);
227 event = fTrace.getNext(ctx);
228
229 assertEquals(50, ctx.getRank());
230 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
231
232 // Verify seek to timestamp after last event in trace
233 seekTs = new TmfTimestamp(103, -3);
234 ctx = fTrace.seekEvent(seekTs);
235 event = fTrace.getNext(ctx);
236
237 assertEquals(-1, ctx.getRank());
238 assertNull(event);
239 }
240 }
This page took 0.037524 seconds and 6 git commands to generate.