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
CommitLineData
3eade83c 1/*******************************************************************************
6e1886bc 2 * Copyright (c) 2012, 2013 Ericsson
3eade83c
BH
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
6e1886bc 11 * Alexandre Montplaisir - Port to JUnit4
3eade83c
BH
12 *******************************************************************************/
13
a3db8436 14package org.eclipse.linuxtools.tmf.core.tests.trace.indexer.checkpoint;
3eade83c 15
6e1886bc
AM
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.assertTrue;
19
3eade83c
BH
20import java.io.File;
21import java.io.IOException;
22import java.net.URISyntaxException;
23import java.net.URL;
24import java.util.List;
25
3eade83c
BH
26import org.eclipse.core.runtime.FileLocator;
27import org.eclipse.core.runtime.Path;
28import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
3eade83c
BH
29import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
30import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
9c4d52ee 31import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace;
3bd46eef 32import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
3eade83c 33import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
a3db8436
AM
34import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
35import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
3eade83c
BH
36import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub;
37import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
6e1886bc
AM
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
3eade83c
BH
41
42/**
43 * Test suite for the TmfCheckpointIndexer class (events with same
44 * timestamp around checkpoint).
45 */
cad06250 46@SuppressWarnings("javadoc")
6e1886bc 47public class TmfCheckpointIndexTest2 {
3eade83c
BH
48
49 // ------------------------------------------------------------------------
50 // Variables
51 // ------------------------------------------------------------------------
52
3eade83c
BH
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
6e1886bc
AM
62 @Before
63 public void setUp() {
9c4d52ee
GB
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());
3eade83c
BH
67 }
68
6e1886bc
AM
69 @After
70 public void tearDown() {
3eade83c
BH
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 {
ff0960ac 97 super(path, blockSize, false, null, null);
3eade83c
BH
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);
51e75066 127 fTrace.indexTrace(true);
3eade83c
BH
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();
51e75066 139 fEmptyTrace.indexTrace(true);
3eade83c
BH
140 }
141 }
142
143 // ------------------------------------------------------------------------
144 // Verify checkpoints
145 // ------------------------------------------------------------------------
146
6e1886bc 147 @Test
3eade83c
BH
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.045535 seconds and 5 git commands to generate.