如何使用策展人从 testServer 获取事件?

How to get an event from testServer using curator?

我已经编写了关于我的服务的测试代码。 我的代码如下所示。

        Object obj = new Object();
        service.add(obj); // zookeeper node created in service.add

        Thread.sleep(7000); // wait until it is created

        Boolean created = OtherService.pick(a2, a2); // use node date in OtherService

        assertThat(created).isTrue();

在这种情况下,我不想使用 Thread.sleep。 有什么方法可以从 TestingServer 获取事件或回调来测试是否创建了节点?不使用 thread.sleep ??

我使用 latch 与 zookeeper 的节点创建同步。

        TreeCacheListener listener = (curator, event) -> {
            switch (event.getType()) {
                case NODE_ADDED:
                    createdLatch.countDown();
                }
            };
        cache.getListenable().addListener(listener);

        Object obj = new Object();
        service.add(obj); // node creation by zookeeper

        // Wait until Node is created
        timing.awaitLatch(createdLatch);

        Boolean created = OtherService.pick(a2, a2); // use node date in OtherService

        assertThat(created).isTrue();