### Question:
AbstractWatchService implements WatchService { public Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes) throws IOException { checkOpen(); return new Key(this, watchable, eventTypes); }  Key register(Watchable watchable, Iterable<? extends WatchEvent.Kind<?>> eventTypes); @VisibleForTesting boolean isOpen(); void cancelled(Key key); @NullableDecl @Override WatchKey poll(); @NullableDecl @Override WatchKey poll(long timeout, TimeUnit unit); @Override WatchKey take(); @Override void close();  }### Answer:
@Test public void testRegister() throws IOException { Watchable watchable = new StubWatchable(); AbstractWatchService.Key key = watcher.register(watchable, ImmutableSet.of(ENTRY_CREATE)); assertThat(key.isValid()).isTrue(); assertThat(key.pollEvents()).isEmpty(); assertThat(key.subscribesTo(ENTRY_CREATE)).isTrue(); assertThat(key.subscribesTo(ENTRY_DELETE)).isFalse(); assertThat(key.watchable()).isEqualTo(watchable); assertThat(key.state()).isEqualTo(READY); }
@Test public void testOverflow() throws IOException { AbstractWatchService.Key key = watcher.register(new StubWatchable(), ImmutableSet.of(ENTRY_CREATE)); for (int i = 0; i < AbstractWatchService.Key.MAX_QUEUE_SIZE + 10; i++) { key.post(new AbstractWatchService.Event<>(ENTRY_CREATE, 1, null)); } key.signal(); List<WatchEvent<?>> events = key.pollEvents(); assertThat(events).hasSize(AbstractWatchService.Key.MAX_QUEUE_SIZE + 1); for (int i = 0; i < AbstractWatchService.Key.MAX_QUEUE_SIZE; i++) { assertThat(events.get(i).kind()).isEqualTo(ENTRY_CREATE); } WatchEvent<?> lastEvent = events.get(AbstractWatchService.Key.MAX_QUEUE_SIZE); assertThat(lastEvent.kind()).isEqualTo(OVERFLOW); assertThat(lastEvent.count()).isEqualTo(10); }
@Test public void testResetAfterCancelReturnsFalse() throws IOException { AbstractWatchService.Key key = watcher.register(new StubWatchable(), ImmutableSet.of(ENTRY_CREATE)); key.signal(); key.cancel(); assertThat(key.reset()).isFalse(); }