1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.lttng2
.kernel
.ui
.swtbot
.tests
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.fail
;
17 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.io
.ByteArrayOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.PrintStream
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.MultiStatus
;
26 import org
.eclipse
.core
.runtime
.jobs
.IJobChangeEvent
;
27 import org
.eclipse
.core
.runtime
.jobs
.IJobManager
;
28 import org
.eclipse
.core
.runtime
.jobs
.Job
;
29 import org
.eclipse
.core
.runtime
.jobs
.JobChangeAdapter
;
30 import org
.eclipse
.jdt
.annotation
.NonNull
;
31 import org
.eclipse
.osgi
.util
.NLS
;
32 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
33 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
34 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
35 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.Messages
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
38 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.shared
.SWTBotUtils
;
39 import org
.junit
.BeforeClass
;
40 import org
.junit
.Test
;
41 import org
.junit
.runner
.RunWith
;
44 * SWTBot stress test for opening and closing of traces.
46 * @author Bernd Hufmann
48 @RunWith(SWTBotJunit4ClassRunner
.class)
49 public class OpenTraceStressTest
{
51 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
52 private static final String KERNEL_PERSPECTIVE_ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
53 private static final @NonNull CtfTestTrace CTF_TRACE
= CtfTestTrace
.SYNC_DEST
;
54 private static final String TRACE_PROJECT_NAME
= "test";
56 private static SWTWorkbenchBot workbenchbot
;
62 public static void init() {
63 SWTBotUtils
.failIfUIThread();
65 /* Set up for swtbot */
66 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
68 workbenchbot
= new SWTWorkbenchBot();
70 /* Close welcome view */
71 SWTBotUtils
.closeView("Welcome", workbenchbot
);
73 /* Switch perspectives */
74 SWTBotUtils
.switchToPerspective(KERNEL_PERSPECTIVE_ID
);
76 /* Finish waiting for eclipse to load */
77 SWTBotUtils
.waitForJobs();
81 * Main test case to test opening and closing of traces concurrently.
84 public void testOpenAndCloseConcurrency() {
85 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
87 File fTestFile
= new File(CtfTmfTestTraceUtils
.getTrace(CTF_TRACE
).getPath());
89 String path
= fTestFile
.getAbsolutePath();
91 assertNotNull(fTestFile
);
92 assumeTrue(fTestFile
.exists());
95 * This opening and closing of traces will trigger several threads for analysis which
96 * will be closed concurrently. There used to be a concurrency bug (447434) which should
97 * be fixed by now and this test should run without any exceptions.
99 * Since the failure depends on timing it only happened sometimes before the bug fix
102 final MultiStatus status
= new MultiStatus("lttn2.kernel.ui.swtbot.tests", IStatus
.OK
, null, null);
103 IJobManager mgr
= Job
.getJobManager();
104 JobChangeAdapter changeListener
= new JobChangeAdapter() {
106 public void done(IJobChangeEvent event
) {
107 Job job
= event
.getJob();
108 // Check for analysis failure
109 String jobNamePrefix
= NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, "");
110 if ((job
.getName().startsWith(jobNamePrefix
)) && (job
.getResult().getSeverity() == IStatus
.ERROR
)) {
111 status
.add(job
.getResult());
115 mgr
.addJobChangeListener(changeListener
);
116 for (int i
= 0; i
< 10; i
++) {
117 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
118 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
119 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
120 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
121 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
122 // Add little delay so that treads have a chance to start
123 SWTBotUtils
.delay(1000);
124 workbenchbot
.closeAllEditors();
126 if (!status
.isOK()) {
127 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
128 fail(handleErrorStatus(status
));
131 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
134 private static String
handleErrorStatus(MultiStatus status
) {
136 // Build a string with all the children status messages, exception
137 // messages and stack traces
138 StringBuilder sb
= new StringBuilder();
139 for (IStatus childStatus
: status
.getChildren()) {
140 StringBuilder childSb
= new StringBuilder();
141 if (!childStatus
.getMessage().isEmpty()) {
142 childSb
.append(childStatus
.getMessage() + '\n');
145 Throwable childException
= childStatus
.getException();
146 if (childException
!= null) {
147 String reason
= childException
.getMessage();
148 // Some system exceptions have no message
149 if (reason
== null) {
150 reason
= childException
.toString();
153 String stackMessage
= getExceptionStackMessage(childException
);
154 if (stackMessage
== null) {
155 stackMessage
= reason
;
158 childSb
.append(stackMessage
);
161 if (childSb
.length() > 0) {
162 childSb
.insert(0, '\n');
163 sb
.append(childSb
.toString());
166 return sb
.toString();
169 private static String
getExceptionStackMessage(Throwable exception
) {
170 String stackMessage
= null;
171 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
172 PrintStream ps
= new PrintStream(baos
);
173 exception
.printStackTrace(ps
);
177 stackMessage
= baos
.toString();
178 } catch (IOException e
) {