1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
;
11 import org
.eclipse
.core
.runtime
.jobs
.Job
;
12 import org
.eclipse
.swt
.widgets
.Display
;
15 * A utility class for Job related things.
17 public final class JobUtils
{
22 private static final long SLEEP_INTERVAL_MS
= 100;
23 private static final long UI_THREAD_SLEEP_INTERVAL_MS
= 10;
24 private static final long DEFAULT_MAX_JOBS_WAIT_TIME_MS
= 300000;
27 * Waits for all Eclipse jobs to finish. Times out after
28 * RuntimeUtils#MAX_JOBS_WAIT_TIME by default.
30 * @throws RuntimeException
31 * once the waiting time passes the default maximum value
33 public static void waitForJobs() {
34 waitForJobs(DEFAULT_MAX_JOBS_WAIT_TIME_MS
);
38 * Waits for all Eclipse jobs to finish
41 * the maximum time to wait, in milliseconds. Once the waiting
42 * time passes the maximum value, a TimeoutException is thrown
43 * @throws RuntimeException
44 * once the waiting time passes the maximum value
46 public static void waitForJobs(long maxWait
) {
47 long waitStart
= System
.currentTimeMillis();
48 Display display
= Display
.getCurrent();
49 while (!Job
.getJobManager().isIdle()) {
50 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
52 throw new RuntimeException("Timed out waiting for jobs to finish."); //$NON-NLS-1$
55 if (display
!= null) {
56 if (!display
.readAndDispatch()) {
57 // We do not use Display.sleep because it might never wake up
58 // if there is no user interaction
60 Thread
.sleep(UI_THREAD_SLEEP_INTERVAL_MS
);
61 } catch (final InterruptedException e
) {
68 Thread
.sleep(SLEEP_INTERVAL_MS
);
69 } catch (final InterruptedException e
) {
76 private static void printJobs() {
77 Job
[] jobs
= Job
.getJobManager().find(null);
78 for (Job job
: jobs
) {
79 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState())); //$NON-NLS-1$
80 Thread thread
= job
.getThread();
82 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
83 System
.err
.println(" " + stractTraceElement
); //$NON-NLS-1$
90 private static String
jobStateToString(int jobState
) {
93 return "RUNNING"; //$NON-NLS-1$
95 return "WAITING"; //$NON-NLS-1$
97 return "SLEEPING"; //$NON-NLS-1$
99 return "NONE"; //$NON-NLS-1$
101 return "UNKNOWN"; //$NON-NLS-1$