Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4b42839081baf9beb9adcc786b6d72cd5f74f693 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * Copyright (c) 2007 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.tests.internal;

import org.eclipse.jpt.utility.internal.SynchronizedBoolean;

import junit.framework.TestCase;

public class SynchronizedBooleanTests extends TestCase {
	private volatile SynchronizedBoolean sb;
	private volatile boolean exCaught;
	private volatile boolean timeoutOccurred;
	private volatile long startTime;
	private volatile long endTime;


	public SynchronizedBooleanTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.sb = new SynchronizedBoolean();
		this.exCaught = false;
		this.timeoutOccurred = false;
		this.startTime = 0;
		this.endTime = 0;
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testAccessors() throws Exception {
		this.sb.setValue(false);
		assertFalse(this.sb.value());
		assertFalse(this.sb.isTrue());
		assertTrue(this.sb.isFalse());

		this.sb.setValue(true);
		assertTrue(this.sb.value());
		assertTrue(this.sb.isTrue());
		assertFalse(this.sb.isFalse());

		this.sb.setFalse();
		assertFalse(this.sb.value());
		assertFalse(this.sb.isTrue());
		assertTrue(this.sb.isFalse());

		this.sb.setTrue();
		assertTrue(this.sb.value());
		assertTrue(this.sb.isTrue());
		assertFalse(this.sb.isFalse());

		assertSame(this.sb, this.sb.mutex());
	}

	public void testEquals() throws Exception {
		this.sb.setValue(false);
		SynchronizedBoolean sb2 = new SynchronizedBoolean(false);
		assertEquals(this.sb, sb2);

		this.sb.setValue(true);
		assertFalse(this.sb.equals(sb2));

		sb2.setValue(true);
		assertEquals(this.sb, sb2);
	}

	public void testHashCode() {
		this.sb.setValue(false);
		assertEquals(0, this.sb.hashCode());

		this.sb.setValue(true);
		assertEquals(1, this.sb.hashCode());
	}

	public void testWaitUntilTrue() throws Exception {
		this.verifyWaitUntilTrue(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and the value should be set to true by t2
		assertTrue(this.sb.value());
		// make a reasonable guess about how long t2 took
		assertTrue(this.elapsedTime() > 150);
	}

	public void testWaitUntilTrueTimeout() throws Exception {
		this.verifyWaitUntilTrue(20);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and the value will eventually be set to true by t1
		assertTrue(this.sb.value());
		// make a reasonable guess about how long t2 took
		assertTrue(this.elapsedTime() < 150);
	}

	private void verifyWaitUntilTrue(long timeout) throws Exception {
		this.sb.setFalse();
		Runnable r1 = this.buildRunnable(this.buildSetTrueCommand(), this.sb, 200);
		Runnable r2 = this.buildRunnable(this.buildWaitUntilTrueCommand(timeout), this.sb, 0);
		Thread t1 = new Thread(r1);
		Thread t2 = new Thread(r2);
		t1.start();
		t2.start();
		while (t1.isAlive() || t2.isAlive()) {
			Thread.sleep(50);
		}
		assertFalse(this.exCaught);
	}

	public void testWaitToSetFalse() throws Exception {
		this.verifyWaitToSetFalse(0);
		// no timeout occurs...
		assertFalse(this.timeoutOccurred);
		// ...and the value should be set to false by t2
		assertFalse(this.sb.value());
		// make a reasonable guess about how long t2 took
		assertTrue(this.elapsedTime() > 150);
	}

	public void testWaitToSetFalseTimeout() throws Exception {
		this.verifyWaitToSetFalse(20);
		// timeout occurs...
		assertTrue(this.timeoutOccurred);
		// ...and the value will eventually be set to true by t1
		assertTrue(this.sb.value());
		// make a reasonable guess about how long t2 took
		assertTrue(this.elapsedTime() < 150);
	}

	private void verifyWaitToSetFalse(long timeout) throws Exception {
		this.sb.setFalse();
		Runnable r1 = this.buildRunnable(this.buildSetTrueCommand(), this.sb, 200);
		Runnable r2 = this.buildRunnable(this.buildWaitToSetFalseCommand(timeout), this.sb, 0);
		Thread t1 = new Thread(r1);
		Thread t2 = new Thread(r2);
		t1.start();
		t2.start();
		while (t1.isAlive() || t2.isAlive()) {
			Thread.sleep(50);
		}
		assertFalse(this.exCaught);
	}

	private Command buildSetTrueCommand() {
		return new Command() {
			public void execute(SynchronizedBoolean syncBool) {
				syncBool.setTrue();
			}
		};
	}

	private Command buildWaitUntilTrueCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedBoolean syncBool) throws Exception {
				SynchronizedBooleanTests.this.setStartTime(System.currentTimeMillis());
				SynchronizedBooleanTests.this.setTimeoutOccurred( ! syncBool.waitUntilTrue(timeout));
				SynchronizedBooleanTests.this.setEndTime(System.currentTimeMillis());
			}
		};
	}

	private Command buildWaitToSetFalseCommand(final long timeout) {
		return new Command() {
			public void execute(SynchronizedBoolean syncBool) throws Exception {
				SynchronizedBooleanTests.this.setStartTime(System.currentTimeMillis());
				SynchronizedBooleanTests.this.setTimeoutOccurred( ! syncBool.waitToSetFalse(timeout));
				SynchronizedBooleanTests.this.setEndTime(System.currentTimeMillis());
			}
		};
	}

	private Runnable buildRunnable(final Command command, final SynchronizedBoolean syncBool, final long sleep) {
		return new Runnable() {
			public void run() {
				try {
					if (sleep != 0) {
						Thread.sleep(sleep);
					}
					command.execute(syncBool);
				} catch (Exception ex) {
					SynchronizedBooleanTests.this.setExCaught(true);
				}
			}
		};
	}

	void setExCaught(boolean exCaught) {
		this.exCaught = exCaught;
	}

	void setTimeoutOccurred(boolean timeoutOccurred) {
		this.timeoutOccurred = timeoutOccurred;
	}

	void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	long elapsedTime() {
		return this.endTime - this.startTime;
	}


	// ********** Command interface **********

	private interface Command {
		void execute(SynchronizedBoolean syncBool) throws Exception;
	}

}

Back to the top