Skip to main content
summaryrefslogtreecommitdiffstats
blob: d995ba12024927b7c8f6be1826b0e42f73426b77 (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
/*******************************************************************************
 * Copyright (c) 2013 Tasktop Technologies 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
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.core.remote;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.AbstractRemoteEmfFactory;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.RemoteEmfObserver;

class TestRemoteObserver<P extends EObject, T, L, C> extends RemoteEmfObserver<P, T, L, C> {

	static final int TEST_TIMEOUT = 15000;

	int updated;

	int responded;

	IStatus failure;

	AbstractRemoteEmfFactory<P, T, L, ?, ?, C> factory;

	public TestRemoteObserver(AbstractRemoteEmfFactory<P, T, L, ?, ?, C> factory) {
		this.factory = factory;
	}

	@Override
	public void updating() {
	}

	@Override
	public void updated(boolean modified) {
		responded++;
		if (modified) {
			updated++;
		}
	}

	public void waitForResponse() {
		waitForResponse(1, 1);
	}

	public void waitForResponse(boolean updated) {
		waitForResponse(1, updated ? 1 : 0);
	}

	private void waitForResponse(int responses, int updates) {
		try {
			long delay;
			delay = 0;
			while (delay < TEST_TIMEOUT) {
				if (responded < responses || updated < updates) {
					try {
						Thread.sleep(10);
						delay += 10;
					} catch (InterruptedException e) {
					}
				} else {
					break;
				}
			}
			try {
				//wait extra to ensure there aren't remaining jobs
				Thread.sleep(25);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			assertThat("Wrong # responses: " + responded + ", updated: " + updated, responded, is(responses));
			assertThat("Wrong # updates" + updated, updated, is(updates));
			if (factory != null) {
				assertThat(factory.getService().isActive(), is(false));
			}
		} finally {
			responded = 0;
			updated = 0;
		}
	}

}

Back to the top