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