tmf: Remove equals()/hashCode() from TmfEventRequest
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests / src / org / eclipse / tracecompass / lttng2 / kernel / ui / swtbot / tests / OpenTraceStressTest.java
CommitLineData
a3d7df19
BH
1/*******************************************************************************
2 * Copyright (c) 2014 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
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;
30import org.eclipse.osgi.util.NLS;
31import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
32import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
33import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
34import org.eclipse.tracecompass.tmf.core.analysis.Messages;
35import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
36import org.eclipse.tracecompass.tmf.ui.swtbot.tests.SWTBotUtil;
37import org.junit.BeforeClass;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40
41/**
42 * SWTBot stress test for opening and closing of traces.
43 *
44 * @author Bernd Hufmann
45 */
46@RunWith(SWTBotJunit4ClassRunner.class)
47public class OpenTraceStressTest {
48
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";
53
54 private static SWTWorkbenchBot workbenchbot;
55
56 /**
57 * Test Class setup
58 */
59 @BeforeClass
60 public static void init() {
61 SWTBotUtil.failIfUIThread();
62
63 /* Set up for swtbot */
64 SWTBotPreferences.TIMEOUT = 20000; /* 20 second timeout */
65
66 workbenchbot = new SWTWorkbenchBot();
67
68 /* Close welcome view */
69 SWTBotUtil.closeView("Welcome", workbenchbot);
70
71 /* Switch perspectives */
72 SWTBotUtil.switchToPerspective(KERNEL_PERSPECTIVE_ID);
73
74 /* Finish waiting for eclipse to load */
75 SWTBotUtil.waitForJobs();
76 }
77
78 /**
79 * Main test case to test opening and closing of traces concurrently.
80 */
81 @Test
82 public void testOpenAndCloseConcurrency() {
83 SWTBotUtil.createProject(TRACE_PROJECT_NAME);
84
85 File fTestFile = new File(CTF_TRACE.getPath());
86
87 String path = fTestFile.getAbsolutePath();
88
89 assertNotNull(fTestFile);
90 assumeTrue(fTestFile.exists());
91
92 /*
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.
96 *
97 * Since the failure depends on timing it only happened sometimes before the bug fix
98 * using this test.
99 */
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() {
103 @Override
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());
110 }
111 }
112 };
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();
123
124 if (!status.isOK()) {
125 SWTBotUtil.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
126 fail(handleErrorStatus(status));
127 }
128 }
129 SWTBotUtil.deleteProject(TRACE_PROJECT_NAME, workbenchbot);
130 }
131
132 private static String handleErrorStatus(MultiStatus status) {
133
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');
141 }
142
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();
149 }
150
151 String stackMessage = getExceptionStackMessage(childException);
152 if (stackMessage == null) {
153 stackMessage = reason;
154 }
155
156 childSb.append(stackMessage);
157 }
158
159 if (childSb.length() > 0) {
160 childSb.insert(0, '\n');
161 sb.append(childSb.toString());
162 }
163 }
164 return sb.toString();
165 }
166
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);
172 ps.flush();
173 try {
174 baos.flush();
175 stackMessage = baos.toString();
176 } catch (IOException e) {
177 }
178
179 return stackMessage;
180 }
181}
This page took 0.032619 seconds and 5 git commands to generate.