1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.trace
;
15 import static org
.junit
.Assert
.assertArrayEquals
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.util
.concurrent
.TimeUnit
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
25 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfEnumPair
;
26 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
27 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
28 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
29 import org
.junit
.After
;
30 import org
.junit
.Before
;
31 import org
.junit
.Rule
;
32 import org
.junit
.Test
;
33 import org
.junit
.rules
.TestRule
;
34 import org
.junit
.rules
.Timeout
;
37 * More advanced CTF tests using "funky_trace", a trace generated with the
38 * Babeltrace CTF writer API, which has lots of fun things like different
39 * integer/float sizes and non-standard struct alignments.
41 * @author Alexandre Montplaisir
43 public class FunkyTraceTest
{
45 /** Time-out tests after 20 seconds */
47 public TestRule globalTimeout
= new Timeout(20, TimeUnit
.SECONDS
);
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.FUNKY_TRACE
;
54 private static final double DELTA
= 0.0000001;
56 private CtfTmfTrace fTrace
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
67 assumeTrue(testTrace
.exists());
68 fTrace
= testTrace
.getTrace();
69 fTrace
.indexTrace(true);
76 public void tearDown() {
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Verify the contents of the first event
90 public void testFirstEvent() {
91 CtfTmfEvent event
= getEvent(0);
92 assertEquals("Simple Event", event
.getType().getName());
93 assertEquals(1234567, event
.getTimestamp().getValue());
94 assertEquals(42, ((Long
) event
.getContent().getField("integer_field").getValue()).intValue());
95 assertEquals(3.1415, ((Double
) event
.getContent().getField("float_field").getValue()).doubleValue(), DELTA
);
99 * Verify the contents of the second event (the first "spammy event")
102 public void testSecondEvent() {
103 CtfTmfEvent event
= getEvent(1);
104 assertEquals("Spammy_Event", event
.getType().getName());
105 assertEquals(1234568, event
.getTimestamp().getValue());
106 assertEquals(0, ((Long
) event
.getContent().getField("field_1").getValue()).intValue());
107 assertEquals("This is a test", event
.getContent().getField("a_string").getValue());
111 * Verify the contents of the last "spammy event"
114 public void testSecondToLastEvent() {
115 CtfTmfEvent event
= getEvent(100000);
116 assertEquals("Spammy_Event", event
.getType().getName());
117 assertEquals(1334567, event
.getTimestamp().getValue());
118 assertEquals(99999, ((Long
) event
.getContent().getField("field_1").getValue()).intValue());
119 assertEquals("This is a test", event
.getContent().getField("a_string").getValue());
123 * Verify the contents of the last, complex event
126 public void testLastEvent() {
128 * Last event as seen in Babeltrace:
129 * [19:00:00.001334568] (+0.000000001) Complex Test Event: { }, {
130 * uint_35 = 0xDDF00D,
132 * complex_structure = {
133 * variant_selector = ( INT16_TYPE : container = 1 ),
134 * a_string = "Test string",
135 * variant_value = { INT16_TYPE = -200 },
136 * inner_structure = {
138 * a_sequence = [ [0] = 4, [1] = 3, [2] = 2, [3] = 1, [4] = 0, [5] = -1, [6] = -2, [7] = -3, [8] = -4, [9] = -5 ]
144 CtfTmfEvent event
= getEvent(100001);
145 assertEquals("Complex Test Event", event
.getType().getName());
146 assertEquals(1334568, event
.getTimestamp().getValue());
147 assertEquals(0xddf00d, ((Long
) event
.getContent().getField("uint_35").getValue()).intValue());
148 assertEquals(-12345, ((Long
) event
.getContent().getField("int_16").getValue()).intValue());
150 ITmfEventField
[] complexStruct
=
151 (ITmfEventField
[]) event
.getContent().getField("complex_structure").getValue();
153 assertEquals("variant_selector", complexStruct
[0].getName());
154 CtfEnumPair variant1
= (CtfEnumPair
) complexStruct
[0].getValue();
155 assertEquals("INT16_TYPE", variant1
.getStringValue());
156 assertEquals(Long
.valueOf(1), variant1
.getLongValue());
158 assertEquals("a_string", complexStruct
[1].getName());
159 assertEquals("Test string", complexStruct
[1].getValue());
161 assertEquals("variant_value", complexStruct
[2].getName());
162 ITmfEventField variantField
= (ITmfEventField
) complexStruct
[2].getValue();
163 assertEquals("INT16_TYPE", variantField
.getName());
164 assertEquals(Long
.valueOf(-200), variantField
.getValue());
166 ITmfEventField
[] innerStruct
= (ITmfEventField
[]) complexStruct
[3].getValue();
168 assertEquals("seq_len", innerStruct
[0].getName());
169 assertEquals(Long
.valueOf(10), innerStruct
[0].getValue());
171 assertEquals("a_sequence", innerStruct
[1].getName());
172 long[] seqValues
= (long[]) innerStruct
[1].getValue();
173 long[] expectedValues
= { 4, 3, 2, 1, 0, -1, -2, -3, -4, -5 };
174 assertArrayEquals(expectedValues
, seqValues
);
177 // ------------------------------------------------------------------------
179 // ------------------------------------------------------------------------
181 private synchronized CtfTmfEvent
getEvent(long index
) {
182 TestEventRequest req
= new TestEventRequest(index
);
183 fTrace
.sendRequest(req
);
185 req
.waitForCompletion();
186 } catch (InterruptedException e
) {
189 return req
.getEvent();
192 private class TestEventRequest
extends TmfEventRequest
{
194 private CtfTmfEvent fRetEvent
= null;
196 public TestEventRequest(long index
) {
197 super(CtfTmfEvent
.class,
198 TmfTimeRange
.ETERNITY
,
201 ExecutionType
.FOREGROUND
);
205 public void handleData(ITmfEvent event
) {
206 fRetEvent
= (CtfTmfEvent
) event
;
209 public CtfTmfEvent
getEvent() {