测试ZooKeeperFetcher类时,我在日志文件中收到堆栈跟踪。我很确定问题出在事实 CuratorFramework客户端在pathChildrenCache实例之前关闭。 pathChildrenCache是否有可能检测并 一旦curatorFramework关闭,再调用close()吗?我不能简单地添加新的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 {
}
}