Make test plugins fragments.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core.tests / src / org / eclipse / linuxtools / lttng / core / tests / LttngTestPreparation.java
1 package org.eclipse.linuxtools.lttng.core.tests;
2
3 import java.io.File;
4 import java.net.URL;
5
6 import junit.framework.TestCase;
7
8 import org.eclipse.core.runtime.FileLocator;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.linuxtools.internal.lttng.core.TraceDebug;
11 import org.eclipse.linuxtools.internal.lttng.core.control.LttngCoreProviderFactory;
12 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
13 import org.eclipse.linuxtools.internal.lttng.core.event.LttngSyntheticEvent;
14 import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
15 import org.eclipse.linuxtools.internal.lttng.core.event.LttngSyntheticEvent.SequenceInd;
16 import org.eclipse.linuxtools.internal.lttng.core.state.experiment.IStateExperimentManager;
17 import org.eclipse.linuxtools.internal.lttng.core.state.experiment.StateManagerFactory;
18 import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTextTrace;
19 import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTrace;
20 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
21 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
22 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
23 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
24 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
25 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
27 import org.osgi.framework.FrameworkUtil;
28
29 @SuppressWarnings("nls")
30 public abstract class LttngTestPreparation extends TestCase {
31 // ========================================================================
32 // Data
33 // ========================================================================
34 private final static String ftracepath_T1 = "traceset/trace-15316events_nolost_newformat";
35 final static String fTextTracepath_T1 = "traceset/trace-15316events_nolost_newformat.txt";
36
37 private static final Long CHECK_POINT_INTERVAL = 1000L;
38
39 final Long[] expectedEvents_T1 = new Long[20];
40 final Long[] requestIntervals_T1 = new Long[32];
41
42 static LTTngTextTrace ftextStream_T1 = null;
43 private static LTTngTrace frealStream = null;
44
45 private TmfExperiment<LttngEvent> fTestExperiment = null;
46 protected volatile int feventCount = 0;
47 protected boolean validSequence = true;
48
49 public LttngTestPreparation() {
50 super();
51 init();
52 }
53
54 public LttngTestPreparation(String name) {
55 super(name);
56 init();
57 }
58
59 protected void init() {
60 fillInRequestIntervals();
61 fillInExpectedEvents();
62 feventCount = 0;
63 }
64
65 /**
66 * @return
67 */
68 protected TmfExperiment<LttngEvent> prepareExperimentToTest() {
69 if (fTestExperiment == null) {
70 String expId = "testExperiment";
71 int nbTraces = 1;
72
73 // Define traces in experiment
74 @SuppressWarnings("unchecked")
75 ITmfTrace<LttngEvent>[] traces = new ITmfTrace[nbTraces];
76 ITmfTrace<LttngEvent> trace = prepareStreamToTest();
77 traces[0] = trace;
78
79 // create experiment and associate traces
80 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
81 expId, traces, TmfTimestamp.ZERO, TmfExperiment.DEFAULT_BLOCK_SIZE, true);
82 // fTestExperiment.indexExperiment(waitForCompletion);
83
84 // Set the current selected experiment as the test experiment
85 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
86 this, fTestExperiment);
87 fTestExperiment.experimentSelected(signal);
88 }
89
90 return fTestExperiment;
91 }
92
93 /**
94 * @return
95 */
96 protected TmfExperiment<LttngEvent> prepareTextExperimentToTest() {
97 if (fTestExperiment == null) {
98 String expId = "testExperiment";
99 int nbTraces = 1;
100
101 // Define traces in experiment
102 @SuppressWarnings("unchecked")
103 ITmfTrace<LttngEvent>[] traces = new ITmfTrace[nbTraces];
104 ITmfTrace<LttngEvent> trace = prepareTextStreamToTest();
105 traces[0] = trace;
106
107 // create experiment and associate traces
108 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
109 expId, traces);
110
111 // Set the current selected experiment as the test experiment
112 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
113 this, fTestExperiment);
114 fTestExperiment.experimentSelected(signal);
115
116 }
117
118 return fTestExperiment;
119 }
120
121 protected LTTngTrace prepareStreamToTest() {
122 if (frealStream == null) {
123 try {
124 URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path(ftracepath_T1),
125 null);
126 File testfile = new File(FileLocator.toFileURL(location).toURI());
127 LTTngTrace tmpStream = new LTTngTrace(testfile.getName(), testfile.getPath(), false);
128 frealStream = tmpStream;
129 } catch (Exception e) {
130 System.out.println("ERROR : Could not open " + ftracepath_T1);
131 frealStream = null;
132 }
133 } else {
134 frealStream.seekEvent(0L);
135 }
136
137 return frealStream;
138 }
139
140 protected LTTngTextTrace prepareTextStreamToTest() {
141 if (ftextStream_T1 == null) {
142 try {
143 URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()),
144 new Path(fTextTracepath_T1), null);
145 File testfile = new File(FileLocator.toFileURL(location).toURI());
146 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getName(), testfile.getPath());
147 ftextStream_T1 = tmpStream;
148
149 } catch (Exception e) {
150 System.out.println("ERROR : Could not open " + fTextTracepath_T1);
151 ftextStream_T1 = null;
152 }
153 } else {
154 ftextStream_T1.seekEvent(0);
155 }
156
157 return ftextStream_T1;
158 }
159
160 protected IStateExperimentManager prepareExperimentContext(
161 boolean waitForRequest) {
162 // Create a new Experiment manager
163 IStateExperimentManager expManager = StateManagerFactory
164 .getExperimentManager();
165 // Configure the interval to create check points so this can be tested
166 // with medium size files i.e. default is 50000 events
167 StateManagerFactory.setTraceCheckPointInterval(CHECK_POINT_INTERVAL);
168
169 // Lets wait for the request completion to analyse the results
170 LttngCoreProviderFactory.getEventProvider(0)
171 .setWaitForRequest(waitForRequest);
172 return expManager;
173 }
174
175 /**
176 * @param <T>
177 * @param k
178 * @param startIdx
179 * , > 0 and between 0 - 31
180 * @param endIdx
181 * , > startIdx and between 0 - 31
182 * @return
183 */
184 protected <T extends LttngEvent> TmfEventRequest<T> prepareEventRequest(Class<T> k, int startIdx, int endIdx) {
185 return prepareEventRequest(k, startIdx, endIdx, true);
186 }
187
188 /**
189 * @param <T>
190 * @param k
191 * @param startIdx
192 * , > 0 and between 0 - 31
193 * @param endIdx
194 * , > startIdx and between 0 - 31
195 * @param printFirst20
196 * , print the first expected events vs actual events
197 * @return
198 */
199 protected <T extends LttngEvent> TmfEventRequest<T> prepareEventRequest(
200 Class<T> k, final int startIdx, int endIdx, final boolean printFirst20) {
201 // verify bounds
202 if (!(endIdx > startIdx && startIdx >= 0 && endIdx <= 31)) {
203 TraceDebug.debug("Event request indexes out of bounds");
204 return null;
205 }
206
207 int DEFAULT_CHUNK = 1;
208
209 // time range
210 TmfTimeRange trange = new TmfTimeRange(new LttngTimestamp(
211 requestIntervals_T1[startIdx]), new LttngTimestamp(
212 requestIntervals_T1[endIdx]));
213
214 // request
215 validSequence = true;
216 TmfEventRequest<T> request = new TmfEventRequest<T>(k, trange, TmfDataRequest.ALL_DATA, DEFAULT_CHUNK) {
217
218 @Override
219 public void handleData(T event) {
220 if (event == null) {
221 System.out
222 .println("Syntheric Event Received is null, after event: "
223 + feventCount);
224 return;
225 }
226
227 // Listen to only one variant of synthetic event to keep
228 // track of
229 if (event instanceof LttngSyntheticEvent) {
230 if (((LttngSyntheticEvent) event).getSynType() != SequenceInd.BEFORE) {
231 return;
232 }
233 }
234
235 // Validating the orders of the first 20 events
236 if (printFirst20 && feventCount < 20) {
237 long timevalue = event.getTimestamp().getValue();
238 if (timevalue != expectedEvents_T1[feventCount]) {
239 validSequence = false;
240 System.out.println("Expected Event: "
241 + expectedEvents_T1[feventCount] + " actual: "
242 + event.getTimestamp().getValue());
243 }
244 }
245
246 // increment count
247 incrementCount();
248 }
249
250 /**
251 * possibly increased by multiple request threads
252 */
253 private synchronized void incrementCount() {
254 feventCount++;
255 }
256
257 @Override
258 public void handleCompleted() {
259 // if (isCancelled() || isFailed()) {
260 // // No notification to end request handlers
261 // } else {
262 // // notify the associated end request handlers
263 // requestCompleted();
264 // }
265
266 // System.out.println("handleCompleted(request:" + startIdx + ") Number of events processed: " + feventCount);
267 }
268
269 };
270 return request;
271 }
272
273 /**
274 * @param <T>
275 * @param k
276 * @param startIdx
277 * , > 0 and between 0 - 31
278 * @param endIdx
279 * , > startIdx and between 0 - 31
280 * @param printFirst20
281 * , print the first expected events vs actual events
282 * @return
283 */
284 protected <T extends LttngEvent> TmfEventRequest<T> prepareEventRequest2(
285 Class<T> k, final int startIdx, int endIdx, final boolean printFirst20) {
286 // verify bounds
287 if (!(endIdx > startIdx && startIdx >= 0 && endIdx <= 31)) {
288 TraceDebug.debug("Event request indexes out of bounds");
289 return null;
290 }
291
292 int DEFAULT_CHUNK = 1;
293
294 // time range
295 TmfTimeRange trange = new TmfTimeRange(new LttngTimestamp(
296 requestIntervals_T1[startIdx]), new LttngTimestamp(
297 requestIntervals_T1[endIdx]));
298
299 // request
300 validSequence = true;
301 TmfEventRequest<T> request = new TmfEventRequest<T>(k, trange, TmfDataRequest.ALL_DATA, DEFAULT_CHUNK) {
302
303 @Override
304 public void handleData(T event) {
305 if (event == null) {
306 System.out
307 .println("Syntheric Event Received is null, after event: "
308 + feventCount);
309 return;
310 }
311
312 // Listen to only one variant of synthetic event to keep
313 // track of
314 if (event instanceof LttngSyntheticEvent) {
315 if (((LttngSyntheticEvent) event).getSynType() != SequenceInd.BEFORE) {
316 return;
317 }
318 }
319
320 // Validating the orders of the first 20 events
321 if (printFirst20 && feventCount < 20) {
322 long timevalue = event.getTimestamp().getValue();
323 if (timevalue != expectedEvents_T1[feventCount]) {
324 validSequence = false;
325 System.out.println("Expected Event: "
326 + expectedEvents_T1[feventCount] + " actual: "
327 + event.getTimestamp().getValue());
328 } else {
329 System.out.println("Synthetic Event: " + feventCount
330 + " matched expected time");
331 }
332 }
333
334 // increment count
335 incrementCount();
336 }
337
338 /**
339 * possibly increased by multiple request threads
340 */
341 private synchronized void incrementCount() {
342 feventCount++;
343 }
344
345 @Override
346 public void handleCompleted() {
347 // if (isCancelled() || isFailed()) {
348 // // No notification to end request handlers
349 // } else {
350 // // notify the associated end request handlers
351 // requestCompleted();
352 // }
353
354 // System.out.println("handleCompleted(request:" + startIdx + ") Number of events processed: " + feventCount);
355 }
356
357 };
358 return request;
359 }
360
361 /**
362 * Validation points
363 */
364 private void fillInExpectedEvents() {
365 expectedEvents_T1[0] = 13589759412128L;
366 expectedEvents_T1[1] = 13589759419903L;
367 expectedEvents_T1[2] = 13589759422785L;
368 expectedEvents_T1[3] = 13589759425598L;
369 expectedEvents_T1[4] = 13589759430979L;
370 expectedEvents_T1[5] = 13589759433694L;
371 expectedEvents_T1[6] = 13589759436212L;
372 expectedEvents_T1[7] = 13589759438797L;
373 expectedEvents_T1[8] = 13589759441253L;
374 expectedEvents_T1[9] = 13589759444795L;
375 expectedEvents_T1[10] = 13589759447800L;
376 expectedEvents_T1[11] = 13589759450836L;
377 expectedEvents_T1[12] = 13589759453835L;
378 expectedEvents_T1[13] = 13589759459351L;
379 expectedEvents_T1[14] = 13589759464411L;
380 expectedEvents_T1[15] = 13589759467021L;
381 expectedEvents_T1[16] = 13589759469668L;
382 expectedEvents_T1[17] = 13589759474938L;
383 expectedEvents_T1[18] = 13589759477536L;
384 expectedEvents_T1[19] = 13589759480485L;
385 }
386
387 /**
388 * Intervals for trace 1, separated %500 + last event
389 */
390 private void fillInRequestIntervals() {
391 requestIntervals_T1[0] = 13589759412128L; /* check point expected */
392 requestIntervals_T1[1] = 13589763490945L; /* between check points */
393 requestIntervals_T1[2] = 13589778265041L; /* check point expected */
394 requestIntervals_T1[3] = 13589783143445L; /* between check points */
395 requestIntervals_T1[4] = 13589786300104L; /* check point expected */
396 requestIntervals_T1[5] = 13589790722564L; /* between check points */
397 requestIntervals_T1[6] = 13589796139823L; /* check point expected */
398 requestIntervals_T1[7] = 13589800400562L; /* between check points */
399 requestIntervals_T1[8] = 13589801594374L; /* check point expected */
400 requestIntervals_T1[9] = 13589802750295L; /* between check points */
401 requestIntervals_T1[10] = 13589804071157L; /* check point expected */
402 requestIntervals_T1[11] = 13589810124488L; /* between check points */
403 requestIntervals_T1[12] = 13589822493183L; /* check point expected */
404 requestIntervals_T1[13] = 13589824131273L; /* between check points */
405 requestIntervals_T1[14] = 13589825398284L; /* check point expected */
406 requestIntervals_T1[15] = 13589826664185L; /* between check points */
407 requestIntervals_T1[16] = 13589827811998L; /* check point expected */
408 requestIntervals_T1[17] = 13589828915763L; /* between check points */
409 requestIntervals_T1[18] = 13589830074220L; /* check point expected */
410 requestIntervals_T1[19] = 13589831232050L; /* between check points */
411 requestIntervals_T1[20] = 13589832394049L; /* check point expected */
412 requestIntervals_T1[21] = 13589833852883L; /* between check points */
413 requestIntervals_T1[22] = 13589839626892L; /* check point expected */
414 requestIntervals_T1[23] = 13589849509798L; /* between check points */
415 requestIntervals_T1[24] = 13589850728538L; /* check point expected */
416 requestIntervals_T1[25] = 13589851889230L; /* between check points */
417 requestIntervals_T1[26] = 13589853294800L; /* check point expected */
418 requestIntervals_T1[27] = 13589859414998L; /* between check points */
419 requestIntervals_T1[28] = 13589878046089L; /* check point expected */
420 requestIntervals_T1[29] = 13589886468603L; /* between check points */
421 requestIntervals_T1[30] = 13589902256918L; /* check point expected */
422 requestIntervals_T1[31] = 13589906758692L; /* last event in T1 */
423 }
424
425 }
This page took 0.040132 seconds and 5 git commands to generate.