Commit | Line | Data |
---|---|---|
217d5c81 MAL |
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 | package org.eclipse.tracecompass.tmf.ui.tests.shared; | |
10 | ||
11 | import org.eclipse.core.runtime.jobs.Job; | |
12 | import org.eclipse.swt.widgets.Display; | |
13 | ||
14 | /** | |
15 | * A utility class for Job related things. | |
16 | */ | |
17 | public final class JobUtils { | |
18 | ||
19 | private JobUtils() { | |
20 | } | |
21 | ||
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; | |
25 | ||
26 | /** | |
27 | * Waits for all Eclipse jobs to finish. Times out after | |
28 | * RuntimeUtils#MAX_JOBS_WAIT_TIME by default. | |
29 | * | |
30 | * @throws RuntimeException | |
31 | * once the waiting time passes the default maximum value | |
32 | */ | |
33 | public static void waitForJobs() { | |
34 | waitForJobs(DEFAULT_MAX_JOBS_WAIT_TIME_MS); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Waits for all Eclipse jobs to finish | |
39 | * | |
40 | * @param maxWait | |
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 | |
45 | */ | |
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) { | |
51 | printJobs(); | |
52 | throw new RuntimeException("Timed out waiting for jobs to finish."); //$NON-NLS-1$ | |
53 | } | |
54 | ||
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 | |
59 | try { | |
60 | Thread.sleep(UI_THREAD_SLEEP_INTERVAL_MS); | |
61 | } catch (final InterruptedException e) { | |
62 | // Ignored | |
63 | } | |
64 | } | |
65 | display.update(); | |
66 | } else { | |
67 | try { | |
68 | Thread.sleep(SLEEP_INTERVAL_MS); | |
69 | } catch (final InterruptedException e) { | |
70 | // Ignored | |
71 | } | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
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(); | |
81 | if (thread != null) { | |
82 | for (StackTraceElement stractTraceElement : thread.getStackTrace()) { | |
83 | System.err.println(" " + stractTraceElement); //$NON-NLS-1$ | |
84 | } | |
85 | } | |
86 | System.err.println(); | |
87 | } | |
88 | } | |
89 | ||
90 | private static String jobStateToString(int jobState) { | |
91 | switch (jobState) { | |
92 | case Job.RUNNING: | |
93 | return "RUNNING"; //$NON-NLS-1$ | |
94 | case Job.WAITING: | |
95 | return "WAITING"; //$NON-NLS-1$ | |
96 | case Job.SLEEPING: | |
97 | return "SLEEPING"; //$NON-NLS-1$ | |
98 | case Job.NONE: | |
99 | return "NONE"; //$NON-NLS-1$ | |
100 | default: | |
101 | return "UNKNOWN"; //$NON-NLS-1$ | |
102 | } | |
103 | } | |
104 | } |