tmf: Add waitUntil / condition to tmf.ui.tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.tests / shared / org / eclipse / tracecompass / tmf / ui / tests / shared / WaitUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2016 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
10 package org.eclipse.tracecompass.tmf.ui.tests.shared;
11
12 import org.eclipse.core.runtime.jobs.Job;
13 import org.eclipse.swt.widgets.Display;
14
15 /**
16 * A utility class for methods related to waiting until certain conditions are met.
17 */
18 public final class WaitUtils {
19
20 private WaitUtils() {
21 }
22
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;
26
27 /**
28 * Waits for all Eclipse jobs to finish.
29 *
30 * @throws WaitTimeoutException
31 * once the waiting time passes the maximum value
32 */
33 public static void waitForJobs() {
34 waitUntil(new IWaitCondition() {
35 @Override
36 public boolean test() throws Exception {
37 return Job.getJobManager().isIdle();
38 }
39
40 @Override
41 public String getFailureMessage() {
42 printJobs();
43 return "Timed out waiting for jobs to finish.";
44 }
45 });
46 }
47
48 /**
49 * Prints the state of all the jobs on stderr.
50 */
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();
56 if (thread != null) {
57 for (StackTraceElement stractTraceElement : thread.getStackTrace()) {
58 System.err.println(" " + stractTraceElement); //$NON-NLS-1$
59 }
60 }
61 System.err.println();
62 }
63 }
64
65 private static String jobStateToString(int jobState) {
66 switch (jobState) {
67 case Job.RUNNING:
68 return "RUNNING"; //$NON-NLS-1$
69 case Job.WAITING:
70 return "WAITING"; //$NON-NLS-1$
71 case Job.SLEEPING:
72 return "SLEEPING"; //$NON-NLS-1$
73 case Job.NONE:
74 return "NONE"; //$NON-NLS-1$
75 default:
76 return "UNKNOWN"; //$NON-NLS-1$
77 }
78 }
79
80 /**
81 * Waits for a certain condition to be met.
82 *
83 * @param condition
84 * the condition to be met
85 *
86 * @throws WaitTimeoutException
87 * once the waiting time passes the maximum value
88 */
89 public static void waitUntil(IWaitCondition condition) {
90 waitUntil(condition, DEFAULT_MAX_WAIT_TIME_MS);
91 }
92
93 /**
94 * Waits for a certain condition to be met.
95 *
96 * @param condition
97 * the condition to be met
98 * @param maxWait
99 * the maximum time to wait, in milliseconds. Once the waiting
100 * time passes the maximum value, a WaitTimeoutException is
101 * thrown
102 * @throws WaitTimeoutException
103 * once the waiting time passes the maximum value
104 */
105 public static void waitUntil(IWaitCondition condition, long maxWait) {
106 long waitStart = System.currentTimeMillis();
107 Display display = Display.getCurrent();
108 try {
109 while (!condition.test()) {
110 if (System.currentTimeMillis() - waitStart > maxWait) {
111 throw new WaitTimeoutException(condition.getFailureMessage()); //$NON-NLS-1$
112 }
113
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
118 try {
119 Thread.sleep(UI_THREAD_SLEEP_INTERVAL_MS);
120 } catch (final InterruptedException e) {
121 // Ignored
122 }
123 }
124 display.update();
125 } else {
126 try {
127 Thread.sleep(SLEEP_INTERVAL_MS);
128 } catch (final InterruptedException e) {
129 // Ignored
130 }
131 }
132 }
133 } catch (Exception e) {
134 throw new WaitTimeoutException(condition.getFailureMessage()); //$NON-NLS-1$
135 }
136 }
137 }
This page took 0.039478 seconds and 5 git commands to generate.