1 /*******************************************************************************
2 * Copyright (c) 2014 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
.osgi
.util
.NLS
;
31 import org
.eclipse
.swtbot
.eclipse
.finder
.SWTWorkbenchBot
;
32 import org
.eclipse
.swtbot
.swt
.finder
.junit
.SWTBotJunit4ClassRunner
;
33 import org
.eclipse
.swtbot
.swt
.finder
.utils
.SWTBotPreferences
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.Messages
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.swtbot
.tests
.SWTBotUtil
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.Test
;
39 import org
.junit
.runner
.RunWith
;
42 * SWTBot stress test for opening and closing of traces.
44 * @author Bernd Hufmann
46 @RunWith(SWTBotJunit4ClassRunner
.class)
47 public class OpenTraceStressTest
{
49 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
50 private static final String KERNEL_PERSPECTIVE_ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
51 private static final CtfTmfTestTrace CTF_TRACE
= CtfTmfTestTrace
.SYNC_DEST
;
52 private static final String TRACE_PROJECT_NAME
= "test";
54 private static SWTWorkbenchBot workbenchbot
;
60 public static void init() {
61 SWTBotUtil
.failIfUIThread();
63 /* Set up for swtbot */
64 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
66 workbenchbot
= new SWTWorkbenchBot();
68 /* Close welcome view */
69 SWTBotUtil
.closeView("Welcome", workbenchbot
);
71 /* Switch perspectives */
72 SWTBotUtil
.switchToPerspective(KERNEL_PERSPECTIVE_ID
);
74 /* Finish waiting for eclipse to load */
75 SWTBotUtil
.waitForJobs();
79 * Main test case to test opening and closing of traces concurrently.
82 public void testOpenAndCloseConcurrency() {
83 SWTBotUtil
.createProject(TRACE_PROJECT_NAME
);
85 File fTestFile
= new File(CTF_TRACE
.getPath());
87 String path
= fTestFile
.getAbsolutePath();
89 assertNotNull(fTestFile
);
90 assumeTrue(fTestFile
.exists());
93 * This opening and closing of traces will trigger several threads for analysis which
94 * will be closed concurrently. There used to be a concurrency bug (447434) which should
95 * be fixed by now and this test should run without any exceptions.
97 * Since the failure depends on timing it only happened sometimes before the bug fix
100 final MultiStatus status
= new MultiStatus("lttn2.kernel.ui.swtbot.tests", IStatus
.OK
, null, null);
101 IJobManager mgr
= Job
.getJobManager();
102 JobChangeAdapter changeListener
= new JobChangeAdapter() {
104 public void done(IJobChangeEvent event
) {
105 Job job
= event
.getJob();
106 // Check for analysis failure
107 String jobNamePrefix
= NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, "");
108 if ((job
.getName().startsWith(jobNamePrefix
)) && (job
.getResult().getSeverity() == IStatus
.ERROR
)) {
109 status
.add(job
.getResult());
113 mgr
.addJobChangeListener(changeListener
);
114 for (int i
= 0; i
< 10; i
++) {
115 SWTBotUtil
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
116 SWTBotUtil
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
117 SWTBotUtil
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
118 SWTBotUtil
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
119 SWTBotUtil
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
120 // Add little delay so that treads have a chance to start
121 SWTBotUtil
.delay(1000);
122 workbenchbot
.closeAllEditors();
124 if (!status
.isOK()) {
125 SWTBotUtil
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
126 fail(handleErrorStatus(status
));
129 SWTBotUtil
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
132 private static String
handleErrorStatus(MultiStatus status
) {
134 // Build a string with all the children status messages, exception
135 // messages and stack traces
136 StringBuilder sb
= new StringBuilder();
137 for (IStatus childStatus
: status
.getChildren()) {
138 StringBuilder childSb
= new StringBuilder();
139 if (!childStatus
.getMessage().isEmpty()) {
140 childSb
.append(childStatus
.getMessage() + '\n');
143 Throwable childException
= childStatus
.getException();
144 if (childException
!= null) {
145 String reason
= childException
.getMessage();
146 // Some system exceptions have no message
147 if (reason
== null) {
148 reason
= childException
.toString();
151 String stackMessage
= getExceptionStackMessage(childException
);
152 if (stackMessage
== null) {
153 stackMessage
= reason
;
156 childSb
.append(stackMessage
);
159 if (childSb
.length() > 0) {
160 childSb
.insert(0, '\n');
161 sb
.append(childSb
.toString());
164 return sb
.toString();
167 private static String
getExceptionStackMessage(Throwable exception
) {
168 String stackMessage
= null;
169 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
170 PrintStream ps
= new PrintStream(baos
);
171 exception
.printStackTrace(ps
);
175 stackMessage
= baos
.toString();
176 } catch (IOException e
) {