use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenWatchmanFailsThenOverflowEventGenerated.
@Test
public void whenWatchmanFailsThenOverflowEventGenerated() throws IOException, InterruptedException {
Capture<WatchEvent<Path>> eventCapture = newCapture();
EventBus eventBus = createStrictMock(EventBus.class);
eventBus.post(capture(eventCapture));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, new FakeWatchmanClient(0, /* queryElapsedTimeNanos */
ImmutableMap.of(FAKE_UUID_QUERY, ImmutableMap.of()), new IOException("oops")), 10000);
try {
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
fail("Should have thrown IOException.");
} catch (IOException e) {
assertTrue("Should be expected error", e.getMessage().startsWith("oops"));
}
verify(eventBus);
assertEquals("Should be overflow event.", StandardWatchEventKinds.OVERFLOW, eventCapture.getValue().kind());
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenQueryResultContainsErrorThenOverflowEventGenerated.
@Test(expected = WatchmanWatcherException.class)
public void whenQueryResultContainsErrorThenOverflowEventGenerated() throws IOException, InterruptedException {
ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("version", "2.9.2", "error", "Watch does not exist.");
Capture<WatchEvent<Path>> eventCapture = newCapture();
EventBus eventBus = createStrictMock(EventBus.class);
eventBus.post(capture(eventCapture));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
try {
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
} finally {
assertEquals("Should be overflow event.", StandardWatchEventKinds.OVERFLOW, eventCapture.getValue().kind());
}
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenWatchmanInstanceIsFreshAndActionIsPostThenAllCachesAreCleared.
@Test
public void whenWatchmanInstanceIsFreshAndActionIsPostThenAllCachesAreCleared() throws IOException, InterruptedException {
ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("version", "2.9.2", "clock", "c:1386170113:26390:5:50273", "is_fresh_instance", true, "files", ImmutableList.of());
final Set<WatchEvent<?>> events = Sets.newHashSet();
EventBus bus = new EventBus("watchman test");
bus.register(new Object() {
@Subscribe
public void listen(WatchEvent<?> event) {
events.add(event);
}
});
WatchmanWatcher watcher = createWatcher(bus, watchmanOutput);
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.POST_OVERFLOW_EVENT);
boolean overflowSeen = false;
for (WatchEvent<?> event : events) {
overflowSeen |= event.kind().equals(StandardWatchEventKinds.OVERFLOW);
}
assertTrue(overflowSeen);
}
use of java.nio.file.WatchEvent in project buck by facebook.
the class WatchmanWatcherTest method whenWatchmanInterruptedThenOverflowEventGenerated.
@Test
public void whenWatchmanInterruptedThenOverflowEventGenerated() throws IOException, InterruptedException {
String message = "Boo!";
Capture<WatchEvent<Path>> eventCapture = newCapture();
EventBus eventBus = createStrictMock(EventBus.class);
eventBus.post(capture(eventCapture));
replay(eventBus);
WatchmanWatcher watcher = createWatcher(eventBus, new FakeWatchmanClient(0, /* queryElapsedTimeNanos */
ImmutableMap.of(FAKE_UUID_QUERY, ImmutableMap.of()), new InterruptedException(message)), 10000);
try {
watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
} catch (InterruptedException e) {
assertEquals("Should be test interruption.", e.getMessage(), message);
}
verify(eventBus);
assertTrue(Thread.currentThread().isInterrupted());
assertEquals("Should be overflow event.", StandardWatchEventKinds.OVERFLOW, eventCapture.getValue().kind());
}
use of java.nio.file.WatchEvent in project jetty.project by eclipse.
the class PathWatcher method run.
/**
* Forever loop.
*
* Wait for the WatchService to report some filesystem events for the
* watched paths.
*
* When an event for a path first occurs, it is subjected to a quiet time.
* Subsequent events that arrive for the same path during this quiet time are
* accumulated and the timer reset. Only when the quiet time has expired are
* the accumulated events sent. MODIFY events are handled slightly differently -
* multiple MODIFY events arriving within a quiet time are coalesced into a
* single MODIFY event. Both the accumulation of events and coalescing of MODIFY
* events reduce the number and frequency of event reporting for "noisy" files (ie
* those that are undergoing rapid change).
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
List<PathWatchEvent> notifiableEvents = new ArrayList<PathWatchEvent>();
// Start the java.nio watching
if (LOG.isDebugEnabled()) {
LOG.debug("Starting java.nio file watching with {}", watchService);
}
while (watchService != null && thread == Thread.currentThread()) {
WatchKey key = null;
try {
//If no pending events, wait forever for new events
if (pendingEvents.isEmpty()) {
if (NOISY_LOG.isDebugEnabled())
NOISY_LOG.debug("Waiting for take()");
key = watchService.take();
} else {
//only wait as long as the quiet time for any new events
if (NOISY_LOG.isDebugEnabled())
NOISY_LOG.debug("Waiting for poll({}, {})", updateQuietTimeDuration, updateQuietTimeUnit);
key = watchService.poll(updateQuietTimeDuration, updateQuietTimeUnit);
//If no new events its safe to process the pendings
if (key == null) {
long now = System.currentTimeMillis();
// no new event encountered.
for (Path path : new HashSet<Path>(pendingEvents.keySet())) {
PathPendingEvents pending = pendingEvents.get(path);
if (pending.isQuiet(now, updateQuietTimeDuration, updateQuietTimeUnit)) {
//so generate the events that were pent up
for (PathWatchEvent p : pending.getEvents()) {
notifiableEvents.add(p);
}
// remove from pending list
pendingEvents.remove(path);
}
}
}
}
} catch (ClosedWatchServiceException e) {
// Normal shutdown of watcher
return;
} catch (InterruptedException e) {
if (isRunning()) {
LOG.warn(e);
} else {
LOG.ignore(e);
}
return;
}
//If there was some new events to process
if (key != null) {
Config config = keys.get(key);
if (config == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("WatchKey not recognized: {}", key);
}
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked") WatchEvent.Kind<Path> kind = (Kind<Path>) event.kind();
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = config.dir.resolve(name);
if (kind == ENTRY_CREATE) {
// recursively
if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
try {
prepareConfig(config.asSubConfig(child));
} catch (IOException e) {
LOG.warn(e);
}
} else if (config.matches(child)) {
addToPendingList(child, new PathWatchEvent(child, ev));
}
} else if (config.matches(child)) {
addToPendingList(child, new PathWatchEvent(child, ev));
}
}
}
//Send any notifications generated this pass
notifyOnPathWatchEvents(notifiableEvents);
notifiableEvents.clear();
if (key != null && !key.reset()) {
keys.remove(key);
if (keys.isEmpty()) {
// all done, no longer monitoring anything
return;
}
}
}
}
Aggregations