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
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
.WaitUtils
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.Test
;
42 import org
.junit
.runner
.RunWith
;
45 * SWTBot stress test for opening and closing of traces.
47 * @author Bernd Hufmann
49 @RunWith(SWTBotJunit4ClassRunner
.class)
50 public class OpenTraceStressTest
{
52 private static final String TRACE_TYPE
= "org.eclipse.linuxtools.lttng2.kernel.tracetype";
53 private static final String KERNEL_PERSPECTIVE_ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.perspective";
54 private static final @NonNull CtfTestTrace CTF_TRACE
= CtfTestTrace
.SYNC_DEST
;
55 private static final String TRACE_PROJECT_NAME
= "test";
57 private static SWTWorkbenchBot workbenchbot
;
63 public static void init() {
64 SWTBotUtils
.initialize();
66 /* Set up for swtbot */
67 SWTBotPreferences
.TIMEOUT
= 20000; /* 20 second timeout */
69 workbenchbot
= new SWTWorkbenchBot();
71 /* Close welcome view */
72 SWTBotUtils
.closeView("Welcome", workbenchbot
);
74 /* Switch perspectives */
75 SWTBotUtils
.switchToPerspective(KERNEL_PERSPECTIVE_ID
);
77 /* Finish waiting for eclipse to load */
78 WaitUtils
.waitForJobs();
82 * Main test case to test opening and closing of traces concurrently.
85 public void testOpenAndCloseConcurrency() {
86 SWTBotUtils
.createProject(TRACE_PROJECT_NAME
);
88 File fTestFile
= new File(CtfTmfTestTraceUtils
.getTrace(CTF_TRACE
).getPath());
90 String path
= fTestFile
.getAbsolutePath();
92 assertNotNull(fTestFile
);
93 assumeTrue(fTestFile
.exists());
96 * This opening and closing of traces will trigger several threads for analysis which
97 * will be closed concurrently. There used to be a concurrency bug (447434) which should
98 * be fixed by now and this test should run without any exceptions.
100 * Since the failure depends on timing it only happened sometimes before the bug fix
103 final MultiStatus status
= new MultiStatus("lttn2.kernel.ui.swtbot.tests", IStatus
.OK
, null, null);
104 IJobManager mgr
= Job
.getJobManager();
105 JobChangeAdapter changeListener
= new JobChangeAdapter() {
107 public void done(IJobChangeEvent event
) {
108 Job job
= event
.getJob();
109 // Check for analysis failure
110 String jobNamePrefix
= NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, "");
111 if ((job
.getName().startsWith(jobNamePrefix
)) && (job
.getResult().getSeverity() == IStatus
.ERROR
)) {
112 status
.add(job
.getResult());
116 mgr
.addJobChangeListener(changeListener
);
117 for (int i
= 0; i
< 10; i
++) {
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 SWTBotUtils
.openTrace(TRACE_PROJECT_NAME
, path
, TRACE_TYPE
, false);
123 // Add little delay so that treads have a chance to start
124 SWTBotUtils
.delay(1000);
125 workbenchbot
.closeAllEditors();
127 if (!status
.isOK()) {
128 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
129 fail(handleErrorStatus(status
));
132 SWTBotUtils
.deleteProject(TRACE_PROJECT_NAME
, workbenchbot
);
135 private static String
handleErrorStatus(MultiStatus status
) {
137 // Build a string with all the children status messages, exception
138 // messages and stack traces
139 StringBuilder sb
= new StringBuilder();
140 for (IStatus childStatus
: status
.getChildren()) {
141 StringBuilder childSb
= new StringBuilder();
142 if (!childStatus
.getMessage().isEmpty()) {
143 childSb
.append(childStatus
.getMessage() + '\n');
146 Throwable childException
= childStatus
.getException();
147 if (childException
!= null) {
148 String reason
= childException
.getMessage();
149 // Some system exceptions have no message
150 if (reason
== null) {
151 reason
= childException
.toString();
154 String stackMessage
= getExceptionStackMessage(childException
);
155 if (stackMessage
== null) {
156 stackMessage
= reason
;
159 childSb
.append(stackMessage
);
162 if (childSb
.length() > 0) {
163 childSb
.insert(0, '\n');
164 sb
.append(childSb
.toString());
167 return sb
.toString();
170 private static String
getExceptionStackMessage(Throwable exception
) {
171 String stackMessage
= null;
172 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
173 PrintStream ps
= new PrintStream(baos
);
174 exception
.printStackTrace(ps
);
178 stackMessage
= baos
.toString();
179 } catch (IOException e
) {