tmf: Add waitUntil / condition to tmf.ui.tests
[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;
f0beeb4a 39import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
a3d7df19
BH
40import org.junit.BeforeClass;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43
44/**
45 * SWTBot stress test for opening and closing of traces.
46 *
47 * @author Bernd Hufmann
48 */
49@RunWith(SWTBotJunit4ClassRunner.class)
50public class OpenTraceStressTest {
51
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";
c4d57ac1 54 private static final @NonNull CtfTestTrace CTF_TRACE = CtfTestTrace.SYNC_DEST;
a3d7df19
BH
55 private static final String TRACE_PROJECT_NAME = "test";
56
57 private static SWTWorkbenchBot workbenchbot;
58
59 /**
60 * Test Class setup
61 */
62 @BeforeClass
63 public static void init() {
5785ab49 64 SWTBotUtils.initialize();
a3d7df19
BH
65
66 /* Set up for swtbot */
67 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
68
69 workbenchbot = new SWTWorkbenchBot();
70
71 /* Close welcome view */
fa24d78b 72 SWTBotUtils.closeView("Welcome", workbenchbot);
a3d7df19
BH
73
74 /* Switch perspectives */
fa24d78b 75 SWTBotUtils.switchToPerspective(KERNEL_PERSPECTIVE_ID);
a3d7df19
BH
76
77 /* Finish waiting for eclipse to load */
f0beeb4a 78 WaitUtils.waitForJobs();
a3d7df19
BH
79 }
80
81 /**
82 * Main test case to test opening and closing of traces concurrently.
83 */
84 @Test
85 public void testOpenAndCloseConcurrency() {
fa24d78b 86 SWTBotUtils.createProject(TRACE_PROJECT_NAME);
a3d7df19 87
c4d57ac1 88 File fTestFile = new File(CtfTmfTestTraceUtils.getTrace(CTF_TRACE).getPath());
a3d7df19
BH
89
90 String path = fTestFile.getAbsolutePath();
91
92 assertNotNull(fTestFile);
93 assumeTrue(fTestFile.exists());
94
95 /*
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.
99 *
100 * Since the failure depends on timing it only happened sometimes before the bug fix
101 * using this test.
102 */
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() {
106 @Override
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());
113 }
114 }
115 };
116 mgr.addJobChangeListener(changeListener);
117 for (int i = 0; i < 10; i++) {
fa24d78b
AM
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);
a3d7df19 123 // Add little delay so that treads have a chance to start
fa24d78b 124 SWTBotUtils.delay(1000);
a3d7df19
BH
125 workbenchbot.closeAllEditors();
126
127 if (!status.isOK()) {
fa24d78b 128 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
a3d7df19
BH
129 fail(handleErrorStatus(status));
130 }
131 }
fa24d78b 132 SWTBotUtils.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
a3d7df19
BH
133 }
134
135 private static String handleErrorStatus(MultiStatus status) {
136
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');
144 }
145
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();
152 }
153
154 String stackMessage = getExceptionStackMessage(childException);
155 if (stackMessage == null) {
156 stackMessage = reason;
157 }
158
159 childSb.append(stackMessage);
160 }
161
162 if (childSb.length() > 0) {
163 childSb.insert(0, '\n');
164 sb.append(childSb.toString());
165 }
166 }
167 return sb.toString();
168 }
169
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);
175 ps.flush();
176 try {
177 baos.flush();
178 stackMessage = baos.toString();
179 } catch (IOException e) {
180 }
181
182 return stackMessage;
183 }
184}
This page took 0.06149 seconds and 5 git commands to generate.