[tests] unwanted event filtering for file watcher tests

This commit is contained in:
Roman Shevchenko
2017-05-25 10:40:44 +03:00
parent d235bc08ae
commit 9506b1d7ad
2 changed files with 26 additions and 23 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -183,7 +183,7 @@ public class FileWatcher {
@Override
public void notifyManualWatchRoots(@NotNull Collection<String> roots) {
myManualWatchRoots.add(roots.isEmpty() ? Collections.emptySet() : ContainerUtil.newHashSet(roots));
notifyOnAnyEvent();
notifyOnEvent(OTHER);
}
@Override
@@ -191,7 +191,7 @@ public class FileWatcher {
if (!mapping.isEmpty()) {
myPathMap.addMapping(mapping);
}
notifyOnAnyEvent();
notifyOnEvent(OTHER);
}
@Override
@@ -204,7 +204,7 @@ public class FileWatcher {
}
}
}
notifyOnAnyEvent();
notifyOnEvent(path);
}
@Override
@@ -221,7 +221,7 @@ public class FileWatcher {
}
}
}
notifyOnAnyEvent();
notifyOnEvent(path);
}
@Override
@@ -232,7 +232,7 @@ public class FileWatcher {
myDirtyPaths.dirtyDirectories.addAll(paths);
}
}
notifyOnAnyEvent();
notifyOnEvent(path);
}
@Override
@@ -245,7 +245,7 @@ public class FileWatcher {
}
}
}
notifyOnAnyEvent();
notifyOnEvent(path);
}
@Override
@@ -263,7 +263,7 @@ public class FileWatcher {
}
}
}
notifyOnReset();
notifyOnEvent(RESET);
}
@Override
@@ -272,22 +272,19 @@ public class FileWatcher {
}
}
/* test data and methods */
//<editor-fold desc="Test stuff.">
public static final String RESET = "(reset)";
public static final String OTHER = "(other)";
private volatile Consumer<Boolean> myTestNotifier = null;
private volatile Consumer<String> myTestNotifier = null;
private void notifyOnAnyEvent() {
Consumer<Boolean> notifier = myTestNotifier;
if (notifier != null) notifier.accept(Boolean.FALSE);
}
private void notifyOnReset() {
Consumer<Boolean> notifier = myTestNotifier;
if (notifier != null) notifier.accept(Boolean.TRUE);
private void notifyOnEvent(String path) {
Consumer<String> notifier = myTestNotifier;
if (notifier != null) notifier.accept(path);
}
@TestOnly
public void startup(@Nullable Consumer<Boolean> notifier) throws IOException {
public void startup(@Nullable Consumer<String> notifier) throws IOException {
myTestNotifier = notifier;
for (PluggableFileWatcher watcher : myWatchers) {
watcher.startup();
@@ -301,4 +298,5 @@ public class FileWatcher {
}
myTestNotifier = null;
}
//</editor-fold>
}
@@ -70,6 +70,7 @@ class FileWatcherTest : BareTestFixtureTestCase() {
private lateinit var watcher: FileWatcher
private lateinit var alarm: Alarm
private val watchedPaths = mutableListOf<String>()
private val watcherEvents = Semaphore()
private val resetHappened = AtomicBoolean()
@@ -85,10 +86,13 @@ class FileWatcherTest : BareTestFixtureTestCase() {
watcher = (fs as LocalFileSystemImpl).fileWatcher
assertFalse(watcher.isOperational)
watcher.startup { reset ->
alarm.cancelAllRequests()
alarm.addRequest({ watcherEvents.up() }, INTER_RESPONSE_DELAY)
if (reset) resetHappened.set(true)
watchedPaths += tempDir.root.path
watcher.startup { path ->
if (path == FileWatcher.RESET || path == FileWatcher.OTHER || watchedPaths.any { path.startsWith(it) }) {
alarm.cancelAllRequests()
alarm.addRequest({ watcherEvents.up() }, INTER_RESPONSE_DELAY)
if (path == FileWatcher.RESET) resetHappened.set(true)
}
}
wait { !watcher.isOperational }
@@ -322,6 +326,7 @@ class FileWatcherTest : BareTestFixtureTestCase() {
val substRoot = IoTestUtil.createSubst(target.path)
VfsRootAccess.allowRootAccess(testRootDisposable, substRoot.path)
val vfsRoot = fs.findFileByIoFile(substRoot)!!
watchedPaths += substRoot.path
val substFile = File(substRoot, "sub/test.txt")
refresh(target)