Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6af33a641536ba447e899f787ef09c2529f6cf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
 * Copyright (C) 2012, 2016 Robin Stocker <robin@nibor.org> and others.
 *
 * 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.egit.ui.test;

import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;

/**
 * Utility for joining a job. Instead of just calling
 * {@link IJobManager#join(Object, org.eclipse.core.runtime.IProgressMonitor)},
 * it first waits until the job has been scheduled.
 * <p>
 * Usage:
 *
 * <pre>
 * JobJoiner jobJoiner = JobJoiner.startListening(jobFamily, 10, TimeUnit.SECONDS);
 * doThingThatSchedulesJob();
 * jobJoiner.join();
 * </pre>
 */
public class JobJoiner {

	private final Object jobFamily;
	private final long timeoutMillis;

	private Job scheduledJob = null;
	private boolean done = false;

	private final IJobChangeListener listener = new JobChangeAdapter() {
		@Override
		public void scheduled(IJobChangeEvent event) {
			if (event.getJob().belongsTo(jobFamily))
				scheduledJob = event.getJob();
		}

		@Override
		public void done(IJobChangeEvent event) {
			if (event.getJob() != null && event.getJob() == scheduledJob) {
				done = true;
				Job.getJobManager().removeJobChangeListener(this);
			}
		}
	};

	/**
	 * Start listening for the given job family and with the given timeout.
	 *
	 * @param jobFamily
	 * @param timeoutDuration
	 * @param timeoutUnit
	 * @return JobJoiner
	 */
	public static JobJoiner startListening(Object jobFamily, long timeoutDuration,
			TimeUnit timeoutUnit) {
		return new JobJoiner(jobFamily, timeoutUnit.toMillis(timeoutDuration));
	}

	private JobJoiner(Object jobFamily, long timeoutMillis) {
		this.jobFamily = jobFamily;
		this.timeoutMillis = timeoutMillis;
		Job.getJobManager().addJobChangeListener(listener);
	}

	/**
	 * Join the job. If the job is either not yet scheduled within the timeout
	 * or not yet done, an {@link AssertionError} is thrown.
	 *
	 * @return the joined job, if any, or {@code null}
	 */
	public Job join() {
		try {
			doJoin();
			return scheduledJob;
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new RuntimeException("Thread was interrupted.", e);
		} finally {
			Job.getJobManager().removeJobChangeListener(listener);
		}
	}

	private void doJoin() throws AssertionError, InterruptedException {
		long timeSlept = 0;
		while (!done) {
			if (timeSlept > timeoutMillis) {
				if (scheduledJob == null)
					throw new AssertionError(
							"Job was not scheduled until timeout of "
									+ timeoutMillis + " ms.");
				else if (!done)
					throw new AssertionError(
							"Job was scheduled but not done until timeout of "
									+ timeoutMillis + " ms.");
			}
			Thread.sleep(100);
			timeSlept += 100;
		}
	}
}

Back to the top