Skip to content

Commit bbcdc23

Browse files
committed
Fix QuietPeriodListener swallowing subsequent events (issue #95)
1 parent 486b0b4 commit bbcdc23

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/main/java/ch/vorburger/fswatch/QuietPeriodListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class QuietPeriodListener implements Listener {
4040
protected @Nullable Thread thread;
4141
protected volatile boolean sleepAgain;
4242

43+
protected volatile Path lastPath;
44+
protected volatile ChangeKind lastChangeKind;
45+
4346
/**
4447
* Constructor.
4548
* @param quietPeriodInMS the quiet period in milliseconds
@@ -54,6 +57,8 @@ public QuietPeriodListener(long quietPeriodInMS, Listener listenerToWrap, Except
5457

5558
@Override
5659
public synchronized void onChange(Path path, ChangeKind changeKind) {
60+
this.lastPath = path;
61+
this.lastChangeKind = changeKind;
5762
if (thread != null && thread.isAlive()) {
5863
sleepAgain = true;
5964
//System.out.println("sleepAgain = true");
@@ -65,7 +70,7 @@ public synchronized void onChange(Path path, ChangeKind changeKind) {
6570
//System.out.println("sleepAgain = false");
6671
Thread.sleep(quietPeriodInMS);
6772
} while (sleepAgain);
68-
delegate.onChange(path, changeKind);
73+
delegate.onChange(lastPath, lastChangeKind);
6974
} catch (Throwable e) {
7075
exceptionHandler.onException(e);
7176
}
@@ -75,4 +80,4 @@ public synchronized void onChange(Path path, ChangeKind changeKind) {
7580
thread.start();
7681
}
7782
}
78-
}
83+
}

src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2424
import static java.util.concurrent.TimeUnit.SECONDS;
2525
import static org.awaitility.Awaitility.await;
26+
import static org.hamcrest.Matchers.anyOf;
2627
import static org.hamcrest.Matchers.is;
2728
import static org.junit.Assert.assertFalse;
2829
import static org.junit.Assert.assertTrue;
@@ -234,18 +235,18 @@ public void testOverwriteExistingFile() throws Throwable {
234235
final File file = File.createTempFile("test", "txt");
235236
final File to = dir.toPath().resolve("test.txt").toFile();
236237
Files.copy(file, to);
237-
java.util.List<ChangeKind> changes = java.util.Collections.synchronizedList(new java.util.ArrayList<>());
238+
AtomicReference<ChangeKind> change = new AtomicReference<>();
238239

239-
try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).quietPeriodInMS(0).listener((p, c) -> {
240+
try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> {
240241
System.out.println("c = " + c);
241-
changes.add(c);
242+
change.set(c);
242243
}).exceptionHandler(assertableExceptionHandler).build()) {
243244
// We want it to call the listener once for setup, even without any change
244245
assertableExceptionHandler.assertNoErrorInTheBackgroundThread();
245246
java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING);
246247

247248
// Files.move(file, to);
248-
await().atMost(5, SECONDS).until(() -> changes.contains(ChangeKind.CREATED) || changes.contains(ChangeKind.MODIFIED));
249+
await().atMost(5, SECONDS).until(change::get, anyOf(is(ChangeKind.CREATED), is(ChangeKind.MODIFIED)));
249250
}
250251
}
251252

0 commit comments

Comments
 (0)