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