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(() -> {
65  			try {
66  				pm.start(1);
67  				fail("start did not fail on background thread");
68  			} catch (IllegalStateException notMainThread) {
69  				// Expected result
70  			}
71  
72  			try {
73  				pm.beginTask("title", 1);
74  				fail("beginTask did not fail on background thread");
75  			} catch (IllegalStateException notMainThread) {
76  				// Expected result
77  			}
78  
79  			try {
80  				pm.endTask();
81  				fail("endTask did not fail on background thread");
82  			} catch (IllegalStateException notMainThread) {
83  				// Expected result
84  			}
85  		});
86  
87  		// Ensure we didn't alter the mock above when checking threads.
88  		assertNull(mock.taskTitle);
89  		assertEquals(0, mock.value);
90  	}
91  
92  	@Test
93  	public void testMethodsOkOnMainThread() {
94  		final MockProgressMonitor mock = new MockProgressMonitor();
95  		final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
96  
97  		pm.start(1);
98  		assertEquals(1, mock.value);
99  
100 		pm.beginTask("title", 42);
101 		assertEquals("title", mock.taskTitle);
102 		assertEquals(42, mock.value);
103 
104 		pm.update(1);
105 		pm.pollForUpdates();
106 		assertEquals(43, mock.value);
107 
108 		pm.update(2);
109 		pm.pollForUpdates();
110 		assertEquals(45, mock.value);
111 
112 		pm.endTask();
113 		assertNull(mock.taskTitle);
114 		assertEquals(0, mock.value);
115 	}
116 
117 	@Test
118 	public void testUpdateOnBackgroundThreads() throws InterruptedException {
119 		final MockProgressMonitor mock = new MockProgressMonitor();
120 		final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
121 
122 		pm.startWorker();
123 
124 		final CountDownLatch doUpdate = new CountDownLatch(1);
125 		final CountDownLatch didUpdate = new CountDownLatch(1);
126 		final CountDownLatch doEndWorker = new CountDownLatch(1);
127 
128 		final Thread bg = new Thread() {
129 			@Override
130 			public void run() {
131 				assertFalse(pm.isCancelled());
132 
133 				await(doUpdate);
134 				pm.update(2);
135 				didUpdate.countDown();
136 
137 				await(doEndWorker);
138 				pm.update(1);
139 				pm.endWorker();
140 			}
141 		};
142 		bg.start();
143 
144 		pm.pollForUpdates();
145 		assertEquals(0, mock.value);
146 		doUpdate.countDown();
147 
148 		await(didUpdate);
149 		pm.pollForUpdates();
150 		assertEquals(2, mock.value);
151 
152 		doEndWorker.countDown();
153 		pm.waitForCompletion();
154 		assertEquals(3, mock.value);
155 	}
156 
157 	private static void await(CountDownLatch cdl) {
158 		try {
159 			assertTrue("latch released", cdl.await(1000, TimeUnit.MILLISECONDS));
160 		} catch (InterruptedException ie) {
161 			fail("Did not expect to be interrupted");
162 		}
163 	}
164 
165 	private static void runOnThread(Runnable task) throws InterruptedException {
166 		Thread t = new Thread(task);
167 		t.start();
168 		t.join(1000);
169 		assertFalse("thread has stopped", t.isAlive());
170 	}
171 
172 	private static class MockProgressMonitor implements ProgressMonitor {
173 		String taskTitle;
174 
175 		int value;
176 
177 		@Override
178 		public void update(int completed) {
179 			value += completed;
180 		}
181 
182 		@Override
183 		public void start(int totalTasks) {
184 			value = totalTasks;
185 		}
186 
187 		@Override
188 		public void beginTask(String title, int totalWork) {
189 			taskTitle = title;
190 			value = totalWork;
191 		}
192 
193 		@Override
194 		public void endTask() {
195 			taskTitle = null;
196 			value = 0;
197 		}
198 
199 		@Override
200 		public boolean isCancelled() {
201 			return false;
202 		}
203 	}
204 }