You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/03/12 04:13:57 UTC
[1/2] git commit: testing continues
Repository: curator
Updated Branches:
refs/heads/CURATOR-88 05fb0b873 -> 656ecdedc
testing continues
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/78b5022a
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/78b5022a
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/78b5022a
Branch: refs/heads/CURATOR-88
Commit: 78b5022ac550f3a7cbd4264493cc238a3b2738ff
Parents: 05fb0b8
Author: randgalt <ra...@apache.org>
Authored: Mon Mar 10 17:04:08 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon Mar 10 17:04:08 2014 -0500
----------------------------------------------------------------------
.../x/rest/entities/OptionalNodeData.java | 49 ++++++++++++
.../curator/x/rest/api/TestPathCache.java | 81 ++++++++++++++++++++
.../x/rest/support/PathChildrenCacheBridge.java | 70 +++++++++++++++++
3 files changed, 200 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/78b5022a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/OptionalNodeData.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/OptionalNodeData.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/OptionalNodeData.java
new file mode 100644
index 0000000..398308c
--- /dev/null
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/OptionalNodeData.java
@@ -0,0 +1,49 @@
+/**
+ * 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.x.rest.entities;
+
+import org.apache.zookeeper.data.Stat;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class OptionalNodeData
+{
+ private NodeData nodeData;
+
+ public OptionalNodeData()
+ {
+ this(null);
+ }
+
+ public OptionalNodeData(NodeData nodeData)
+ {
+ this.nodeData = nodeData;
+ }
+
+ public NodeData getNodeData()
+ {
+ return nodeData;
+ }
+
+ public void setNodeData(NodeData nodeData)
+ {
+ this.nodeData = nodeData;
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/78b5022a/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
new file mode 100644
index 0000000..619713c
--- /dev/null
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestPathCache.java
@@ -0,0 +1,81 @@
+/**
+ * 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.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.support.BaseClassForTests;
+import org.apache.curator.x.rest.support.PathChildrenCacheBridge;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+public class TestPathCache extends BaseClassForTests
+{
+ @Test
+ public void testBasics() throws Exception
+ {
+/*
+ client.create().forPath("/test");
+
+ final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
+ PathChildrenCacheBridge cache = new PathChildrenCacheBridge(restClient, sessionManager, uriMaker, "/test", true, false);
+ try
+ {
+ cache.getListenable().addListener
+ (
+ new PathChildrenCacheListener()
+ {
+ @Override
+ public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception
+ {
+ if ( event.getData().getPath().equals("/test/one") )
+ {
+ events.offer(event.getType());
+ }
+ }
+ }
+ );
+ cache.start();
+
+ client.create().forPath("/test/one", "hey there".getBytes());
+ Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.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!");
+
+ client.delete().forPath("/test/one");
+ Assert.assertEquals(events.poll(10, TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(cache);
+ }
+*/
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/78b5022a/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
new file mode 100644
index 0000000..b593839
--- /dev/null
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/PathChildrenCacheBridge.java
@@ -0,0 +1,70 @@
+/**
+ * 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.x.rest.support;
+
+import com.sun.jersey.api.client.Client;
+import org.apache.curator.framework.listen.ListenerContainer;
+import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
+import org.apache.curator.x.rest.entities.NodeData;
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+
+public class PathChildrenCacheBridge implements Closeable
+{
+ 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>();
+
+ public PathChildrenCacheBridge(Client restClient, SessionManager sessionManager, UriMaker uriMaker, String path, boolean cacheData, boolean dataIsCompressed)
+ {
+ this.restClient = restClient;
+ this.sessionManager = sessionManager;
+ this.uriMaker = uriMaker;
+ this.path = path;
+ this.cacheData = cacheData;
+ this.dataIsCompressed = dataIsCompressed;
+ }
+
+ public void start() throws Exception
+ {
+
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+
+ }
+
+ public ListenerContainer<PathChildrenCacheListener> getListenable()
+ {
+ return listeners;
+ }
+
+ public List<NodeData> getCurrentData()
+ {
+ return null;
+ }
+}
[2/2] git commit: TestPathCache completed
Posted by ra...@apache.org.
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 <ra...@apache.org>
Authored: Tue Mar 11 22:13:56 2014 -0500
Committer: randgalt <ra...@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;
}
}