View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.lib;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.util.concurrent.CountDownLatch;
53  import java.util.concurrent.TimeUnit;
54  
55  import org.junit.Test;
56  
57  public class ThreadSafeProgressMonitorTest {
58  	@Test
59  	public void testFailsMethodsOnBackgroundThread()
60  			throws InterruptedException {
61  		final MockProgressMonitor mock = new MockProgressMonitor();
62  		final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
63  
64  		runOnThread(new Runnable() {
65  			@Override
66  			public void run() {
67  				try {
68  					pm.start(1);
69  					fail("start did not fail on background thread");
70  				} catch (IllegalStateException notMainThread) {
71  					// Expected result
72  				}
73  
74  				try {
75  					pm.beginTask("title", 1);
76  					fail("beginTask did not fail on background thread");
77  				} catch (IllegalStateException notMainThread) {
78  					// Expected result
79  				}
80  
81  				try {
82  					pm.endTask();
83  					fail("endTask did not fail on background thread");
84  				} catch (IllegalStateException notMainThread) {
85  					// Expected result
86  				}
87  			}
88  		});
89  
90  		// Ensure we didn't alter the mock above when checking threads.
91  		assertNull(mock.taskTitle);
92  		assertEquals(0, mock.value);
93  	}
94  
95  	@Test
96  	public void testMethodsOkOnMainThread() {
97  		final MockProgressMonitor mock = new MockProgressMonitor();
98  		final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
99  
100 		pm.start(1);
101 		assertEquals(1, mock.value);
102 
103 		pm.beginTask("title", 42);
104 		assertEquals("title", mock.taskTitle);
105 		assertEquals(42, mock.value);
106 
107 		pm.update(1);
108 		pm.pollForUpdates();
109 		assertEquals(43, mock.value);
110 
111 		pm.update(2);
112 		pm.pollForUpdates();
113 		assertEquals(45, mock.value);
114 
115 		pm.endTask();
116 		assertNull(mock.taskTitle);
117 		assertEquals(0, mock.value);
118 	}
119 
120 	@Test
121 	public void testUpdateOnBackgroundThreads() throws InterruptedException {
122 		final MockProgressMonitor mock = new MockProgressMonitor();
123 		final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
124 
125 		pm.startWorker();
126 
127 		final CountDownLatch doUpdate = new CountDownLatch(1);
128 		final CountDownLatch didUpdate = new CountDownLatch(1);
129 		final CountDownLatch doEndWorker = new CountDownLatch(1);
130 
131 		final Thread bg = new Thread() {
132 			@Override
133 			public void run() {
134 				assertFalse(pm.isCancelled());
135 
136 				await(doUpdate);
137 				pm.update(2);
138 				didUpdate.countDown();
139 
140 				await(doEndWorker);
141 				pm.update(1);
142 				pm.endWorker();
143 			}
144 		};
145 		bg.start();
146 
147 		pm.pollForUpdates();
148 		assertEquals(0, mock.value);
149 		doUpdate.countDown();
150 
151 		await(didUpdate);
152 		pm.pollForUpdates();
153 		assertEquals(2, mock.value);
154 
155 		doEndWorker.countDown();
156 		pm.waitForCompletion();
157 		assertEquals(3, mock.value);
158 	}
159 
160 	private static void await(CountDownLatch cdl) {
161 		try {
162 			assertTrue("latch released", cdl.await(1000, TimeUnit.MILLISECONDS));
163 		} catch (InterruptedException ie) {
164 			fail("Did not expect to be interrupted");
165 		}
166 	}
167 
168 	private static void runOnThread(Runnable task) throws InterruptedException {
169 		Thread t = new Thread(task);
170 		t.start();
171 		t.join(1000);
172 		assertFalse("thread has stopped", t.isAlive());
173 	}
174 
175 	private static class MockProgressMonitor implements ProgressMonitor {
176 		String taskTitle;
177 
178 		int value;
179 
180 		@Override
181 		public void update(int completed) {
182 			value += completed;
183 		}
184 
185 		@Override
186 		public void start(int totalTasks) {
187 			value = totalTasks;
188 		}
189 
190 		@Override
191 		public void beginTask(String title, int totalWork) {
192 			taskTitle = title;
193 			value = totalWork;
194 		}
195 
196 		@Override
197 		public void endTask() {
198 			taskTitle = null;
199 			value = 0;
200 		}
201 
202 		@Override
203 		public boolean isCancelled() {
204 			return false;
205 		}
206 	}
207 }