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