ServerSetImpl implements ServerSet, DynamicHostSet<ServiceInstance> { @Override public Command watch(HostChangeMonitor<ServiceInstance> monitor) throws MonitorException { ServerSetWatcher serverSetWatcher = new ServerSetWatcher(zkClient, monitor); try { return serverSetWatcher.watch(); } catch (Group.WatchException e) { throw new MonitorException("ZooKeeper watch failed.", e); } catch (InterruptedException e) { throw new MonitorException("Interrupted while watching ZooKeeper.", e); } } ServerSetImpl(ZooKeeperClient zkClient, String path);  ServerSetImpl(ZooKeeperClient zkClient, Iterable<ACL> acl, String path);  ServerSetImpl(ZooKeeperClient zkClient, Group group);  ServerSetImpl(ZooKeeperClient zkClient, Group group, Codec<ServiceInstance> codec); @Override EndpointStatus join(
      InetSocketAddress endpoint,
      Map<String, InetSocketAddress> additionalEndpoints); @Override Command watch(HostChangeMonitor<ServiceInstance> monitor);  }
@Test public void testUnwatchOnException() throws Exception { IMocksControl control = createControl(); ZooKeeperClient zkClient = control.createMock(ZooKeeperClient.class); Watcher onExpirationWatcher = control.createMock(Watcher.class); expect(zkClient.registerExpirationHandler(anyObject(Command.class))) .andReturn(onExpirationWatcher); expect(zkClient.get()).andThrow(new InterruptedException()); expect(zkClient.unregister(onExpirationWatcher)).andReturn(true); control.replay(); Group group = new Group(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, "/blabla"); ServerSetImpl serverset = new ServerSetImpl(zkClient, group); try { serverset.watch(hostSet -> {}); fail("Expected MonitorException"); } catch (DynamicHostSet.MonitorException e) { assertTrue(Thread.interrupted()); } control.verify(); }