ctf: Remove AutoCloseable from CtfTmfTrace
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / CtfTmfLostEventsTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ctf.core.tests.event;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assume.assumeTrue;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfLostEvent;
23 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
24 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
25 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
27 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
28 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
29 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
30 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 /**
36 * Tests to verify that lost events are handled correctly.
37 *
38 * Be wary if you are using Babeltrace to cross-check those values. There could
39 * be a bug in Babeltrace with regards to lost events. See
40 * http://bugs.lttng.org/issues/589
41 *
42 * It's not 100% sure at this point which implementation is correct, so for now
43 * these tests assume the Java implementation is the right one.
44 *
45 * @author Alexandre Montplaisir
46 */
47 public class CtfTmfLostEventsTest {
48
49 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.HELLO_LOST;
50
51 private CtfTmfTrace fixture = null;
52
53 /**
54 * Class setup
55 */
56 @Before
57 public void setUp() {
58 assumeTrue(testTrace.exists());
59 fixture = testTrace.getTrace();
60 fixture.indexTrace(true);
61 }
62
63 /**
64 * Clean-up
65 */
66 @After
67 public void tearDown() {
68 if (fixture != null) {
69 fixture.dispose();
70 }
71 }
72
73 // ------------------------------------------------------------------------
74 // Test methods
75 // ------------------------------------------------------------------------
76
77 /**
78 * Test that the number of events is reported correctly (a range of lost
79 * events is counted as one event).
80 */
81 @Test
82 public void testNbEvents() {
83 final long expectedReal = 32300;
84 final long expectedLost = 562;
85
86 EventCountRequest req = new EventCountRequest();
87 fixture.sendRequest(req);
88 try {
89 req.waitForCompletion();
90 } catch (InterruptedException e) {
91 e.printStackTrace();
92 }
93
94 assertEquals(expectedReal, req.getReal());
95 assertEquals(expectedLost, req.getLost());
96 }
97
98 /**
99 * Test that the number of events is reported correctly (a range of lost
100 * events is counted as one event). Events could be wrongly counted as lost
101 * events in certain situations.
102 */
103 @Test
104 public void testNbEventsBug475007() {
105 final CtfTmfTestTrace tmfTestTrace = CtfTmfTestTrace.DYNSCOPE;
106 assumeTrue(tmfTestTrace.exists());
107 CtfTmfTrace trace = tmfTestTrace.getTrace();
108 trace.indexTrace(true);
109
110 final long expectedReal = 100003;
111 final long expectedLost = 1;
112
113 EventCountRequest req = new EventCountRequest();
114 trace.sendRequest(req);
115 try {
116 req.waitForCompletion();
117 } catch (InterruptedException e) {
118 e.printStackTrace();
119 }
120
121 assertEquals(expectedReal, req.getReal());
122 assertEquals(expectedLost, req.getLost());
123
124 trace.dispose();
125 }
126
127 /**
128 * Test getting the first lost event from the trace.
129 */
130 @Test
131 public void testFirstLostEvent() {
132 final long rank = 190;
133 final ITmfTimestamp start = new TmfNanoTimestamp(1376592664828900165L);
134 final ITmfTimestamp end = new TmfNanoTimestamp(1376592664829403076L);
135 final long nbLost = 859;
136
137 final CtfTmfEvent ev = getOneEventTime(start);
138 /* Make sure seeking by rank yields the same event */
139 final CtfTmfEvent ev2 = getOneEventRank(rank);
140 assertEquals(ev, ev2);
141
142 assertTrue(ev instanceof ITmfLostEvent);
143 ITmfLostEvent event = (ITmfLostEvent) ev;
144
145 assertEquals(start, event.getTimestamp());
146 assertEquals(start, event.getTimeRange().getStartTime());
147 assertEquals(end, event.getTimeRange().getEndTime());
148 assertEquals(nbLost, event.getNbLostEvents());
149 }
150
151 /**
152 * Test getting the second lost event from the trace.
153 */
154 @Test
155 public void testSecondLostEvent() {
156 final long rank = 229;
157 final ITmfTimestamp start = new TmfNanoTimestamp(1376592664829477058L);
158 final ITmfTimestamp end = new TmfNanoTimestamp(1376592664829824514L);
159 final long nbLost = 488;
160
161 final CtfTmfEvent ev = getOneEventTime(start);
162 /* Make sure seeking by rank yields the same event */
163 final CtfTmfEvent ev2 = getOneEventRank(rank);
164 assertEquals(ev, ev2);
165
166 assertTrue(ev instanceof ITmfLostEvent);
167 ITmfLostEvent event = (ITmfLostEvent) ev;
168
169 assertEquals(start, event.getTimestamp());
170 assertEquals(start, event.getTimeRange().getStartTime());
171 assertEquals(end, event.getTimeRange().getEndTime());
172 assertEquals(nbLost, event.getNbLostEvents());
173 }
174
175 /**
176 * Test getting one normal event from the trace (lost events should not
177 * interfere).
178 */
179 @Test
180 public void testNormalEvent() {
181 final long rank = 200;
182 final ITmfTimestamp ts = new TmfNanoTimestamp(1376592664829425780L);
183
184 final CtfTmfEvent event = getOneEventTime(ts);
185 /* Make sure seeking by rank yields the same event */
186 final CtfTmfEvent event2 = getOneEventRank(rank);
187 assertEquals(event, event2);
188
189 assertFalse(event instanceof ITmfLostEvent);
190 assertEquals(ts, event.getTimestamp());
191 }
192
193 // ------------------------------------------------------------------------
194 // Event requests
195 // ------------------------------------------------------------------------
196
197 private CtfTmfEvent getOneEventRank(long rank) {
198 OneEventRequestPerRank req = new OneEventRequestPerRank(rank);
199 fixture.sendRequest(req);
200 try {
201 req.waitForCompletion();
202 } catch (InterruptedException e) {
203 e.printStackTrace();
204 }
205 return req.getEvent();
206 }
207
208 private CtfTmfEvent getOneEventTime(@NonNull ITmfTimestamp ts) {
209 OneEventRequestPerTs req = new OneEventRequestPerTs(ts);
210 fixture.sendRequest(req);
211 try {
212 req.waitForCompletion();
213 } catch (InterruptedException e) {
214 e.printStackTrace();
215 }
216 return req.getEvent();
217 }
218
219 private class OneEventRequestPerRank extends TmfEventRequest {
220
221 private CtfTmfEvent event = null;
222
223 public OneEventRequestPerRank(long rank) {
224 super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, rank, 1, ExecutionType.FOREGROUND);
225 }
226
227 @Override
228 public void handleData(ITmfEvent ev) {
229 /* Type is checked by the request, cast should be safe */
230 event = (CtfTmfEvent) ev;
231 }
232
233 public CtfTmfEvent getEvent() {
234 return event;
235 }
236 }
237
238 private class OneEventRequestPerTs extends TmfEventRequest {
239
240 private CtfTmfEvent event = null;
241
242 public OneEventRequestPerTs(@NonNull ITmfTimestamp ts) {
243 super(CtfTmfEvent.class,
244 new TmfTimeRange(ts, ts),
245 0, ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND);
246 }
247
248 @Override
249 public void handleData(ITmfEvent ev) {
250 event = (CtfTmfEvent) ev;
251 }
252
253 public CtfTmfEvent getEvent() {
254 return event;
255 }
256 }
257
258 private class EventCountRequest extends TmfEventRequest {
259
260 private long nbReal = 0;
261 private long nbLost = 0;
262
263 public EventCountRequest() {
264 super(CtfTmfEvent.class, TmfTimeRange.ETERNITY, 0,
265 ITmfEventRequest.ALL_DATA, ExecutionType.FOREGROUND);
266 }
267
268 @Override
269 public void handleData(ITmfEvent event) {
270 if (event instanceof ITmfLostEvent) {
271 nbLost++;
272 } else {
273 nbReal++;
274 }
275 }
276
277 public long getReal() {
278 return nbReal;
279 }
280
281 public long getLost() {
282 return nbLost;
283 }
284 }
285 }
This page took 0.040261 seconds and 6 git commands to generate.