|
22 | 22 | import static org.junit.Assert.fail; |
23 | 23 |
|
24 | 24 | import java.io.File; |
| 25 | +import java.util.ArrayList; |
25 | 26 | import java.util.Collection; |
26 | 27 | import java.util.Iterator; |
27 | 28 | import java.util.concurrent.Executors; |
| 29 | +import java.util.concurrent.ThreadFactory; |
28 | 30 |
|
29 | 31 | import org.apache.commons.io.testtools.TestUtils; |
30 | 32 | import org.junit.Test; |
@@ -176,4 +178,38 @@ private void checkFile(final String label, final File file, final Collection<Fil |
176 | 178 | } |
177 | 179 | fail(label + " " + file + " not found"); |
178 | 180 | } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test case for IO-535 |
| 184 | + * |
| 185 | + * Verify that {@link FileAlterationMonitor#stop()} stops the created thread |
| 186 | + */ |
| 187 | + @Test |
| 188 | + public void testStopWhileWaitingForNextInterval() throws Exception { |
| 189 | + final Collection<Thread> createdThreads = new ArrayList<>(1); |
| 190 | + final ThreadFactory threadFactory = new ThreadFactory() { |
| 191 | + private final ThreadFactory delegate = Executors.defaultThreadFactory(); |
| 192 | + |
| 193 | + @Override |
| 194 | + public Thread newThread(Runnable r) { |
| 195 | + final Thread thread = delegate.newThread(r); |
| 196 | + thread.setDaemon(true); //do not leak threads if the test fails |
| 197 | + createdThreads.add(thread); |
| 198 | + return thread; |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + final FileAlterationMonitor monitor = new FileAlterationMonitor(1_000); |
| 203 | + monitor.setThreadFactory(threadFactory); |
| 204 | + |
| 205 | + monitor.start(); |
| 206 | + assertFalse(createdThreads.isEmpty()); |
| 207 | + |
| 208 | + Thread.sleep(10); // wait until the watcher thread enters Thread.sleep() |
| 209 | + monitor.stop(100); |
| 210 | + |
| 211 | + for (Thread thread : createdThreads) { |
| 212 | + assertFalse("The FileAlterationMonitor did not stop the threads it created.", thread.isAlive()); |
| 213 | + } |
| 214 | + } |
179 | 215 | } |
0 commit comments