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
;
20 import java
.util
.concurrent
.BrokenBarrierException
;
21 import java
.util
.concurrent
.CyclicBarrier
;
22 import java
.util
.concurrent
.TimeUnit
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.Messages
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestStateSystemModule
;
32 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestStateSystemProvider
;
33 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.TmfXmlTraceStub
;
34 import org
.junit
.After
;
35 import org
.junit
.Before
;
36 import org
.junit
.Rule
;
37 import org
.junit
.Test
;
38 import org
.junit
.rules
.TestRule
;
39 import org
.junit
.rules
.Timeout
;
42 * Test the {@link TmfStateSystemAnalysisModule} class
44 * @author Geneviève Bastien
46 public class StateSystemAnalysisModuleTest
{
48 /** Time-out tests after 1 minute. */
50 public TestRule globalTimeout
= new Timeout(1, TimeUnit
.MINUTES
);
52 /** ID of the test state system analysis module */
53 public static final String MODULE_SS
= "org.eclipse.linuxtools.tmf.core.tests.analysis.sstest";
54 private static final String XML_TRACE
= "testfiles/stub_xml_traces/valid/analysis_dependency.xml";
56 private TestStateSystemModule fModule
;
57 private ITmfTrace fTrace
;
63 public void setupTraces() {
64 TmfXmlTraceStub trace
= TmfXmlTraceStub
.setupTrace(TmfCoreTestPlugin
.getAbsoluteFilePath(XML_TRACE
));
65 trace
.traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
68 fModule
= (TestStateSystemModule
) trace
.getAnalysisModule(MODULE_SS
);
72 * Some tests use traces, let's clean them here
75 public void cleanupTraces() {
80 * Test the state system module execution and result
83 public void testSsModule() {
84 ITmfStateSystem ss
= fModule
.getStateSystem();
87 if (fModule
.waitForCompletion()) {
88 ss
= fModule
.getStateSystem();
91 fail("Module did not complete properly");
96 * Make sure that the state system is initialized after calling
97 * {@link TmfStateSystemAnalysisModule#waitForInitialization()}.
100 public void testInitialization() {
101 assertNull(fModule
.getStateSystem());
104 assertTrue("Initialization succeeded", fModule
.waitForInitialization());
105 assertNotNull(fModule
.getStateSystem());
109 * Test that helper returns the right properties
112 public void testProperties() {
114 /* The stub state system has in mem backend 2 properties */
115 Map
<String
, String
> properties
= fModule
.getProperties();
116 assertEquals(fModule
.getBackendName(), properties
.get(Messages
.TmfStateSystemAnalysisModule_PropertiesBackend
));
117 assertEquals(fModule
.getId(), properties
.get(org
.eclipse
.tracecompass
.tmf
.core
.analysis
.Messages
.TmfAbstractAnalysisModule_LabelId
));
120 private static final String CRUCIAL_EVENT
= "crucialEvent";
121 private static final String CRUCIAL_FIELD
= "crucialInfo";
123 private static void setupDependentAnalysisHandler(CyclicBarrier barrier
) {
124 TestStateSystemProvider
.setEventHandler((ss
, event
) -> {
126 /* Wait before processing the current event */
128 if (event
.getName().equals(CRUCIAL_EVENT
)) {
129 String crucialInfo
= (String
) event
.getContent().getField(CRUCIAL_FIELD
).getValue();
130 int quark
= ss
.getQuarkAbsoluteAndAdd(CRUCIAL_FIELD
);
132 ss
.modifyAttribute(event
.getTimestamp().toNanos(), TmfStateValue
.newValueString(crucialInfo
), quark
);
133 } catch (Exception e
) {
134 fail(e
.getMessage());
137 /* Wait before processing the next event */
140 } catch (InterruptedException
| BrokenBarrierException e1
) {
148 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
151 public void testIsQueryable() {
153 CyclicBarrier barrier
= new CyclicBarrier(2);
154 setupDependentAnalysisHandler(barrier
);
156 TestStateSystemModule module
= fModule
;
157 assertNotNull(module
);
159 /* Module is not started, it should be queriable */
160 assertTrue(module
.isQueryable(1));
161 assertTrue(module
.isQueryable(4));
162 assertTrue(module
.isQueryable(5));
163 assertTrue(module
.isQueryable(7));
164 assertTrue(module
.isQueryable(10));
168 assertTrue(module
.waitForInitialization());
170 assertFalse(module
.isQueryable(1));
173 /* 2 waits for a barrier for one event */
179 assertTrue(module
.isQueryable(1));
180 assertTrue(module
.isQueryable(4));
181 assertFalse(module
.isQueryable(5));
185 assertTrue(module
.isQueryable(1));
186 assertTrue(module
.isQueryable(4));
187 assertFalse(module
.isQueryable(5));
191 assertTrue(module
.isQueryable(1));
192 assertTrue(module
.isQueryable(4));
193 assertFalse(module
.isQueryable(5));
197 assertTrue(module
.isQueryable(1));
198 assertTrue(module
.isQueryable(4));
199 assertTrue(module
.isQueryable(5));
200 assertFalse(module
.isQueryable(7));
204 assertTrue(module
.isQueryable(1));
205 assertTrue(module
.isQueryable(4));
206 assertTrue(module
.isQueryable(5));
207 assertFalse(module
.isQueryable(7));
211 assertTrue(module
.isQueryable(1));
212 assertTrue(module
.isQueryable(4));
213 assertTrue(module
.isQueryable(5));
214 assertTrue(module
.isQueryable(7));
215 assertFalse(module
.isQueryable(10));
218 fModule
.waitForCompletion();
219 assertTrue(module
.isQueryable(1));
220 assertTrue(module
.isQueryable(4));
221 assertTrue(module
.isQueryable(5));
222 assertTrue(module
.isQueryable(7));
223 assertTrue(module
.isQueryable(10));
225 // Should return true only if later than trace time
226 assertTrue(module
.isQueryable(100));
228 } catch (InterruptedException
| BrokenBarrierException e1
) {
229 fail(e1
.getMessage());
232 TestStateSystemProvider
.setEventHandler(null);
237 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
238 * when the analysis is cancelled
241 public void testIsQueryableCancel() {
243 TestStateSystemModule module
= fModule
;
244 assertNotNull(module
);
245 /* Set the queue to 1 to limit the number of events buffered */
246 module
.setPerEventSignalling(true);
248 /* Module is not started, it should be queriable */
249 assertTrue(module
.isQueryable(1));
250 assertTrue(module
.isQueryable(4));
251 assertTrue(module
.isQueryable(5));
252 assertTrue(module
.isQueryable(7));
253 assertTrue(module
.isQueryable(10));
257 assertTrue(module
.waitForInitialization());
259 assertFalse(module
.isQueryable(1));
261 // Process 2 events, then cancel
262 module
.signalNextEvent();
263 module
.signalNextEvent();
265 module
.setPerEventSignalling(false);
267 fModule
.waitForCompletion();
268 assertTrue(module
.isQueryable(1));
269 assertTrue(module
.isQueryable(4));
270 assertTrue(module
.isQueryable(5));
271 assertTrue(module
.isQueryable(7));
272 assertTrue(module
.isQueryable(10));