From 217d5c8162282eb789d9fcff663e2c5129eeb686 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Mon, 19 Sep 2016 01:27:43 -0400 Subject: [PATCH] Make waitForJobs available to non-SWTBot test plugins The waitForJobs pattern is used in a few other places that SWTBot but they do not have timeouts and the ability to print the stack traces when it times out. By moving the code to the common plugin, all tests can use it and benefit from it. Change-Id: I56a19e5da30216d53a40f44dc3302b427985b1ae Signed-off-by: Marc-Andre Laperle Reviewed-on: https://git.eclipse.org/r/81304 Reviewed-by: Hudson CI Reviewed-by: Matthew Khouzam --- .../META-INF/MANIFEST.MF | 1 + .../component/TraceControlTestFacility.java | 6 +- .../ui/swtbot/tests/shared/SWTBotUtils.java | 56 +--------- .../tmf/ui/tests/shared/JobUtils.java | 104 ++++++++++++++++++ .../uml2sd/loader/Uml2SDTestFacility.java | 6 +- 5 files changed, 112 insertions(+), 61 deletions(-) create mode 100644 tmf/org.eclipse.tracecompass.tmf.ui.tests/shared/org/eclipse/tracecompass/tmf/ui/tests/shared/JobUtils.java diff --git a/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/META-INF/MANIFEST.MF b/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/META-INF/MANIFEST.MF index fda20e499f..3e2fa9ae07 100644 --- a/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/META-INF/MANIFEST.MF +++ b/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/META-INF/MANIFEST.MF @@ -19,6 +19,7 @@ Require-Bundle: org.junit;bundle-version="4.0.0", org.eclipse.tracecompass.tmf.remote.core, org.eclipse.tracecompass.tmf.remote.core.tests, org.eclipse.tracecompass.tmf.remote.ui, + org.eclipse.tracecompass.tmf.ui.tests, org.eclipse.core.resources Export-Package: org.eclipse.tracecompass.internal.lttng2.control.stubs.dialogs;x-friends:="org.eclipse.tracecompass.lttng2.control.ui.swtbot.tests", org.eclipse.tracecompass.internal.lttng2.control.stubs.service;x-friends:="org.eclipse.tracecompass.lttng2.control.ui.swtbot.tests", diff --git a/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/src/org/eclipse/tracecompass/lttng2/control/ui/tests/model/component/TraceControlTestFacility.java b/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/src/org/eclipse/tracecompass/lttng2/control/ui/tests/model/component/TraceControlTestFacility.java index 0eaef20b5e..7b6e5ebeaa 100644 --- a/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/src/org/eclipse/tracecompass/lttng2/control/ui/tests/model/component/TraceControlTestFacility.java +++ b/lttng/org.eclipse.tracecompass.lttng2.control.ui.tests/src/org/eclipse/tracecompass/lttng2/control/ui/tests/model/component/TraceControlTestFacility.java @@ -18,13 +18,13 @@ import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.commands.NotEnabledException; import org.eclipse.core.commands.NotHandledException; import org.eclipse.core.commands.common.NotDefinedException; -import org.eclipse.core.runtime.jobs.Job; import org.eclipse.swt.widgets.Display; import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState; import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView; import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent; import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent; import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; +import org.eclipse.tracecompass.tmf.ui.tests.shared.JobUtils; import org.eclipse.ui.IViewPart; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; @@ -182,9 +182,7 @@ public class TraceControlTestFacility { * Waits for all Eclipse jobs to finish */ public void waitForJobs() { - while (!Job.getJobManager().isIdle()) { - delay(WAIT_FOR_JOBS_DELAY); - } + JobUtils.waitForJobs(); } private IViewPart showView(String viewId) throws PartInitException { diff --git a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java index 7e7363de49..a1a8018f06 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java @@ -30,7 +30,6 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jface.bindings.keys.IKeyLookup; import org.eclipse.jface.bindings.keys.KeyStroke; @@ -76,6 +75,7 @@ import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder; import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.ProjectElementHasChild; +import org.eclipse.tracecompass.tmf.ui.tests.shared.JobUtils; import org.eclipse.tracecompass.tmf.ui.views.TracingPerspectiveFactory; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; @@ -109,61 +109,11 @@ public final class SWTBotUtils { * Waits for all Eclipse jobs to finish. Times out after * SWTBotUtils#MAX_JOBS_WAIT_TIME by default. * - * @throws TimeoutException + * @throws RuntimeException * once the waiting time passes the default maximum value */ public static void waitForJobs() { - waitForJobs(MAX_JOBS_WAIT_TIME); - } - - /** - * Waits for all Eclipse jobs to finish - * - * @param maxWait - * the maximum time to wait, in milliseconds. Once the waiting - * time passes the maximum value, a TimeoutException is thrown - * @throws TimeoutException - * once the waiting time passes the maximum value - */ - public static void waitForJobs(long maxWait) { - long waitStart = System.currentTimeMillis(); - while (!Job.getJobManager().isIdle()) { - if (System.currentTimeMillis() - waitStart > maxWait) { - printJobs(); - throw new TimeoutException("Timed out waiting for jobs to finish."); - } - - delay(100); - } - } - - private static void printJobs() { - Job[] jobs = Job.getJobManager().find(null); - for (Job job : jobs) { - System.err.println(job.toString() + " state: " + jobStateToString(job.getState())); - Thread thread = job.getThread(); - if (thread != null) { - for (StackTraceElement stractTraceElement : thread.getStackTrace()) { - System.err.println(" " + stractTraceElement); - } - } - System.err.println(); - } - } - - private static String jobStateToString(int jobState) { - switch (jobState) { - case Job.RUNNING: - return "RUNNING"; - case Job.WAITING: - return "WAITING"; - case Job.SLEEPING: - return "SLEEPING"; - case Job.NONE: - return "NONE"; - default: - return "UNKNOWN"; - } + JobUtils.waitForJobs(MAX_JOBS_WAIT_TIME); } /** diff --git a/tmf/org.eclipse.tracecompass.tmf.ui.tests/shared/org/eclipse/tracecompass/tmf/ui/tests/shared/JobUtils.java b/tmf/org.eclipse.tracecompass.tmf.ui.tests/shared/org/eclipse/tracecompass/tmf/ui/tests/shared/JobUtils.java new file mode 100644 index 0000000000..474119fa6d --- /dev/null +++ b/tmf/org.eclipse.tracecompass.tmf.ui.tests/shared/org/eclipse/tracecompass/tmf/ui/tests/shared/JobUtils.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2016 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.tracecompass.tmf.ui.tests.shared; + +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.swt.widgets.Display; + +/** + * A utility class for Job related things. + */ +public final class JobUtils { + + private JobUtils() { + } + + private static final long SLEEP_INTERVAL_MS = 100; + private static final long UI_THREAD_SLEEP_INTERVAL_MS = 10; + private static final long DEFAULT_MAX_JOBS_WAIT_TIME_MS = 300000; + + /** + * Waits for all Eclipse jobs to finish. Times out after + * RuntimeUtils#MAX_JOBS_WAIT_TIME by default. + * + * @throws RuntimeException + * once the waiting time passes the default maximum value + */ + public static void waitForJobs() { + waitForJobs(DEFAULT_MAX_JOBS_WAIT_TIME_MS); + } + + /** + * Waits for all Eclipse jobs to finish + * + * @param maxWait + * the maximum time to wait, in milliseconds. Once the waiting + * time passes the maximum value, a TimeoutException is thrown + * @throws RuntimeException + * once the waiting time passes the maximum value + */ + public static void waitForJobs(long maxWait) { + long waitStart = System.currentTimeMillis(); + Display display = Display.getCurrent(); + while (!Job.getJobManager().isIdle()) { + if (System.currentTimeMillis() - waitStart > maxWait) { + printJobs(); + throw new RuntimeException("Timed out waiting for jobs to finish."); //$NON-NLS-1$ + } + + if (display != null) { + if (!display.readAndDispatch()) { + // We do not use Display.sleep because it might never wake up + // if there is no user interaction + try { + Thread.sleep(UI_THREAD_SLEEP_INTERVAL_MS); + } catch (final InterruptedException e) { + // Ignored + } + } + display.update(); + } else { + try { + Thread.sleep(SLEEP_INTERVAL_MS); + } catch (final InterruptedException e) { + // Ignored + } + } + } + } + + private static void printJobs() { + Job[] jobs = Job.getJobManager().find(null); + for (Job job : jobs) { + System.err.println(job.toString() + " state: " + jobStateToString(job.getState())); //$NON-NLS-1$ + Thread thread = job.getThread(); + if (thread != null) { + for (StackTraceElement stractTraceElement : thread.getStackTrace()) { + System.err.println(" " + stractTraceElement); //$NON-NLS-1$ + } + } + System.err.println(); + } + } + + private static String jobStateToString(int jobState) { + switch (jobState) { + case Job.RUNNING: + return "RUNNING"; //$NON-NLS-1$ + case Job.WAITING: + return "WAITING"; //$NON-NLS-1$ + case Job.SLEEPING: + return "SLEEPING"; //$NON-NLS-1$ + case Job.NONE: + return "NONE"; //$NON-NLS-1$ + default: + return "UNKNOWN"; //$NON-NLS-1$ + } + } +} diff --git a/tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/views/uml2sd/loader/Uml2SDTestFacility.java b/tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/views/uml2sd/loader/Uml2SDTestFacility.java index 0e4f0caaa6..eb30978fd3 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/views/uml2sd/loader/Uml2SDTestFacility.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/views/uml2sd/loader/Uml2SDTestFacility.java @@ -22,7 +22,6 @@ import java.util.List; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.jobs.Job; import org.eclipse.swt.widgets.Display; import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; @@ -35,6 +34,7 @@ import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; +import org.eclipse.tracecompass.tmf.ui.tests.shared.JobUtils; import org.eclipse.tracecompass.tmf.ui.tests.uml2sd.trace.TmfUml2SDTestTrace; import org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDView; import org.eclipse.tracecompass.tmf.ui.views.uml2sd.dialogs.Criteria; @@ -238,9 +238,7 @@ public class Uml2SDTestFacility { * Waits for all Eclipse jobs to finish */ public void waitForJobs() { - while (!Job.getJobManager().isIdle()) { - delay(IUml2SDTestConstants.WAIT_FOR_JOBS_DELAY); - } + JobUtils.waitForJobs(); } /** -- 2.34.1