ctf: Depend on the tracecompass-test-traces project
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests / src / org / eclipse / tracecompass / lttng2 / kernel / ui / swtbot / tests / OpenTraceStressTest.java
CommitLineData
a3d7df19 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 Ericsson
a3d7df19
BH
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
13package org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests;
14
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.fail;
17import static org.junit.Assume.assumeTrue;
18
19import java.io.ByteArrayOutputStream;
20import java.io.File;
21import java.io.IOException;
22import java.io.PrintStream;
23
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.core.runtime.MultiStatus;
26import org.eclipse.core.runtime.jobs.IJobChangeEvent;
27import org.eclipse.core.runtime.jobs.IJobManager;
28import org.eclipse.core.runtime.jobs.Job;
29import org.eclipse.core.runtime.jobs.JobChangeAdapter;
c4d57ac1 30import org.eclipse.jdt.annotation.NonNull;
a3d7df19
BH
31import org.eclipse.osgi.util.NLS;
32import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
33import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
34import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
c4d57ac1 35import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
a3d7df19 36import org.eclipse.tracecompass.tmf.core.analysis.Messages;
c4d57ac1 37import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
fa24d78b 38import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
a3d7df19
BH
39import org.junit.BeforeClass;
40import org.junit.Test;
41import 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)
49public 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";
c4d57ac1 53 private static final @NonNull CtfTestTrace CTF_TRACE = CtfTestTrace.SYNC_DEST;
a3d7df19
BH
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() {
fa24d78b 63 SWTBotUtils.failIfUIThread();
a3d7df19
BH
64
65 /* Set up for swtbot */
66 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
67
68 workbenchbot = new SWTWorkbenchBot();
69
70 /* Close welcome view */
fa24d78b 71 SWTBotUtils.closeView("Welcome", workbenchbot);
a3d7df19
BH
72
73 /* Switch perspectives */
fa24d78b 74 SWTBotUtils.switchToPerspective(KERNEL_PERSPECTIVE_ID);
a3d7df19
BH
75
76 /* Finish waiting for eclipse to load */
fa24d78b 77 SWTBotUtils.waitForJobs();
a3d7df19
BH
78 }
79
80 /**
81 * Main test case to test opening and closing of traces concurrently.
82 */
83 @Test
84 public void testOpenAndCloseConcurrency() {
fa24d78b 85 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
a3d7df19 86
c4d57ac1 87 File fTestFile = new File(CtfTmfTestTraceUtils.getTrace(CTF_TRACE).getPath());
a3d7df19
BH
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++) {
fa24d78b
AM
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);
a3d7df19 122 // Add little delay so that treads have a chance to start
fa24d78b 123 SWTBotUtils.delay(1000);
a3d7df19
BH
124 workbenchbot.closeAllEditors();
125
126 if (!status.isOK()) {
fa24d78b 127 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
a3d7df19
BH
128 fail(handleErrorStatus(status));
129 }
130 }
fa24d78b 131 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
a3d7df19
BH
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.087785 seconds and 5 git commands to generate.