curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [2/2] git commit: TestPathCache completed
Date Wed, 12 Mar 2014 03:13:58 GMT
TestPathCache completed


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/656ecded
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/656ecded
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/656ecded

Branch: refs/heads/CURATOR-88
Commit: 656ecdedc86c1b72c94a482383c2a09758ff7b04
Parents: 78b5022
Author: randgalt <randgalt@apache.org>
Authored: Tue Mar 11 22:13:56 2014 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Tue Mar 11 22:13:56 2014 -0500

----------------------------------------------------------------------
 .../x/rest/api/PathChildrenCacheResource.java   |  2 +-
 .../curator/x/rest/api/TestPathCache.java       | 59 +++++++------
 .../x/rest/support/PathChildrenCacheBridge.java | 88 ++++++++++++++++++--
 3 files changed, 114 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/656ecded/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/PathChildrenCacheResource.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/PathChildrenCacheResource.java
b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/PathChildrenCacheResource.java
index a694930..771eb93 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/PathChildrenCacheResource.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/PathChildrenCacheResource.java
@@ -127,7 +127,7 @@ public class PathChildrenCacheResource
     public Response getCacheDataForPath(@PathParam("cache-id") String cacheId, @PathParam("path")
String path) throws Exception
     {
         PathChildrenCache cache = Constants.getThing(context.getSession(), cacheId, PathChildrenCache.class);
-        ChildData currentData = cache.getCurrentData(path);
+        ChildData currentData = cache.getCurrentData("/" + path);
         if ( currentData == null )
         {
             return Response.status(Response.Status.NOT_FOUND).build();

http://git-wip-us.apache.org/repos/asf/curator/blob/656ecded/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
index 619713c..e9b2dc2 100644
--- a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
@@ -19,17 +19,16 @@
 
 package org.apache.curator.x.rest.api;
 
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.framework.recipes.cache.PathChildrenCache;
-import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
-import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
-import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.utils.CloseableUtils;
+import org.apache.curator.x.rest.entities.CreateSpec;
+import org.apache.curator.x.rest.entities.DeleteSpec;
+import org.apache.curator.x.rest.entities.PathAndId;
+import org.apache.curator.x.rest.entities.SetDataSpec;
 import org.apache.curator.x.rest.support.BaseClassForTests;
 import org.apache.curator.x.rest.support.PathChildrenCacheBridge;
 import org.testng.Assert;
 import org.testng.annotations.Test;
+import javax.ws.rs.core.MediaType;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
@@ -39,43 +38,51 @@ public class TestPathCache extends BaseClassForTests
     @Test
     public void testBasics() throws Exception
     {
-/*
-        client.create().forPath("/test");
+        CreateSpec createSpec = new CreateSpec();
 
-        final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
+        createSpec.setPath("/test");
+        restClient.resource(uriMaker.getMethodUri("create")).type(MediaType.APPLICATION_JSON).post(PathAndId.class,
createSpec);
+
+        final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
         PathChildrenCacheBridge cache = new PathChildrenCacheBridge(restClient, sessionManager,
uriMaker, "/test", true, false);
         try
         {
             cache.getListenable().addListener
-                (
-                    new PathChildrenCacheListener()
+            (
+                new PathChildrenCacheBridge.Listener()
+                {
+                    @Override
+                    public void childEvent(String event, String path) throws Exception
                     {
-                        @Override
-                        public void childEvent(CuratorFramework client, PathChildrenCacheEvent
event) throws Exception
+                        if ( path.equals("/test/one") )
                         {
-                            if ( event.getData().getPath().equals("/test/one") )
-                            {
-                                events.offer(event.getType());
-                            }
+                            events.offer(event);
                         }
                     }
-                );
+                }
+            );
             cache.start();
 
-            client.create().forPath("/test/one", "hey there".getBytes());
-            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);
+            createSpec.setPath("/test/one");
+            createSpec.setData("hey there");
+            restClient.resource(uriMaker.getMethodUri("create")).type(MediaType.APPLICATION_JSON).post(PathAndId.class,
createSpec);
+            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), "child_added");
 
-            client.setData().forPath("/test/one", "sup!".getBytes());
-            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
-            Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()),
"sup!");
+            SetDataSpec setDataSpec = new SetDataSpec();
+            setDataSpec.setPath("/test/one");
+            setDataSpec.setData("sup!");
+            restClient.resource(uriMaker.getMethodUri("setData")).type(MediaType.APPLICATION_JSON).post(setDataSpec);
+            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), "child_updated");
+            Assert.assertEquals(cache.getCurrentData("/test/one").getNodeData().getData(),
"sup!");
 
-            client.delete().forPath("/test/one");
-            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
+            DeleteSpec deleteSpec = new DeleteSpec();
+            deleteSpec.setPath("/test/one");
+            restClient.resource(uriMaker.getMethodUri("delete")).type(MediaType.APPLICATION_JSON).post(deleteSpec);
+            Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), "child_removed");
         }
         finally
         {
             CloseableUtils.closeQuietly(cache);
         }
-*/
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/656ecded/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
index b593839..ff0c628 100644
--- a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
@@ -19,23 +19,39 @@
 
 package org.apache.curator.x.rest.support;
 
+import com.google.common.base.Function;
 import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.GenericType;
 import org.apache.curator.framework.listen.ListenerContainer;
-import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
-import org.apache.curator.x.rest.entities.NodeData;
+import org.apache.curator.framework.recipes.cache.ChildData;
+import org.apache.curator.framework.recipes.cache.PathChildrenCache;
+import org.apache.curator.x.rest.api.PathChildrenCacheResource;
+import org.apache.curator.x.rest.entities.OptionalNodeData;
+import org.apache.curator.x.rest.entities.PathAndId;
+import org.apache.curator.x.rest.entities.PathChildrenCacheSpec;
+import org.apache.curator.x.rest.entities.Status;
+import org.apache.curator.x.rest.entities.StatusMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.MediaType;
 import java.io.Closeable;
 import java.io.IOException;
+import java.net.URI;
 import java.util.List;
 
-public class PathChildrenCacheBridge implements Closeable
+public class PathChildrenCacheBridge implements Closeable, StatusListener
 {
+    private final Logger log = LoggerFactory.getLogger(getClass());
     private final Client restClient;
     private final SessionManager sessionManager;
     private final UriMaker uriMaker;
     private final String path;
     private final boolean cacheData;
     private final boolean dataIsCompressed;
-    private final ListenerContainer<PathChildrenCacheListener> listeners = new ListenerContainer<PathChildrenCacheListener>();
+    private final ListenerContainer<Listener> listeners = new ListenerContainer<Listener>();
+
+    private volatile String id;
 
     public PathChildrenCacheBridge(Client restClient, SessionManager sessionManager, UriMaker
uriMaker, String path, boolean cacheData, boolean dataIsCompressed)
     {
@@ -49,22 +65,78 @@ public class PathChildrenCacheBridge implements Closeable
 
     public void start() throws Exception
     {
-
+        PathChildrenCacheSpec spec = new PathChildrenCacheSpec();
+        spec.setPath(path);
+        spec.setCacheData(cacheData);
+        spec.setDataIsCompressed(dataIsCompressed);
+        spec.setStartMode(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
+        id = restClient.resource(uriMaker.getMethodUri(PathChildrenCacheResource.class, null)).type(MediaType.APPLICATION_JSON).post(PathAndId.class,
spec).getId();
+        sessionManager.addEntry(uriMaker.getLocalhost(), id, this);
     }
 
     @Override
     public void close() throws IOException
     {
+        sessionManager.removeEntry(uriMaker.getLocalhost(), id);
+        if ( id != null )
+        {
+            URI uri = uriMaker.getMethodUri(PathChildrenCacheResource.class, null);
+            restClient.resource(uri).path(id).delete();
+        }
+    }
 
+    public interface Listener
+    {
+        public void childEvent(String event, String path) throws Exception;
     }
 
-    public ListenerContainer<PathChildrenCacheListener> getListenable()
+    public ListenerContainer<Listener> getListenable()
     {
         return listeners;
     }
 
-    public List<NodeData> getCurrentData()
+    public List<ChildData> getCurrentData()
+    {
+        GenericType<List<ChildData>> type = new GenericType<List<ChildData>>(){};
+        return restClient.resource(uriMaker.getMethodUri(PathChildrenCacheResource.class,
"getCacheData")).path(id).type(MediaType.APPLICATION_JSON).get(type);
+    }
+
+    public OptionalNodeData getCurrentData(String path)
+    {
+        URI uri = uriMaker.getMethodUri(PathChildrenCacheResource.class, null);
+        return restClient.resource(uri).path(id).path(path).type(MediaType.APPLICATION_JSON).get(OptionalNodeData.class);
+    }
+
+    @Override
+    public void statusUpdate(List<StatusMessage> messages)
+    {
+        for ( final StatusMessage statusMessage : messages )
+        {
+            if ( statusMessage.getType().equals("path-cache") && statusMessage.getSourceId().equals(id)
)
+            {
+                listeners.forEach(new Function<Listener, Void>()
+                {
+                    @Nullable
+                    @Override
+                    public Void apply(Listener listener)
+                    {
+                        try
+                        {
+                            listener.childEvent(statusMessage.getMessage(), statusMessage.getDetails());
+                        }
+                        catch ( Exception e )
+                        {
+                            log.error("Calling listener", e);
+                        }
+                        return null;
+                    }
+                });
+            }
+        }
+    }
+
+    @Override
+    public void errorState(Status status)
     {
-        return null;
     }
 }


Mime
View raw message