2e4a0b6dedcfc0ae934ffe10b48619ed3755c9d1
[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.util.Map;
20 import java.util.concurrent.BrokenBarrierException;
21 import java.util.concurrent.CyclicBarrier;
22 import java.util.concurrent.TimeUnit;
23
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;
40
41 /**
42 * Test the {@link TmfStateSystemAnalysisModule} class
43 *
44 * @author Geneviève Bastien
45 */
46 public class StateSystemAnalysisModuleTest {
47
48 /** Time-out tests after 1 minute. */
49 @Rule
50 public TestRule globalTimeout = new Timeout(1, TimeUnit.MINUTES);
51
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";
55
56 private TestStateSystemModule fModule;
57 private ITmfTrace fTrace;
58
59 /**
60 * Setup test trace
61 */
62 @Before
63 public void setupTraces() {
64 TmfXmlTraceStub trace = TmfXmlTraceStub.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(XML_TRACE));
65 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
66 fTrace = trace;
67
68 fModule = (TestStateSystemModule) trace.getAnalysisModule(MODULE_SS);
69 }
70
71 /**
72 * Some tests use traces, let's clean them here
73 */
74 @After
75 public void cleanupTraces() {
76 fTrace.dispose();
77 }
78
79 /**
80 * Test the state system module execution and result
81 */
82 @Test
83 public void testSsModule() {
84 ITmfStateSystem ss = fModule.getStateSystem();
85 assertNull(ss);
86 fModule.schedule();
87 if (fModule.waitForCompletion()) {
88 ss = fModule.getStateSystem();
89 assertNotNull(ss);
90 } else {
91 fail("Module did not complete properly");
92 }
93 }
94
95 /**
96 * Make sure that the state system is initialized after calling
97 * {@link TmfStateSystemAnalysisModule#waitForInitialization()}.
98 */
99 @Test
100 public void testInitialization() {
101 assertNull(fModule.getStateSystem());
102 fModule.schedule();
103
104 assertTrue("Initialization succeeded", fModule.waitForInitialization());
105 assertNotNull(fModule.getStateSystem());
106 }
107
108 /**
109 * Test that helper returns the right properties
110 */
111 @Test
112 public void testProperties() {
113
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));
118 }
119
120 private static final String CRUCIAL_EVENT = "crucialEvent";
121 private static final String CRUCIAL_FIELD = "crucialInfo";
122
123 private static void setupDependentAnalysisHandler(CyclicBarrier barrier) {
124 TestStateSystemProvider.setEventHandler((ss, event) -> {
125 try {
126 /* Wait before processing the current event */
127 barrier.await();
128 if (event.getName().equals(CRUCIAL_EVENT)) {
129 String crucialInfo = (String) event.getContent().getField(CRUCIAL_FIELD).getValue();
130 int quark = ss.getQuarkAbsoluteAndAdd(CRUCIAL_FIELD);
131 try {
132 ss.modifyAttribute(event.getTimestamp().toNanos(), TmfStateValue.newValueString(crucialInfo), quark);
133 } catch (Exception e) {
134 fail(e.getMessage());
135 }
136 }
137 /* Wait before processing the next event */
138 barrier.await();
139 return true;
140 } catch (InterruptedException | BrokenBarrierException e1) {
141 return false;
142 }
143
144 });
145 }
146
147 /**
148 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
149 */
150 @Test
151 public void testIsQueryable() {
152
153 CyclicBarrier barrier = new CyclicBarrier(2);
154 setupDependentAnalysisHandler(barrier);
155
156 TestStateSystemModule module = fModule;
157 assertNotNull(module);
158
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));
165
166 module.schedule();
167
168 assertTrue(module.waitForInitialization());
169
170 assertFalse(module.isQueryable(1));
171
172 try {
173 /* 2 waits for a barrier for one event */
174 // event 1
175 barrier.await();
176 barrier.await();
177 // event 2
178 barrier.await();
179 assertTrue(module.isQueryable(1));
180 assertTrue(module.isQueryable(4));
181 assertFalse(module.isQueryable(5));
182 barrier.await();
183 // event 3
184 barrier.await();
185 assertTrue(module.isQueryable(1));
186 assertTrue(module.isQueryable(4));
187 assertFalse(module.isQueryable(5));
188 barrier.await();
189 // event 4
190 barrier.await();
191 assertTrue(module.isQueryable(1));
192 assertTrue(module.isQueryable(4));
193 assertFalse(module.isQueryable(5));
194 barrier.await();
195 // event 5
196 barrier.await();
197 assertTrue(module.isQueryable(1));
198 assertTrue(module.isQueryable(4));
199 assertTrue(module.isQueryable(5));
200 assertFalse(module.isQueryable(7));
201 barrier.await();
202 // event 6
203 barrier.await();
204 assertTrue(module.isQueryable(1));
205 assertTrue(module.isQueryable(4));
206 assertTrue(module.isQueryable(5));
207 assertFalse(module.isQueryable(7));
208 barrier.await();
209 // event 7
210 barrier.await();
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));
216 barrier.await();
217
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));
224
225 // Should return true only if later than trace time
226 assertTrue(module.isQueryable(100));
227
228 } catch (InterruptedException | BrokenBarrierException e1) {
229 fail(e1.getMessage());
230 fModule.cancel();
231 } finally {
232 TestStateSystemProvider.setEventHandler(null);
233 }
234 }
235
236 /**
237 * Test the {@link TmfStateSystemAnalysisModule#isQueryable(long)} method
238 * when the analysis is cancelled
239 */
240 @Test
241 public void testIsQueryableCancel() {
242
243 TestStateSystemModule module = fModule;
244 assertNotNull(module);
245 /* Set the queue to 1 to limit the number of events buffered */
246 module.setPerEventSignalling(true);
247
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));
254
255 fModule.schedule();
256
257 assertTrue(module.waitForInitialization());
258
259 assertFalse(module.isQueryable(1));
260
261 // Process 2 events, then cancel
262 module.signalNextEvent();
263 module.signalNextEvent();
264 module.cancel();
265 module.setPerEventSignalling(false);
266
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));
273 }
274 }
This page took 0.037109 seconds and 4 git commands to generate.