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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
;
12 import org
.eclipse
.core
.runtime
.jobs
.Job
;
13 import org
.eclipse
.swt
.widgets
.Display
;
16 * A utility class for methods related to waiting until certain conditions are met.
18 public final class WaitUtils
{
23 private static final long SLEEP_INTERVAL_MS
= 100;
24 private static final long UI_THREAD_SLEEP_INTERVAL_MS
= 10;
25 private static final long DEFAULT_MAX_WAIT_TIME_MS
= 300000;
28 * Waits for all Eclipse jobs to finish.
30 * @throws WaitTimeoutException
31 * once the waiting time passes the maximum value
33 public static void waitForJobs() {
34 waitUntil(new IWaitCondition() {
36 public boolean test() throws Exception
{
37 return Job
.getJobManager().isIdle();
41 public String
getFailureMessage() {
43 return "Timed out waiting for jobs to finish.";
49 * Prints the state of all the jobs on stderr.
51 public static void printJobs() {
52 Job
[] jobs
= Job
.getJobManager().find(null);
53 for (Job job
: jobs
) {
54 System
.err
.println(job
.toString() + " state: " + jobStateToString(job
.getState())); //$NON-NLS-1$
55 Thread thread
= job
.getThread();
57 for (StackTraceElement stractTraceElement
: thread
.getStackTrace()) {
58 System
.err
.println(" " + stractTraceElement
); //$NON-NLS-1$
65 private static String
jobStateToString(int jobState
) {
68 return "RUNNING"; //$NON-NLS-1$
70 return "WAITING"; //$NON-NLS-1$
72 return "SLEEPING"; //$NON-NLS-1$
74 return "NONE"; //$NON-NLS-1$
76 return "UNKNOWN"; //$NON-NLS-1$
81 * Waits for a certain condition to be met.
84 * the condition to be met
86 * @throws WaitTimeoutException
87 * once the waiting time passes the maximum value
89 public static void waitUntil(IWaitCondition condition
) {
90 waitUntil(condition
, DEFAULT_MAX_WAIT_TIME_MS
);
94 * Waits for a certain condition to be met.
97 * the condition to be met
99 * the maximum time to wait, in milliseconds. Once the waiting
100 * time passes the maximum value, a WaitTimeoutException is
102 * @throws WaitTimeoutException
103 * once the waiting time passes the maximum value
105 public static void waitUntil(IWaitCondition condition
, long maxWait
) {
106 long waitStart
= System
.currentTimeMillis();
107 Display display
= Display
.getCurrent();
109 while (!condition
.test()) {
110 if (System
.currentTimeMillis() - waitStart
> maxWait
) {
111 throw new WaitTimeoutException(condition
.getFailureMessage()); //$NON-NLS-1$
114 if (display
!= null) {
115 if (!display
.readAndDispatch()) {
116 // We do not use Display.sleep because it might never wake up
117 // if there is no user interaction
119 Thread
.sleep(UI_THREAD_SLEEP_INTERVAL_MS
);
120 } catch (final InterruptedException e
) {
127 Thread
.sleep(SLEEP_INTERVAL_MS
);
128 } catch (final InterruptedException e
) {
133 } catch (Exception e
) {
134 throw new WaitTimeoutException(condition
.getFailureMessage()); //$NON-NLS-1$