|
22 | 22 | import static org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode.BUILD_INITIAL_CACHE; |
23 | 23 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
24 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
25 | 26 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
26 | 27 | import static org.junit.jupiter.api.Assertions.assertNull; |
27 | 28 | import static org.junit.jupiter.api.Assertions.assertThrows; |
28 | 29 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 30 | +import java.util.concurrent.CountDownLatch; |
29 | 31 | import java.util.concurrent.Semaphore; |
30 | 32 | import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
31 | 34 | import org.apache.curator.framework.CuratorFramework; |
32 | 35 | import org.apache.curator.framework.CuratorFrameworkFactory; |
33 | 36 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; |
34 | 37 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; |
35 | 38 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; |
| 39 | +import org.apache.curator.framework.recipes.watch.PersistentWatcher; |
36 | 40 | import org.apache.curator.retry.RetryOneTime; |
37 | 41 | import org.apache.curator.test.Timing; |
38 | 42 | import org.apache.curator.test.compatibility.CuratorTestBase; |
39 | 43 | import org.apache.curator.test.compatibility.Timing2; |
40 | 44 | import org.apache.curator.utils.ZKPaths; |
| 45 | +import org.apache.zookeeper.Watcher; |
| 46 | +import org.apache.zookeeper.Watcher.Event.EventType; |
41 | 47 | import org.junit.jupiter.api.AfterEach; |
42 | 48 | import org.junit.jupiter.api.BeforeAll; |
43 | 49 | import org.junit.jupiter.api.BeforeEach; |
@@ -187,4 +193,48 @@ public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) th |
187 | 193 | assertNull(client.checkExists().forPath("/test")); |
188 | 194 | } |
189 | 195 | } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testTouchNodeNotCreated() throws Exception { |
| 199 | + final String mainPath = "/parent/main"; |
| 200 | + final String touchPath = ZKPaths.makePath(mainPath, PersistentTtlNode.DEFAULT_CHILD_NODE_NAME); |
| 201 | + final long testTtlMs = 500L; |
| 202 | + final CountDownLatch mainCreatedLatch = new CountDownLatch(1); |
| 203 | + final CountDownLatch mainDeletedLatch = new CountDownLatch(1); |
| 204 | + final AtomicBoolean touchCreated = new AtomicBoolean(); |
| 205 | + try (CuratorFramework client = |
| 206 | + CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1))) { |
| 207 | + client.start(); |
| 208 | + assertTrue(client.blockUntilConnected(1, TimeUnit.SECONDS)); |
| 209 | + try (PersistentWatcher watcher = new PersistentWatcher(client, mainPath, true)) { |
| 210 | + final Watcher listener = event -> { |
| 211 | + final String path = event.getPath(); |
| 212 | + if (mainPath.equals(path)) { |
| 213 | + final EventType type = event.getType(); |
| 214 | + if (EventType.NodeCreated.equals(type)) { |
| 215 | + mainCreatedLatch.countDown(); |
| 216 | + } else if (EventType.NodeDeleted.equals(type)) { |
| 217 | + mainDeletedLatch.countDown(); |
| 218 | + } |
| 219 | + } else if (touchPath.equals(path)) { |
| 220 | + touchCreated.set(true); |
| 221 | + } |
| 222 | + }; |
| 223 | + watcher.getListenable().addListener(listener); |
| 224 | + watcher.start(); |
| 225 | + try (PersistentTtlNode node = new PersistentTtlNode(client, mainPath, testTtlMs, new byte[0]) { |
| 226 | + @Override |
| 227 | + void touch() { |
| 228 | + // NOP |
| 229 | + } |
| 230 | + }) { |
| 231 | + node.start(); |
| 232 | + assertTrue(mainCreatedLatch.await(1L, TimeUnit.SECONDS)); |
| 233 | + } |
| 234 | + assertNull(client.checkExists().forPath(touchPath)); |
| 235 | + assertTrue(mainDeletedLatch.await(3L * testTtlMs, TimeUnit.MILLISECONDS)); |
| 236 | + assertFalse(touchCreated.get()); // Just to control that touch ZNode never created |
| 237 | + } |
| 238 | + } |
| 239 | + } |
190 | 240 | } |
0 commit comments