Copyright header update, 2015 edition
[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, 2014 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.event.TmfEvent;
29 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
30 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
31 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
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 {
83 @SuppressWarnings({ })
84 public TestIndexer(TestTrace testTrace) {
85 super(testTrace, BLOCK_SIZE);
86 }
87 @SuppressWarnings({ })
88 public TestIndexer(EmptyTestTrace testTrace) {
89 super(testTrace, BLOCK_SIZE);
90 }
91 public ITmfCheckpointIndex getCheckpoints() {
92 return getTraceIndex();
93 }
94 }
95
96 private class TestTrace extends TmfTraceStub {
97 public TestTrace(String path, int blockSize) throws TmfTraceException {
98 super(path, blockSize, false, null);
99 }
100
101 @Override
102 protected ITmfTraceIndexer createIndexer(int interval) {
103 return new TestIndexer(this);
104 }
105
106 @Override
107 public TestIndexer getIndexer() {
108 return (TestIndexer) super.getIndexer();
109 }
110 }
111
112 private class EmptyTestTrace extends TmfEmptyTraceStub {
113
114 public EmptyTestTrace() {
115 super();
116 init(getClass().getSimpleName(), TmfEvent.class);
117 }
118
119 @Override
120 protected ITmfTraceIndexer createIndexer(int interval) {
121 return new TestIndexer(this);
122 }
123
124 @Override
125 public TestIndexer getIndexer() {
126 return (TestIndexer) 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 fEmptyTrace = new EmptyTestTrace();
152 fEmptyTrace.indexTrace(true);
153 }
154 }
155
156 // ------------------------------------------------------------------------
157 // Verify checkpoints
158 // ------------------------------------------------------------------------
159
160 @Test
161 public void testTmfTraceMultiTimestamps() {
162 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
163 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
164 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
165 assertEquals("getRange-end", 102, fTrace.getTimeRange().getEndTime().getValue());
166 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
167 assertEquals("getEndTime", 102, fTrace.getEndTime().getValue());
168
169 ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
170 assertTrue("Checkpoints exist", checkpoints != null);
171 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE + 1, checkpoints.size());
172
173 // Trace has 3 events with same timestamp (ts=101) at rank 99, 100, 101
174
175 // Verify that the event at rank=99 is returned when seeking to ts=101 (first event with this timestamp)
176 // and not the event at checkpoint boundary
177 TmfTimestamp seekTs = new TmfTimestamp(101, -3);
178 ITmfContext ctx = fTrace.seekEvent(seekTs);
179 ITmfEvent event = fTrace.getNext(ctx);
180
181 assertEquals(99, ctx.getRank());
182 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
183
184 event = fTrace.getNext(ctx);
185
186 assertEquals(100, ctx.getRank());
187 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
188
189 event = fTrace.getNext(ctx);
190
191 assertEquals(101, ctx.getRank());
192 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
193
194 // Trace has events with same timestamp (ts=102) for ranks 102..702 -> 2 checkpoints with same timestamp are created
195 // Verify that the event at rank=102 is returned when seeking to ts=102 (first event with this timestamp)
196 // and not the event at checkpoint boundary
197 seekTs = new TmfTimestamp(102, -3);
198 ctx = fTrace.seekEvent(seekTs);
199 event = fTrace.getNext(ctx);
200
201 assertEquals(102, ctx.getRank());
202 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
203
204 // Verify seek to first checkpoint
205 seekTs = new TmfTimestamp(1, -3);
206 ctx = fTrace.seekEvent(seekTs);
207 event = fTrace.getNext(ctx);
208
209 assertEquals(1, ctx.getRank());
210 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
211
212 // Verify seek to timestamp before first event
213 seekTs = new TmfTimestamp(0, -3);
214 ctx = fTrace.seekEvent(seekTs);
215 event = fTrace.getNext(ctx);
216
217 assertEquals(1, ctx.getRank());
218 assertEquals(0, new TmfTimestamp(1, -3).compareTo(event.getTimestamp()));
219
220 // Verify seek to timestamp between first and second checkpoint
221 seekTs = new TmfTimestamp(50, -3);
222 ctx = fTrace.seekEvent(seekTs);
223 event = fTrace.getNext(ctx);
224
225 assertEquals(50, ctx.getRank());
226 assertEquals(0, seekTs.compareTo(event.getTimestamp()));
227
228 // Verify seek to timestamp after last event in trace
229 seekTs = new TmfTimestamp(103, -3);
230 ctx = fTrace.seekEvent(seekTs);
231 event = fTrace.getNext(ctx);
232
233 assertEquals(-1, ctx.getRank());
234 assertNull(event);
235 }
236 }
This page took 0.038785 seconds and 5 git commands to generate.