Правильная обработка метода CuratorFramework close - PullRequest
0 голосов
/ 17 ноября 2018

Я получаю трассировку стека в файлах журналов при тестировании моего класса ZooKeeperFetcher.Я совершенно уверен, что проблема заключается в том, что клиент CuratorFramework закрывается перед экземпляром pathChildrenCache.Возможно ли pathChildrenCache обнаружить и вызвать close () после закрытия curatorFramework?Я не могу просто добавить новый close () -> {pathChildrenCache.close ();} метод для ZooKeeperFetcher.

Упрощенный код:

public class ZooKeeperFetcher() {
  private final CuratorFramework client;
  private PathChildrenCache pathChildrenCache;

  ZooKeeperFetcher(CuratorFramework client) {
     this.client = client;
     pathChildrenCache = new PathChildrenCache(client, '/', true);

     PathChildrenCacheListener pathListener =
        (client, event) -> /* handle event */;
  } 

  /* this option is just not possible for me */
  // public void close() {
  //    pathChildrenCache.close();
  // }
}

And the test class:
public class TestZooKeeperFetcher {
  private TestingServer zookeeperServer;
  private final CuratorFramework client;
  private ZooKeeperFetcher fetcher; 

  @Before
  public void beforeEachTest() {
        zookeeperServer = new TestingServer(true);
        client =
                CuratorFrameworkFactory.newClient(
                        zookeeperServer.getConnectString(), new RetryNTimes(10, 50));
        client.start();
        fetcher = ZooKeeperFetcher(client);
  }

  @After
  public void afterEachTest() throws Exception {
      client.close();
  }

  @Test
  public void justDontThrowExceptionsTest() throws Exception {
  } 
}
...