tmf: Add an XML trace stub with timestamps in seconds
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / statesystem / StateSystemAnalysisModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 École Polytechnique de Montréal
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
10 package org.eclipse.tracecompass.tmf.core.tests.statesystem;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.File;
20 import java.util.Map;
21 import java.util.concurrent.BrokenBarrierException;
22 import java.util.concurrent.CyclicBarrier;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
27 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
28 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
29 import org.eclipse.tracecompass.tmf.core.statesystem.Messages;
30 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
31 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
33 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestStateSystemModule;
34 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestStateSystemProvider;
35 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
36 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStubNs;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Ignore;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.TestRule;
43 import org.junit.rules.Timeout;
44
45 /**
46 * Test the {@link TmfStateSystemAnalysisModule} class
47 *
48 * @author Geneviève Bastien
49 */
50 public class StateSystemAnalysisModuleTest {
51
52 /** Time-out tests after 1 minute. */
53 @Rule
54 public TestRule globalTimeout = new Timeout(1, TimeUnit.MINUTES);
55
56 /** ID of the test state system analysis module */
57 public static final String MODULE_SS = "org.eclipse.linuxtools.tmf.core.tests.analysis.sstest";
58 private static final String XML_TRACE = "testfiles/stub_xml_traces/valid/analysis_dependency.xml";
59
60 private TestStateSystemModule fModule;
61 private ITmfTrace fTrace;
62
63 /**
64 * Setup test trace
65 */
66 @Before
67 public void setupTraces() {
68 TmfXmlTraceStub trace = TmfXmlTraceStubNs.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(XML_TRACE));
69 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
70 fTrace = trace;
71
72 fModule = (TestStateSystemModule) trace.getAnalysisModule(MODULE_SS);
73 }
74
75 /**
76 * Some tests use traces, let's clean them here
77 */
78 @After
79 public void cleanupTraces() {
80 fTrace.dispose();
81 }
82
83 /**
84 * Test the state system module execution and result
85 */
86 @Test
87 public void testSsModule() {
88 ITmfStateSystem ss = fModule.getStateSystem();
89 assertNull(ss);
90 fModule.schedule();
91 if (fModule.waitForCompletion()) {
92 ss = fModule.getStateSystem();
93 assertNotNull(ss);
94 } else {
95 fail("Module did not complete properly");
96 }
97 }
98
99 /**
100 * Make sure that the state system is initialized after calling
101 * {@link TmfStateSystemAnalysisModule#waitForInitialization()}.
102 */
103 @Test
104 public void testInitialization() {
105 assertNull(fModule.getStateSystem());
106 fModule.schedule();
107
108 assertTrue("Initialization succeeded", fModule.waitForInitialization());
109 assertNotNull(fModule.getStateSystem());
110 }
111
112 /**
113 * Test that helper returns the right properties
114 */
115 @Test
116 public void testProperties() {
117
118 /* The stub state system has in mem backend 2 properties */
119 Map<String, String> properties = fModule.getProperties();
120 assertEquals(fModule.getBackendName(), properties.get(Messages.TmfStateSystemAnalysisModule_PropertiesBackend));
121 assertEquals(fModule.getId(), properties.get(org.eclipse.tracecompass.tmf.core.analysis.Messages.TmfAbstractAnalysisModule_LabelId));
122 }
123
124 private static final String CRUCIAL_EVENT = "crucialEvent";
125 private static final String CRUCIAL_FIELD = "crucialInfo";
126
127 private static void setupDependentAnalysisHandler(CyclicBarrier barrier) {
128 TestStateSystemProvider.setEventHandler((ss, event) -> {
129 try {
130 /* Wait before processing the current event */
131 barrier.await();
132 if (event.getName().equals(CRUCIAL_EVENT)) {
133 String crucialInfo = (String) event.getContent().getField(CRUCIAL_FIELD).getValue();
134 int quark = ss.getQuarkAbsoluteAndAdd(CRUCIAL_FIELD);
135 try {
136 ss.modifyAttribute(event.getTimestamp().toNanos(), TmfStateValue.newValueString(crucialInfo), quark);
137 } catch (Exception e) {
138 fail(e.getMessage());
139 }
140 }
141 /* Wait before processing the next event */
142 barrier.await();
143 return true;
144 } catch (InterruptedException | BrokenBarrierException e1) {
145 return false;
146 }
147
148 });
149 }
150
151 /**
152 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
153 */
154 @Test
155 public void testIsQueryable() {
156
157 CyclicBarrier barrier = new CyclicBarrier(2);
158 setupDependentAnalysisHandler(barrier);
159
160 TestStateSystemModule module = fModule;
161 assertNotNull(module);
162
163 /* Module is not started, it should be queriable */
164 assertTrue(module.isQueryable(1));
165 assertTrue(module.isQueryable(4));
166 assertTrue(module.isQueryable(5));
167 assertTrue(module.isQueryable(7));
168 assertTrue(module.isQueryable(10));
169
170 module.schedule();
171
172 assertTrue(module.waitForInitialization());
173
174 assertFalse(module.isQueryable(1));
175
176 try {
177 /* 2 waits for a barrier for one event */
178 // event 1
179 barrier.await();
180 barrier.await();
181 // event 2
182 barrier.await();
183 assertTrue(module.isQueryable(1));
184 assertTrue(module.isQueryable(4));
185 assertFalse(module.isQueryable(5));
186 barrier.await();
187 // event 3
188 barrier.await();
189 assertTrue(module.isQueryable(1));
190 assertTrue(module.isQueryable(4));
191 assertFalse(module.isQueryable(5));
192 barrier.await();
193 // event 4
194 barrier.await();
195 assertTrue(module.isQueryable(1));
196 assertTrue(module.isQueryable(4));
197 assertFalse(module.isQueryable(5));
198 barrier.await();
199 // event 5
200 barrier.await();
201 assertTrue(module.isQueryable(1));
202 assertTrue(module.isQueryable(4));
203 assertTrue(module.isQueryable(5));
204 assertFalse(module.isQueryable(7));
205 barrier.await();
206 // event 6
207 barrier.await();
208 assertTrue(module.isQueryable(1));
209 assertTrue(module.isQueryable(4));
210 assertTrue(module.isQueryable(5));
211 assertFalse(module.isQueryable(7));
212 barrier.await();
213 // event 7
214 barrier.await();
215 assertTrue(module.isQueryable(1));
216 assertTrue(module.isQueryable(4));
217 assertTrue(module.isQueryable(5));
218 assertTrue(module.isQueryable(7));
219 assertFalse(module.isQueryable(10));
220 barrier.await();
221
222 fModule.waitForCompletion();
223 assertTrue(module.isQueryable(1));
224 assertTrue(module.isQueryable(4));
225 assertTrue(module.isQueryable(5));
226 assertTrue(module.isQueryable(7));
227 assertTrue(module.isQueryable(10));
228
229 // Should return true only if later than trace time
230 assertTrue(module.isQueryable(100));
231
232 } catch (InterruptedException | BrokenBarrierException e1) {
233 fail(e1.getMessage());
234 fModule.cancel();
235 } finally {
236 TestStateSystemProvider.setEventHandler(null);
237 }
238 }
239
240 /**
241 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
242 * when the analysis is cancelled
243 */
244 @Ignore("Hangs very often")
245 @Test
246 public void testIsQueryableCancel() {
247
248 TestStateSystemModule module = fModule;
249 assertNotNull(module);
250 /* Set the queue to 1 to limit the number of events buffered */
251 module.setPerEventSignalling(true);
252
253 /* Module is not started, it should be queriable */
254 assertTrue(module.isQueryable(1));
255 assertTrue(module.isQueryable(4));
256 assertTrue(module.isQueryable(5));
257 assertTrue(module.isQueryable(7));
258 assertTrue(module.isQueryable(10));
259
260 fModule.schedule();
261
262 assertTrue(module.waitForInitialization());
263
264 assertFalse(module.isQueryable(1));
265
266 // Process 2 events, then cancel
267 module.signalNextEvent();
268 module.signalNextEvent();
269 module.cancel();
270 module.setPerEventSignalling(false);
271
272 fModule.waitForCompletion();
273 assertTrue(module.isQueryable(1));
274 assertTrue(module.isQueryable(4));
275 assertTrue(module.isQueryable(5));
276 assertTrue(module.isQueryable(7));
277 assertTrue(module.isQueryable(10));
278 }
279
280 /**
281 * Test that an analysis with full backend is re-read correctly
282 * @throws TmfAnalysisException Propagates exceptions
283 */
284 @Test
285 public void testReReadFullAnalysis() throws TmfAnalysisException {
286 TestStateSystemModule module = new TestStateSystemModule(true);
287 TestStateSystemModule module2 = new TestStateSystemModule(true);
288 try {
289 ITmfTrace trace = fTrace;
290 assertNotNull(trace);
291 module.setTrace(trace);
292 module2.setTrace(trace);
293
294 // Execute the first module
295 module.schedule();
296 assertTrue(module.waitForCompletion());
297
298 // Execute the second module, it should read the state system file
299 File ssFile = module2.getSsFile();
300 assertNotNull(ssFile);
301 assertTrue(ssFile.exists());
302 module2.schedule();
303 assertTrue(module2.waitForCompletion());
304 } finally {
305 module.dispose();
306 module2.dispose();
307 }
308 }
309 }
This page took 0.038058 seconds and 5 git commands to generate.