1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.statesystem
;
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
;
21 import java
.util
.concurrent
.BrokenBarrierException
;
22 import java
.util
.concurrent
.CyclicBarrier
;
23 import java
.util
.concurrent
.TimeUnit
;
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
;
46 * Test the {@link TmfStateSystemAnalysisModule} class
48 * @author Geneviève Bastien
50 public class StateSystemAnalysisModuleTest
{
52 /** Time-out tests after 1 minute. */
54 public TestRule globalTimeout
= new Timeout(1, TimeUnit
.MINUTES
);
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";
60 private TestStateSystemModule fModule
;
61 private ITmfTrace fTrace
;
67 public void setupTraces() {
68 TmfXmlTraceStub trace
= TmfXmlTraceStubNs
.setupTrace(TmfCoreTestPlugin
.getAbsoluteFilePath(XML_TRACE
));
69 trace
.traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
72 fModule
= (TestStateSystemModule
) trace
.getAnalysisModule(MODULE_SS
);
76 * Some tests use traces, let's clean them here
79 public void cleanupTraces() {
84 * Test the state system module execution and result
87 public void testSsModule() {
88 ITmfStateSystem ss
= fModule
.getStateSystem();
91 if (fModule
.waitForCompletion()) {
92 ss
= fModule
.getStateSystem();
95 fail("Module did not complete properly");
100 * Make sure that the state system is initialized after calling
101 * {@link TmfStateSystemAnalysisModule#waitForInitialization()}.
104 public void testInitialization() {
105 assertNull(fModule
.getStateSystem());
108 assertTrue("Initialization succeeded", fModule
.waitForInitialization());
109 assertNotNull(fModule
.getStateSystem());
113 * Test that helper returns the right properties
116 public void testProperties() {
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
));
124 private static final String CRUCIAL_EVENT
= "crucialEvent";
125 private static final String CRUCIAL_FIELD
= "crucialInfo";
127 private static void setupDependentAnalysisHandler(CyclicBarrier barrier
) {
128 TestStateSystemProvider
.setEventHandler((ss
, event
) -> {
130 /* Wait before processing the current event */
132 if (event
.getName().equals(CRUCIAL_EVENT
)) {
133 String crucialInfo
= (String
) event
.getContent().getField(CRUCIAL_FIELD
).getValue();
134 int quark
= ss
.getQuarkAbsoluteAndAdd(CRUCIAL_FIELD
);
136 ss
.modifyAttribute(event
.getTimestamp().toNanos(), TmfStateValue
.newValueString(crucialInfo
), quark
);
137 } catch (Exception e
) {
138 fail(e
.getMessage());
141 /* Wait before processing the next event */
144 } catch (InterruptedException
| BrokenBarrierException e1
) {
152 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
155 public void testIsQueryable() {
157 CyclicBarrier barrier
= new CyclicBarrier(2);
158 setupDependentAnalysisHandler(barrier
);
160 TestStateSystemModule module
= fModule
;
161 assertNotNull(module
);
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));
172 assertTrue(module
.waitForInitialization());
174 assertFalse(module
.isQueryable(1));
177 /* 2 waits for a barrier for one event */
183 assertTrue(module
.isQueryable(1));
184 assertTrue(module
.isQueryable(4));
185 assertFalse(module
.isQueryable(5));
189 assertTrue(module
.isQueryable(1));
190 assertTrue(module
.isQueryable(4));
191 assertFalse(module
.isQueryable(5));
195 assertTrue(module
.isQueryable(1));
196 assertTrue(module
.isQueryable(4));
197 assertFalse(module
.isQueryable(5));
201 assertTrue(module
.isQueryable(1));
202 assertTrue(module
.isQueryable(4));
203 assertTrue(module
.isQueryable(5));
204 assertFalse(module
.isQueryable(7));
208 assertTrue(module
.isQueryable(1));
209 assertTrue(module
.isQueryable(4));
210 assertTrue(module
.isQueryable(5));
211 assertFalse(module
.isQueryable(7));
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));
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));
229 // Should return true only if later than trace time
230 assertTrue(module
.isQueryable(100));
232 } catch (InterruptedException
| BrokenBarrierException e1
) {
233 fail(e1
.getMessage());
236 TestStateSystemProvider
.setEventHandler(null);
241 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
242 * when the analysis is cancelled
244 @Ignore("Hangs very often")
246 public void testIsQueryableCancel() {
248 TestStateSystemModule module
= fModule
;
249 assertNotNull(module
);
250 /* Set the queue to 1 to limit the number of events buffered */
251 module
.setPerEventSignalling(true);
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));
262 assertTrue(module
.waitForInitialization());
264 assertFalse(module
.isQueryable(1));
266 // Process 2 events, then cancel
267 module
.signalNextEvent();
268 module
.signalNextEvent();
270 module
.setPerEventSignalling(false);
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));
281 * Test that an analysis with full backend is re-read correctly
282 * @throws TmfAnalysisException Propagates exceptions
285 public void testReReadFullAnalysis() throws TmfAnalysisException
{
286 TestStateSystemModule module
= new TestStateSystemModule(true);
287 TestStateSystemModule module2
= new TestStateSystemModule(true);
289 ITmfTrace trace
= fTrace
;
290 assertNotNull(trace
);
291 module
.setTrace(trace
);
292 module2
.setTrace(trace
);
294 // Execute the first module
296 assertTrue(module
.waitForCompletion());
298 // Execute the second module, it should read the state system file
299 File ssFile
= module2
.getSsFile();
300 assertNotNull(ssFile
);
301 assertTrue(ssFile
.exists());
303 assertTrue(module2
.waitForCompletion());