tmf: Add a timeout to AnalysisModuleTest
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / analysis / AnalysisModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 É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 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.analysis;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.concurrent.TimeUnit;
25
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
32 import org.eclipse.tracecompass.tmf.core.analysis.Messages;
33 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
34 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
35 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestHelper;
36 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis;
39 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis2;
40 import org.junit.After;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.TestRule;
44 import org.junit.rules.Timeout;
45
46 /**
47 * Test suite for the {@link TmfAbstractAnalysisModule} class
48 */
49 public class AnalysisModuleTest {
50
51 /** Test timeout */
52 @Rule
53 public TestRule timeoutRule = new Timeout(30, TimeUnit.SECONDS);
54
55 private static final @NonNull String MODULE_GENERIC_ID = "test.id";
56 private static final @NonNull String MODULE_GENERIC_NAME = "Test analysis";
57
58 /**
59 * Some tests use traces, let's clean them here
60 */
61 @After
62 public void cleanupTraces() {
63 TmfTestTrace.A_TEST_10K.dispose();
64 }
65
66 /**
67 * Test suite for analysis module getters and setters
68 */
69 @Test
70 public void testGettersSetters() {
71 IAnalysisModule module = new TestAnalysis();
72
73 module.setName(MODULE_GENERIC_NAME);
74 module.setId(MODULE_GENERIC_ID);
75 assertEquals(MODULE_GENERIC_ID, module.getId());
76 assertEquals(MODULE_GENERIC_NAME, module.getName());
77
78 module.setAutomatic(false);
79 assertFalse(module.isAutomatic());
80 module.setAutomatic(true);
81 assertTrue(module.isAutomatic());
82 module.addParameter(TestAnalysis.PARAM_TEST);
83 assertNull(module.getParameter(TestAnalysis.PARAM_TEST));
84 module.setParameter(TestAnalysis.PARAM_TEST, 1);
85 assertEquals(1, module.getParameter(TestAnalysis.PARAM_TEST));
86
87 /* Try to set and get wrong parameter */
88 String wrongParam = "abc";
89 Exception exception = null;
90 try {
91 module.setParameter(wrongParam, 1);
92 } catch (RuntimeException e) {
93 exception = e;
94 assertEquals(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, wrongParam, module.getName()), e.getMessage());
95 }
96 assertNotNull(exception);
97 assertNull(module.getParameter(wrongParam));
98
99 module.dispose();
100 }
101
102 private static TestAnalysis setUpAnalysis() {
103 TestAnalysis module = new TestAnalysis();
104
105 module.setName(MODULE_GENERIC_NAME);
106 module.setId(MODULE_GENERIC_ID);
107 module.addParameter(TestAnalysis.PARAM_TEST);
108
109 return module;
110 }
111
112 /**
113 * Test suite for analysis module
114 * {@link TmfAbstractAnalysisModule#waitForCompletion} with successful
115 * execution
116 */
117 @Test
118 public void testWaitForCompletionSuccess() {
119 TestAnalysis module = setUpAnalysis();
120
121 IStatus status = module.schedule();
122 assertEquals(IStatus.ERROR, status.getSeverity());
123
124 /* Set a stub trace for analysis */
125 try {
126 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
127 } catch (TmfAnalysisException e) {
128 fail(e.getMessage());
129 }
130
131 /* Default execution, with output 1 */
132 module.setParameter(TestAnalysis.PARAM_TEST, 1);
133 status = module.schedule();
134 assertEquals(Status.OK_STATUS, status);
135 boolean completed = module.waitForCompletion();
136
137 assertTrue(completed);
138 assertEquals(1, module.getAnalysisOutput());
139
140 module.dispose();
141 }
142
143 /**
144 * Test suite for {@link TmfAbstractAnalysisModule#waitForCompletion} with
145 * cancellation
146 */
147 @Test
148 public void testWaitForCompletionCancelled() {
149 TestAnalysis module = setUpAnalysis();
150
151 /* Set a stub trace for analysis */
152 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
153 try {
154 assertTrue(module.setTrace(trace));
155 } catch (TmfAnalysisException e) {
156 fail(e.getMessage());
157 }
158
159 module.setParameter(TestAnalysis.PARAM_TEST, 0);
160 IStatus status = module.schedule();
161 assertEquals(Status.OK_STATUS, status);
162 boolean completed = module.waitForCompletion();
163
164 assertFalse(completed);
165 assertEquals(0, module.getAnalysisOutput());
166
167 module.dispose();
168 }
169
170 /**
171 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
172 * with wrong trace
173 */
174 @Test
175 public void testSetWrongTrace() {
176 IAnalysisModule module = new TestAnalysis2();
177
178 module.setName(MODULE_GENERIC_NAME);
179 module.setId(MODULE_GENERIC_ID);
180 assertEquals(MODULE_GENERIC_ID, module.getId());
181 assertEquals(MODULE_GENERIC_NAME, module.getName());
182
183 try {
184 assertFalse(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
185 } catch (TmfAnalysisException e) {
186 fail();
187 }
188
189 module.dispose();
190 }
191
192 /**
193 * Test the {@link TmfAbstractAnalysisModule#setTrace(ITmfTrace)} method
194 * with wrong trace
195 */
196 @Test
197 public void testSetTraceTwice() {
198 IAnalysisModule module = new TestAnalysis();
199
200 module.setName(MODULE_GENERIC_NAME);
201 module.setId(MODULE_GENERIC_ID);
202
203 try {
204 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
205 } catch (TmfAnalysisException e) {
206 fail();
207 }
208 TmfAnalysisException exception = null;
209 try {
210 module.setTrace(TmfTestTrace.A_TEST_10K.getTrace());
211 } catch (TmfAnalysisException e) {
212 exception = e;
213 }
214 assertNotNull(exception);
215
216 module.dispose();
217 }
218
219 /**
220 * Test suite for the {@link TmfAbstractAnalysisModule#cancel()} method
221 */
222 @Test
223 public void testCancel() {
224 TestAnalysis module = setUpAnalysis();
225
226 module.setParameter(TestAnalysis.PARAM_TEST, 999);
227 try {
228 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
229 } catch (TmfAnalysisException e) {
230 fail(e.getMessage());
231 }
232
233 assertEquals(Status.OK_STATUS, module.schedule());
234
235 /* Give the job a chance to start */
236 try {
237 Thread.sleep(1000);
238 } catch (InterruptedException e) {
239 fail(e.getMessage());
240 }
241
242 module.cancel();
243 assertFalse(module.waitForCompletion());
244 assertEquals(-1, module.getAnalysisOutput());
245
246 module.dispose();
247 }
248
249 /**
250 * Test suite for the {@link IAnalysisModule#notifyParameterChanged(String)}
251 * method
252 */
253 @Test
254 public void testParameterChanged() {
255 TestAnalysis module = setUpAnalysis();
256
257 try {
258 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
259 } catch (TmfAnalysisException e) {
260 fail(e.getMessage());
261 }
262
263 /* Check exception if no wrong parameter name */
264 Exception exception = null;
265 try {
266 module.notifyParameterChanged("aaa");
267 } catch (RuntimeException e) {
268 exception = e;
269 }
270 assertNotNull(exception);
271
272 /*
273 * Cannot test anymore of this method, need a parameter provider to do
274 * this
275 */
276 module.dispose();
277 }
278
279 /**
280 * Test the {@link TmfTestHelper#executeAnalysis(IAnalysisModule)} method
281 */
282 @Test
283 public void testHelper() {
284 TestAnalysis module = setUpAnalysis();
285
286 try {
287 assertTrue(module.setTrace(TmfTestTrace.A_TEST_10K.getTrace()));
288 } catch (TmfAnalysisException e) {
289 fail(e.getMessage());
290 }
291
292 module.setParameter(TestAnalysis.PARAM_TEST, 1);
293 boolean res = TmfTestHelper.executeAnalysis(module);
294 assertTrue(res);
295
296 module.setParameter(TestAnalysis.PARAM_TEST, 0);
297 res = TmfTestHelper.executeAnalysis(module);
298 assertFalse(res);
299
300 module.dispose();
301 }
302
303 /**
304 * Test the {@link TmfAbstractAnalysisModule} also executes the dependent
305 * analyses
306 */
307 @Test
308 public void testDependentAnalyses() {
309
310 ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
311 int paramAndResult = 5;
312
313 /* Setup the dependent module */
314 final String suffix = " dep";
315 final TestAnalysis depModule = new TestAnalysis() {
316
317 @Override
318 protected boolean executeAnalysis(IProgressMonitor monitor) {
319 try {
320 Thread.sleep(1000);
321 } catch (InterruptedException e) {
322 return false;
323 }
324 return super.executeAnalysis(monitor);
325 }
326
327 };
328 depModule.setName(MODULE_GENERIC_NAME + suffix);
329 depModule.setId(MODULE_GENERIC_ID + suffix);
330 depModule.addParameter(TestAnalysis.PARAM_TEST);
331 depModule.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
332
333 /* Prepare the main analysis with a dependent analysis */
334 TestAnalysis module = new TestAnalysis() {
335
336 @Override
337 protected Iterable<IAnalysisModule> getDependentAnalyses() {
338 Set<IAnalysisModule> modules = new HashSet<>();
339 modules.add(depModule);
340 return modules;
341 }
342
343 };
344
345 module.setName(MODULE_GENERIC_NAME);
346 module.setId(MODULE_GENERIC_ID);
347 module.addParameter(TestAnalysis.PARAM_TEST);
348 module.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
349
350 try {
351 assertTrue(depModule.setTrace(trace));
352 assertTrue(module.setTrace(trace));
353 } catch (TmfAnalysisException e) {
354 fail(e.getMessage());
355 }
356
357 /* Verify none of the module has run */
358 assertEquals(0, module.getAnalysisOutput());
359 assertEquals(0, depModule.getAnalysisOutput());
360
361 module.schedule();
362 assertTrue(module.waitForCompletion());
363 assertEquals(paramAndResult, module.getAnalysisOutput());
364
365 /* Make sure the dependent analysis has run and completed */
366 assertEquals(paramAndResult, depModule.getAnalysisOutput());
367
368 module.dispose();
369 depModule.dispose();
370 trace.dispose();
371
372 }
373 }
This page took 0.039315 seconds and 5 git commands to generate.