Search in sources :

Example 51 with WatchEvent

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());
}
Also used : FakeWatchmanClient(com.facebook.buck.io.FakeWatchmanClient) FakeClock(com.facebook.buck.timing.FakeClock) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with WatchEvent

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());
    }
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 53 with WatchEvent

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);
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Subscribe(com.google.common.eventbus.Subscribe) Test(org.junit.Test)

Example 54 with WatchEvent

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());
}
Also used : FakeWatchmanClient(com.facebook.buck.io.FakeWatchmanClient) FakeClock(com.facebook.buck.timing.FakeClock) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 55 with WatchEvent

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;
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) WatchKey(java.nio.file.WatchKey) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) IOException(java.io.IOException) Kind(java.nio.file.WatchEvent.Kind) WatchEvent(java.nio.file.WatchEvent) HashSet(java.util.HashSet)

Aggregations

WatchEvent (java.nio.file.WatchEvent)92 Path (java.nio.file.Path)68 WatchKey (java.nio.file.WatchKey)58 IOException (java.io.IOException)33 File (java.io.File)27 Test (org.junit.Test)24 WatchService (java.nio.file.WatchService)20 BuckEventBus (com.facebook.buck.event.BuckEventBus)13 FakeClock (com.facebook.buck.timing.FakeClock)13 EventBus (com.google.common.eventbus.EventBus)12 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)10 EasyMock.anyObject (org.easymock.EasyMock.anyObject)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)6 Subscribe (com.google.common.eventbus.Subscribe)5 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)4 FileSystems (java.nio.file.FileSystems)4 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)4 HashSet (java.util.HashSet)4 List (java.util.List)4