14102c228081e06f262c48f93b414b80bcfef426
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests / src / org / eclipse / tracecompass / lttng2 / kernel / ui / swtbot / tests / OpenTraceStressTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests;
14
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.fail;
17 import static org.junit.Assume.assumeTrue;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.PrintStream;
23
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;
42
43 /**
44 * SWTBot stress test for opening and closing of traces.
45 *
46 * @author Bernd Hufmann
47 */
48 @RunWith(SWTBotJunit4ClassRunner.class)
49 public class OpenTraceStressTest {
50
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";
55
56 private static SWTWorkbenchBot workbenchbot;
57
58 /**
59 * Test Class setup
60 */
61 @BeforeClass
62 public static void init() {
63 SWTBotUtils.initialize();
64
65 /* Set up for swtbot */
66 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
67
68 workbenchbot = new SWTWorkbenchBot();
69
70 /* Close welcome view */
71 SWTBotUtils.closeView("Welcome", workbenchbot);
72
73 /* Switch perspectives */
74 SWTBotUtils.switchToPerspective(KERNEL_PERSPECTIVE_ID);
75
76 /* Finish waiting for eclipse to load */
77 SWTBotUtils.waitForJobs();
78 }
79
80 /**
81 * Main test case to test opening and closing of traces concurrently.
82 */
83 @Test
84 public void testOpenAndCloseConcurrency() {
85 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
86
87 File fTestFile = new File(CtfTmfTestTraceUtils.getTrace(CTF_TRACE).getPath());
88
89 String path = fTestFile.getAbsolutePath();
90
91 assertNotNull(fTestFile);
92 assumeTrue(fTestFile.exists());
93
94 /*
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.
98 *
99 * Since the failure depends on timing it only happened sometimes before the bug fix
100 * using this test.
101 */
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() {
105 @Override
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());
112 }
113 }
114 };
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();
125
126 if (!status.isOK()) {
127 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
128 fail(handleErrorStatus(status));
129 }
130 }
131 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
132 }
133
134 private static String handleErrorStatus(MultiStatus status) {
135
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');
143 }
144
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();
151 }
152
153 String stackMessage = getExceptionStackMessage(childException);
154 if (stackMessage == null) {
155 stackMessage = reason;
156 }
157
158 childSb.append(stackMessage);
159 }
160
161 if (childSb.length() > 0) {
162 childSb.insert(0, '\n');
163 sb.append(childSb.toString());
164 }
165 }
166 return sb.toString();
167 }
168
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);
174 ps.flush();
175 try {
176 baos.flush();
177 stackMessage = baos.toString();
178 } catch (IOException e) {
179 }
180
181 return stackMessage;
182 }
183 }
This page took 0.0371 seconds and 4 git commands to generate.