jira-importer commented on issue #554: URL: https://github.com/apache/curator/issues/554#issuecomment-2604695562
<i><a href="https://issues.apache.org/jira/secure/ViewProfile.jspa?name=githubbot">githubbot</a>:</i> <p>Github user dragonsinth commented on a diff in the pull request:</p> <p> <a href="https://github.com/apache/curator/pull/17#discussion_r15325624" class="external-link" target="_blank" rel="nofollow noopener">https://github.com/apache/curator/pull/17#discussion_r15325624</a></p> <p> — Diff: curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java —<br/> @@ -0,0 +1,421 @@<br/> +/**<br/> + * Licensed to the Apache Software Foundation (ASF) under one<br/> + * or more contributor license agreements. See the NOTICE file<br/> + * distributed with this work for additional information<br/> + * regarding copyright ownership. The ASF licenses this file<br/> + * to you under the Apache License, Version 2.0 (the<br/> + * "License"); you may not use this file except in compliance<br/> + * with the License. You may obtain a copy of the License at<br/> + *<br/> + * <a href="http://www.apache.org/licenses/LICENSE-2.0" class="external-link" target="_blank" rel="nofollow noopener">http://www.apache.org/licenses/LICENSE-2.0</a><br/> + *<br/> + * Unless required by applicable law or agreed to in writing,<br/> + * software distributed under the License is distributed on an<br/> + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY<br/> + * KIND, either express or implied. See the License for the<br/> + * specific language governing permissions and limitations<br/> + * under the License.<br/> + */<br/> +<br/> +package org.apache.curator.framework.recipes.cache;<br/> +<br/> +import com.google.common.collect.ImmutableSortedSet;<br/> +import org.apache.curator.framework.CuratorFramework;<br/> +import org.apache.curator.framework.CuratorFrameworkFactory;<br/> +import org.apache.curator.framework.api.UnhandledErrorListener;<br/> +import org.apache.curator.retry.RetryOneTime;<br/> +import org.apache.curator.test.BaseClassForTests;<br/> +import org.apache.curator.test.KillSession;<br/> +import org.apache.curator.test.Timing;<br/> +import org.apache.curator.utils.CloseableUtils;<br/> +import org.apache.zookeeper.CreateMode;<br/> +import org.testng.Assert;<br/> +import org.testng.annotations.AfterMethod;<br/> +import org.testng.annotations.BeforeMethod;<br/> +import org.testng.annotations.Test;<br/> +import java.util.ArrayList;<br/> +import java.util.Collections;<br/> +import java.util.List;<br/> +import java.util.concurrent.BlockingQueue;<br/> +import java.util.concurrent.LinkedBlockingQueue;<br/> +import java.util.concurrent.Semaphore;<br/> +import java.util.concurrent.TimeUnit;<br/> +<br/> +public class TestTreeCache extends BaseClassForTests<br/> +{<br/> + private final Timing timing = new Timing();<br/> + private CuratorFramework client;<br/> + private TreeCache cache;<br/> + private List<Throwable> exceptions;<br/> + private BlockingQueue<TreeCacheEvent> events;<br/> + private TreeCacheListener eventListener;<br/> +<br/> + /**<br/> + * A TreeCache that records exceptions.<br/> + */<br/> + private TreeCache newTreeCache(String path, boolean cacheData)<br/> + {<br/> +return new TreeCache(client, path, cacheData)<br/> +{<br/> + @Override<br/> + protected void handleException(Throwable e)<br/> + </p> { +exceptions.add(e); + } <p> +};<br/> + }<br/> +<br/> + @Override<br/> + @BeforeMethod<br/> + public void setup() throws Exception<br/> + {<br/> +super.setup();<br/> +<br/> +exceptions = new ArrayList<Throwable>();<br/> +events = new LinkedBlockingQueue<TreeCacheEvent>();<br/> +eventListener = new TreeCacheListener()<br/> +{<br/> + @Override<br/> + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception<br/> + </p> { +events.add(event); + } <p> +};<br/> +<br/> +client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));<br/> +client.start();<br/> +client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener()<br/> + {<br/> + @Override<br/> + public void unhandledError(String message, Throwable e)<br/> + </p> { + exceptions.add(e); + } <p> + }<br/> +);<br/> +cache = newTreeCache("/test", true);<br/> +cache.getListenable().addListener(eventListener);<br/> + }<br/> +<br/> + @Override<br/> + @AfterMethod<br/> + public void teardown() throws Exception<br/> + {<br/> +try<br/> +{<br/> + try<br/> + {<br/> +for ( Throwable exception : exceptions )<br/> +</p> { + Assert.fail("Exception was thrown", exception); +} <p> + }<br/> + finally<br/> + </p> { +CloseableUtils.closeQuietly(cache); +CloseableUtils.closeQuietly(client); + } <p> +}<br/> +finally<br/> +</p> { + super.teardown(); +} <p> + }<br/> +<br/> + @Test<br/> + public void testStartup() throws Exception<br/> + </p> { +client.create().forPath("/test"); +client.create().forPath("/test/1", "one".getBytes()); +client.create().forPath("/test/2", "two".getBytes()); +client.create().forPath("/test/3", "three".getBytes()); +client.create().forPath("/test/2/sub", "two-sub".getBytes()); + +cache.start(); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2", "two".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3", "three".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2/sub", "two-sub".getBytes()); +assertEvent(TreeCacheEvent.Type.INITIALIZED); +assertNoMoreEvents(); + +Assert.assertEquals(cache.getCurrentChildren("/test"), ImmutableSortedSet.of("1", "2", "3")); +Assert.assertEquals(cache.getCurrentChildren("/test/1"), Collections.emptySet()); +Assert.assertEquals(cache.getCurrentChildren("/test/2"), ImmutableSortedSet.of("sub")); +Assert.assertNull(cache.getCurrentChildren("/test/non_exist")); + } <p> +<br/> + @Test<br/> + public void testStartEmpty() throws Exception<br/> + </p> { +cache.start(); +assertEvent(TreeCacheEvent.Type.INITIALIZED); + +client.create().forPath("/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testAsyncInitialPopulation() throws Exception<br/> + </p> { +client.create().forPath("/test"); +client.create().forPath("/test/one", "hey there".getBytes()); +cache.start(); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testSyncInitialPopulation() throws Exception<br/> + </p> { +cache.start(); +assertEvent(TreeCacheEvent.Type.INITIALIZED); +client.create().forPath("/test"); +client.create().forPath("/test/one", "hey there".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one"); +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testChildrenInitialized() throws Exception<br/> + </p> { +client.create().forPath("/test", "".getBytes()); +client.create().forPath("/test/1", "1".getBytes()); +client.create().forPath("/test/2", "2".getBytes()); +client.create().forPath("/test/3", "3".getBytes()); + +cache.start(); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/2"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/3"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testUpdateWhenNotCachingData() throws Exception<br/> + </p> { +cache = newTreeCache("/test", false); +cache.getListenable().addListener(eventListener); + +client.create().forPath("/test"); +cache.start(); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); + +client.create().forPath("/test/foo", "first".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo"); + +client.setData().forPath("/test/foo", "something new".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_UPDATED, "/test/foo"); +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testDeleteThenCreate() throws Exception<br/> + </p> { +client.create().forPath("/test"); +client.create().forPath("/test/foo", "one".getBytes()); +cache.start(); + +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); + +client.delete().forPath("/test/foo"); +assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/foo"); +client.create().forPath("/test/foo", "two".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo"); + +assertNoMoreEvents(); + } <p> +<br/> + // see <a href="https://github.com/Netflix/curator/issues/27" class="external-link" target="_blank" rel="nofollow noopener">https://github.com/Netflix/curator/issues/27</a> - was caused by not comparing old->new data<br/> + @Test<br/> + public void testIssue27() throws Exception<br/> + </p> { +client.create().forPath("/test"); +client.create().forPath("/test/a"); +client.create().forPath("/test/b"); +client.create().forPath("/test/c"); + +client.getChildren().forPath("/test"); + +cache.start(); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/a"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/b"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/c"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); + +client.delete().forPath("/test/a"); +client.create().forPath("/test/a"); +assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/a"); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/a"); + +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testKilledSession() throws Exception<br/> + </p> { +client.create().forPath("/test"); +cache.start(); + +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); +assertEvent(TreeCacheEvent.Type.INITIALIZED); + +client.create().forPath("/test/foo", "foo".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo"); +client.create().withMode(CreateMode.EPHEMERAL).forPath("/test/me", "data".getBytes()); +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/me"); + +KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString()); +assertEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED); +assertEvent(TreeCacheEvent.Type.CONNECTION_LOST); +assertEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED); +assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/me"); + +assertNoMoreEvents(); + } <p> +<br/> + @Test<br/> + public void testBasics() throws Exception<br/> + {<br/> +client.create().forPath("/test");<br/> +cache.start();<br/> +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");<br/> +assertEvent(TreeCacheEvent.Type.INITIALIZED);<br/> +Assert.assertEquals(cache.getCurrentChildren("/test"), Collections.emptySortedSet());<br/> +<br/> +client.create().forPath("/test/one", "hey there".getBytes());<br/> +assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");<br/> +Assert.assertEquals(cache.getCurrentChildren("/test"), ImmutableSortedSet.of("one"));<br/> +Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");<br/> +<br/> +client.setData().forPath("/test/one", "sup!".getBytes());<br/> +assertEvent(TreeCacheEvent.Type.NODE_UPDATED, "/test/one");<br/> +Assert.assertEquals(cache.getCurrentChildren("/test"), ImmutableSortedSet.of("one"));<br/> +Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");<br/> +<br/> +client.delete().forPath("/test/one");<br/> +assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/one");<br/> +Assert.assertEquals(cache.getCurrentChildren("/test"), Collections.emptySortedSet());<br/> — End diff –</p> <p> Oops! No problem, can remove those.</p> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@curator.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org