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