Return-Path: X-Original-To: apmail-curator-dev-archive@minotaur.apache.org Delivered-To: apmail-curator-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 78B3E115C4 for ; Tue, 5 Aug 2014 14:03:33 +0000 (UTC) Received: (qmail 54241 invoked by uid 500); 5 Aug 2014 14:03:33 -0000 Delivered-To: apmail-curator-dev-archive@curator.apache.org Received: (qmail 54195 invoked by uid 500); 5 Aug 2014 14:03:33 -0000 Mailing-List: contact dev-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list dev@curator.apache.org Received: (qmail 54183 invoked by uid 99); 5 Aug 2014 14:03:32 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Aug 2014 14:03:32 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 9D79D9BF8A6; Tue, 5 Aug 2014 14:03:32 +0000 (UTC) From: Randgalt To: dev@curator.apache.org Reply-To: dev@curator.apache.org References: In-Reply-To: Subject: [GitHub] curator pull request: CURATOR-33 recursive TreeCache recipe Content-Type: text/plain Message-Id: <20140805140332.9D79D9BF8A6@tyr.zones.apache.org> Date: Tue, 5 Aug 2014 14:03:32 +0000 (UTC) Github user Randgalt commented on a diff in the pull request: https://github.com/apache/curator/pull/17#discussion_r15813477 --- Diff: curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java --- @@ -0,0 +1,335 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.curator.framework.recipes.cache; + +import com.google.common.collect.ImmutableSet; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.test.KillSession; +import org.apache.curator.utils.CloseableUtils; +import org.apache.zookeeper.CreateMode; +import org.testng.Assert; +import org.testng.annotations.Test; +import java.util.concurrent.Semaphore; + +public class TestTreeCache extends BaseTestTreeCache +{ + @Test + public void testStartup() throws Exception + { + 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").keySet(), ImmutableSet.of("1", "2", "3")); + Assert.assertEquals(cache.getCurrentChildren("/test/1").keySet(), ImmutableSet.of()); + Assert.assertEquals(cache.getCurrentChildren("/test/2").keySet(), ImmutableSet.of("sub")); + Assert.assertNull(cache.getCurrentChildren("/test/non_exist")); + } + + @Test + public void testStartEmpty() throws Exception + { + cache.start(); + assertEvent(TreeCacheEvent.Type.INITIALIZED); + + client.create().forPath("/test"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); + assertNoMoreEvents(); + } + + @Test + public void testAsyncInitialPopulation() throws Exception + { + 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(); + } + + @Test + public void testFromRoot() throws Exception + { + client.create().forPath("/test"); + client.create().forPath("/test/one", "hey there".getBytes()); + + cache = new TreeCache(client, "/", true); + cache.start(); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one"); + assertEvent(TreeCacheEvent.Type.INITIALIZED); + assertNoMoreEvents(); + + Assert.assertTrue(cache.getCurrentChildren("/").keySet().contains("test")); + Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one")); + Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of()); + Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there"); + } + + @Test + public void testWithNamespace() throws Exception + { + client.create().forPath("/outer"); + client.create().forPath("/outer/foo"); + client.create().forPath("/outer/test"); + client.create().forPath("/outer/test/one", "hey there".getBytes()); + + cache = new TreeCache(client.usingNamespace("outer"), "/test", true); + cache.start(); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one"); + assertEvent(TreeCacheEvent.Type.INITIALIZED); + assertNoMoreEvents(); + + Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one")); + Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of()); + Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there"); + } + + @Test + public void testWithNamespaceAtRoot() throws Exception + { + client.create().forPath("/outer"); + client.create().forPath("/outer/foo"); + client.create().forPath("/outer/test"); + client.create().forPath("/outer/test/one", "hey there".getBytes()); + + cache = new TreeCache(client.usingNamespace("outer"), "/", true); + cache.start(); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/foo"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test"); + assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one"); + assertEvent(TreeCacheEvent.Type.INITIALIZED); + assertNoMoreEvents(); + Assert.assertEquals(cache.getCurrentChildren("/").keySet(), ImmutableSet.of("foo", "test")); + Assert.assertEquals(cache.getCurrentChildren("/foo").keySet(), ImmutableSet.of()); + Assert.assertEquals(cache.getCurrentChildren("/test").keySet(), ImmutableSet.of("one")); + Assert.assertEquals(cache.getCurrentChildren("/test/one").keySet(), ImmutableSet.of()); + Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there"); + } + + @Test + public void testSyncInitialPopulation() throws Exception + { + 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(); + } + + @Test + public void testChildrenInitialized() throws Exception + { + 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(); + } + + @Test + public void testUpdateWhenNotCachingData() throws Exception + { + client.create().forPath("/test"); + + cache = new TreeCache(client, "/test", false); --- End diff -- Doesn't setup() already do this? Why is this second allocation needed? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---