cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r768230 - in /cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src: main/java/org/apache/cxf/dosgi/discovery/zookeeper/ test/java/org/apache/cxf/dosgi/discovery/zookeeper/
Date Fri, 24 Apr 2009 09:06:22 GMT
Author: davidb
Date: Fri Apr 24 09:06:21 2009
New Revision: 768230

URL: http://svn.apache.org/viewvc?rev=768230&view=rev
Log:
[Discovery] Improvements to the zookeeper node listener code.

Added:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
  (with props)
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
  (contents, props changed)
      - copied, changed from r767877, cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
  (contents, props changed)
      - copied, changed from r767877, cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
Removed:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListener.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImpl.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImplTest.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java

Added: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java?rev=768230&view=auto
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
(added)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
Fri Apr 24 09:06:21 2009
@@ -0,0 +1,17 @@
+package org.apache.cxf.dosgi.discovery.zookeeper;
+
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.AsyncCallback.StatCallback;
+import org.apache.zookeeper.data.Stat;
+
+public class ChildMonitor implements StatCallback, Watcher {
+    public void processResult(int rc, String path, Object ctx, Stat stat) {
+        System.out.println("~~~ Got a callback! " + path + "@@@" + rc );
+    }
+
+    public void process(WatchedEvent event) {
+        System.out.println("??? Got a watcher callback! " + event );
+    }
+
+}

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListener.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListener.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListener.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListener.java
Fri Apr 24 09:06:21 2009
@@ -19,5 +19,5 @@
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
 interface DataMonitorListener {
-    void exists();
+    void change();
 }
\ No newline at end of file

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImpl.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImpl.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImpl.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImpl.java
Fri Apr 24 09:06:21 2009
@@ -47,12 +47,16 @@
         discoveredServiceTracker = dst;
     }
     
-    public void exists() {
+    public void change() {
         try {
             LOG.info("Zookeeper callback on node: " + znode);
             List<String> children = zookeeper.getChildren(znode, false);
             
             for (String child : children) {
+//                ChildMonitor cl = new ChildMonitor();
+//                zookeeper.exists(znode + '/' + child, cl, cl, null);
+                
+                // move to child watcher?
                 byte[] data = zookeeper.getData(znode + '/' + child, false, null);
                 Properties p = new Properties();
                 p.load(new ByteArrayInputStream(data));

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
Fri Apr 24 09:06:21 2009
@@ -35,8 +35,8 @@
 public class FindInZooKeeperCustomizer implements ServiceTrackerCustomizer {
     private final BundleContext bundleContext;
     private final ZooKeeper zookeeper;
-    final Map<DiscoveredServiceTracker, List<DataMonitor>> watchers = 
-        new ConcurrentHashMap<DiscoveredServiceTracker, List<DataMonitor>>();
+    final Map<DiscoveredServiceTracker, List<InterfaceMonitor>> watchers = 
+        new ConcurrentHashMap<DiscoveredServiceTracker, List<InterfaceMonitor>>();
     
     public FindInZooKeeperCustomizer(BundleContext bc, ZooKeeper zk) {
         bundleContext = bc;
@@ -58,9 +58,9 @@
         Collection<String> interfaces = Util.getMultiValueProperty(
             sr.getProperty(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES));
 
-        List<DataMonitor> dmList = new ArrayList<DataMonitor>(interfaces.size());
+        List<InterfaceMonitor> dmList = new ArrayList<InterfaceMonitor>(interfaces.size());
         for (String intf : interfaces) {
-            DataMonitor dm = new DataMonitor(zookeeper, intf, dst);
+            InterfaceMonitor dm = new InterfaceMonitor(zookeeper, intf, dst);
             dmList.add(dm);
             dm.process();
         }
@@ -74,15 +74,15 @@
     }
 
     public void removedService(ServiceReference sr, Object svcObj) {
-        List<DataMonitor> oldVal = watchers.remove(svcObj);
+        List<InterfaceMonitor> oldVal = watchers.remove(svcObj);
         if (oldVal != null) {
             // TODO unregister any listeners directly registered with ZooKeeper
         }
     }
 
     public void processGlobalEvent(WatchedEvent event) {
-        for (List<DataMonitor> dmList : watchers.values()) {
-            for (DataMonitor dm : dmList) {
+        for (List<InterfaceMonitor> dmList : watchers.values()) {
+            for (InterfaceMonitor dm : dmList) {
                 dm.process();
             }
         }

Copied: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
(from r767877, cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java)
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java?p2=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java&p1=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java&r1=767877&r2=768230&rev=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
Fri Apr 24 09:06:21 2009
@@ -18,68 +18,68 @@
   */
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
-import java.util.Arrays;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.AsyncCallback.StatCallback;
 import org.apache.zookeeper.KeeperException.Code;
 import org.apache.zookeeper.data.Stat;
 import org.osgi.service.discovery.DiscoveredServiceTracker;
 
-public class DataMonitor implements StatCallback {
-    private static final Logger LOG = Logger.getLogger(DataMonitor.class.getName());
+public class InterfaceMonitor implements Watcher, StatCallback {
+    private static final Logger LOG = Logger.getLogger(InterfaceMonitor.class.getName());
 
     DataMonitorListener listener;
     final String znode;
     final ZooKeeper zookeeper;
-    private byte [] prevData;
 
-    public DataMonitor(ZooKeeper zk, String intf, DiscoveredServiceTracker dst) {
+    public InterfaceMonitor(ZooKeeper zk, String intf, DiscoveredServiceTracker dst) {
         listener = new DataMonitorListenerImpl(zk, intf, dst);
         zookeeper = zk;
         znode = Util.getZooKeeperPath(intf);
     }
     
     public void process() {
-        /* */ System.out.println("*** Kicking off a zookeeper.exists()");
-        zookeeper.exists(znode, true, this, null);
+        LOG.finest("Kicking off a zookeeper.exists() on node: " + znode);
+        zookeeper.exists(znode, this, this, null);
     }
 
+    public void process(WatchedEvent event) {
+        LOG.finer("ZooKeeper watcher callback " + event);
+        processDelta();
+    }
+    
     public void processResult(int rc, String path, Object ctx, Stat stat) {
-        boolean exists;
+        LOG.finer("ZooKeeper callback on node: " + znode + " code: " + rc);
         
         switch (rc) {
         case Code.Ok:
-            exists = true;
             break;
         case Code.NoNode:
-            exists = false;
             break;
         case Code.SessionExpired:
-            LOG.info("ZooKeeper reports: SessionExpired on node: " + znode);
             return;
         case Code.NoAuth:
-            LOG.info("ZooKeeper reports: NoAuth on node: " + znode);
             return;
         default:
             process();
             return;
         }
         
-        byte [] b = null;
-        if (exists) {
-            try {
-                b = zookeeper.getData(znode, false, null);                
-            } catch (Exception ke) {
-                LOG.log(Level.SEVERE, "Error getting ZooKeeper data.", ke);
-            }
-            
-            if (!Arrays.equals(prevData, b)) {
-                listener.exists();
-                prevData = b;
+        processDelta();
+    }
+
+    private void processDelta() {
+        try {
+            if (zookeeper.exists(znode, false) != null) {
+                listener.change();
+                zookeeper.getChildren(znode, this);
             }
+        } catch (Exception ke) {
+            LOG.log(Level.SEVERE, "Error getting ZooKeeper data.", ke);
         }
     }
 }

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitor.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImplTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImplTest.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImplTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorListenerImplTest.java
Fri Apr 24 09:06:21 2009
@@ -34,7 +34,7 @@
 import org.osgi.service.discovery.ServiceEndpointDescription;
 
 public class DataMonitorListenerImplTest extends TestCase {
-    public void testExists() throws Exception {
+    public void testChange() throws Exception {
         final List<DiscoveredServiceNotification> dsnCallbacks = new ArrayList<DiscoveredServiceNotification>();
         DiscoveredServiceTracker dst = new DiscoveredServiceTracker() {
             public void serviceChanged(DiscoveredServiceNotification dsn) {
@@ -57,7 +57,7 @@
         DataMonitorListenerImpl dml = new DataMonitorListenerImpl(zk, String.class.getName(),
dst);
         
         assertEquals("Precondition failed", 0, dsnCallbacks.size());
-        dml.exists();
+        dml.change();
         assertEquals(1, dsnCallbacks.size());
         DiscoveredServiceNotification dsn = dsnCallbacks.iterator().next();
         assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces());

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
Fri Apr 24 09:06:21 2009
@@ -106,20 +106,20 @@
         DiscoveryBean db = new DiscoveryBean();
         
         FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(null, null);
-        List<DataMonitor> l1 = new ArrayList<DataMonitor>();
-        DataMonitor dm1a = EasyMock.createMock(DataMonitor.class);
+        List<InterfaceMonitor> l1 = new ArrayList<InterfaceMonitor>();
+        InterfaceMonitor dm1a = EasyMock.createMock(InterfaceMonitor.class);
         dm1a.process();
         EasyMock.expectLastCall();
         EasyMock.replay(dm1a);
-        DataMonitor dm1b = EasyMock.createMock(DataMonitor.class);
+        InterfaceMonitor dm1b = EasyMock.createMock(InterfaceMonitor.class);
         dm1b.process();
         EasyMock.expectLastCall();
         EasyMock.replay(dm1b);
         l1.add(dm1a);
         l1.add(dm1b);
         
-        List<DataMonitor> l2 = new ArrayList<DataMonitor>();
-        DataMonitor dm2 = EasyMock.createMock(DataMonitor.class);
+        List<InterfaceMonitor> l2 = new ArrayList<InterfaceMonitor>();
+        InterfaceMonitor dm2 = EasyMock.createMock(InterfaceMonitor.class);
         dm2.process();
         EasyMock.expectLastCall();
         EasyMock.replay(dm2);

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java?rev=768230&r1=768229&r2=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
Fri Apr 24 09:06:21 2009
@@ -24,8 +24,10 @@
 
 import junit.framework.TestCase;
 
+import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.AsyncCallback.StatCallback;
+import org.easymock.IAnswer;
 import org.easymock.classextension.EasyMock;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -82,9 +84,9 @@
         
         DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next();
         assertSame(dst, key);
-        List<DataMonitor> dmList = fc.watchers.get(key);
+        List<InterfaceMonitor> dmList = fc.watchers.get(key);
         assertEquals(1, dmList.size());
-        DataMonitor dm = dmList.iterator().next();
+        InterfaceMonitor dm = dmList.iterator().next();
         assertNotNull(dm.listener);
         assertSame(zk, dm.zookeeper);
         assertEquals(Util.getZooKeeperPath(String.class.getName()), dm.znode);        
@@ -96,7 +98,7 @@
         assertTrue(fc.watchers.containsKey(dst));
         assertTrue(fc.watchers.containsKey(dst2));
         assertEquals(dmList, fc.watchers.get(dst));
-        List<DataMonitor> dmList2 = fc.watchers.get(dst2);
+        List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2);
         assertEquals(2, dmList2.size());
         assertEquals(Util.getZooKeeperPath(Integer.class.getName()), dmList2.get(0).znode);
         assertEquals(Util.getZooKeeperPath(Comparable.class.getName()), dmList2.get(1).znode);
@@ -122,7 +124,7 @@
         assertTrue(fc.watchers.containsKey(dst));
         assertTrue(fc.watchers.containsKey(dst2));
         assertEquals(dmList2, fc.watchers.get(dst2));
-        List<DataMonitor> dmList3 = fc.watchers.get(dst);
+        List<InterfaceMonitor> dmList3 = fc.watchers.get(dst);
         assertEquals(1, dmList3.size());
         assertEquals(Util.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode);
 
@@ -145,7 +147,15 @@
     }
 
     private void zkExpectExists(ZooKeeper zk, String className) {
-        zk.exists(EasyMock.eq(Util.getZooKeeperPath(className)), EasyMock.eq(true), 
+        zk.exists(EasyMock.eq(Util.getZooKeeperPath(className)), 
+                (Watcher) EasyMock.anyObject(), 
                 (StatCallback) EasyMock.anyObject(), EasyMock.isNull());
+        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+            public Object answer() throws Throwable {
+                assertEquals(EasyMock.getCurrentArguments()[1],
+                        EasyMock.getCurrentArguments()[2]);
+                return null;
+            }            
+        });
     }
 }

Copied: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
(from r767877, cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java)
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java?p2=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java&p1=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java&r1=767877&r2=768230&rev=768230&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
Fri Apr 24 09:06:21 2009
@@ -28,20 +28,25 @@
 
 import junit.framework.TestCase;
 
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.AsyncCallback.StatCallback;
 import org.apache.zookeeper.KeeperException.Code;
+import org.apache.zookeeper.data.Stat;
+import org.easymock.IAnswer;
 import org.easymock.classextension.EasyMock;
 import org.osgi.service.discovery.DiscoveredServiceNotification;
 import org.osgi.service.discovery.DiscoveredServiceTracker;
 import org.osgi.service.discovery.ServiceEndpointDescription;
 
-public class DataMonitorTest extends TestCase {
+public class InterfaceMonitorTest extends TestCase {
     public void testCreateListener() {
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
         DiscoveredServiceTracker dst = EasyMock.createMock(DiscoveredServiceTracker.class);
       
         
-        DataMonitor dm = new DataMonitor(zk, String.class.getName(), dst);
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), dst);
         DataMonitorListenerImpl listener = (DataMonitorListenerImpl) dm.listener;
         assertSame(zk, listener.zookeeper);
         assertEquals(Util.getZooKeeperPath(String.class.getName()), listener.znode);
@@ -49,7 +54,7 @@
         assertSame(dst, listener.discoveredServiceTracker);
     }
     
-    public void testDataMonitor() throws Exception {
+    public void testInterfaceMonitor() throws Exception {
         Properties s1Props = new Properties();
         s1Props.put("a", "b");
         ByteArrayOutputStream s1Bytes = new ByteArrayOutputStream();
@@ -61,8 +66,8 @@
         s2Props.store(s2Bytes, "");
         
         ZooKeeper zk = EasyMock.createNiceMock(ZooKeeper.class);
-        EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()), false,
null))
-            .andReturn(new byte[0]).anyTimes();
+        zk.exists(Util.getZooKeeperPath(String.class.getName()), false);
+        EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
         EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), false))
             .andReturn(Arrays.asList("a#90#r", "b#90#r")).anyTimes();
         EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/a#90#r",
false, null))
@@ -78,7 +83,7 @@
             }            
         };
         
-        DataMonitor dm = new DataMonitor(zk, String.class.getName(), dst);
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), dst);
         assertEquals("Precondition failed", 0, notifications.size());
         dm.processResult(Code.Ok, null, null, null);
         assertEquals(2, notifications.size());
@@ -108,8 +113,8 @@
         
         // Third time around, with different data
         EasyMock.reset(zk);
-        EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()), false,
null))
-            .andReturn(new byte[] {123}).anyTimes();
+        zk.exists(Util.getZooKeeperPath(String.class.getName()), false);
+        EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
         EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), false))
             .andReturn(Arrays.asList("a#90#r")).anyTimes();
         EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/a#90#r",
false, null))
@@ -124,39 +129,16 @@
         assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces());
         ServiceEndpointDescription sed = dsn.getServiceEndpointDescription();
         assertEquals(Collections.singleton(String.class.getName()), sed.getProvidedInterfaces());
-        assertEquals(s1Props, sed.getProperties());
-        
-        // Fourth time around, now with null
-        EasyMock.reset(zk);
-        EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()), false,
null))
-            .andReturn(null).anyTimes();
-        EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), false))
-            .andReturn(Arrays.asList("b#90#r")).anyTimes();
-        EasyMock.expect(zk.getData(Util.getZooKeeperPath(String.class.getName()) + "/b#90#r",
false, null))
-            .andReturn(s2Bytes.toByteArray()).anyTimes();
-        EasyMock.replay(zk);
-        notifications.clear();
-        dm.processResult(Code.Ok, null, null, null);
-        DiscoveredServiceNotification dsn2 = notifications.iterator().next();
-        assertEquals(1, notifications.size());
-        assertEquals(DiscoveredServiceNotification.AVAILABLE, dsn2.getType());
-        assertEquals(Collections.emptyList(), dsn2.getFilters());
-        assertEquals(Collections.singleton(String.class.getName()), dsn2.getInterfaces());
-        ServiceEndpointDescription sed2 = dsn2.getServiceEndpointDescription();
-        assertEquals(Collections.singleton(String.class.getName()), sed2.getProvidedInterfaces());
-        assertEquals(s2Props, sed2.getProperties());
-
-        // Fifth time around, with null again
-        notifications.clear();
-        dm.processResult(Code.Ok, null, null, null);
-        assertEquals("No changes, so should not get any new notifications", 0, notifications.size());
+        assertEquals(s1Props, sed.getProperties());        
     }
     
-    public void testDataMonitorNoExist() throws Exception {
+    public void testInterfaceMonitorNoExist() throws Exception {
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
+        EasyMock.expect(zk.exists(Util.getZooKeeperPath(String.class.getName()), false))
+            .andReturn(null);
         EasyMock.replay(zk);
 
-        DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);        
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);   
    
         dm.processResult(Code.NoNode, null, null, null);
 
         EasyMock.verify(zk);
@@ -166,27 +148,75 @@
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
         zk.exists(
             EasyMock.eq(Util.getZooKeeperPath(String.class.getName())),
-            EasyMock.eq(true),
+            (Watcher) EasyMock.anyObject(),
             (StatCallback) EasyMock.anyObject(), 
             EasyMock.isNull());
-        EasyMock.expectLastCall();
+        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
+            public Object answer() throws Throwable {
+                assertEquals(EasyMock.getCurrentArguments()[1],
+                    EasyMock.getCurrentArguments()[2]);
+                return null;
+            }            
+        });
         EasyMock.replay(zk);
 
-        DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);        
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);   
    
         dm.process();
 
         EasyMock.verify(zk);        
     }
 
-    public void testDataMonitorDefault() {
+    public void testProcessWatchedEvent() throws Exception {
+        ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
+
+        zk.exists(
+            Util.getZooKeeperPath(String.class.getName()),
+            false);
+        EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
+        EasyMock.expect(zk.getChildren(Util.getZooKeeperPath(String.class.getName()), dm)).andReturn(null);
       
+        EasyMock.replay(zk);
+
+        DataMonitorListener listener = EasyMock.createMock(DataMonitorListener.class);
+        listener.change();
+        EasyMock.replay(listener);
+        dm.listener = listener;
+        
+        dm.process((WatchedEvent) null);
+
+        EasyMock.verify(zk);
+        EasyMock.verify(listener);
+    }
+
+    public void testProcessWatchedEventNoExist() throws Exception {
+        ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
+
+        zk.exists(
+            Util.getZooKeeperPath(String.class.getName()),
+            false);
+        EasyMock.expectLastCall().andReturn(null);
+        EasyMock.replay(zk);
+
+        DataMonitorListener listener = EasyMock.createMock(DataMonitorListener.class);
+        EasyMock.replay(listener);
+        dm.listener = listener;
+        
+        dm.process((WatchedEvent) null);
+
+        EasyMock.verify(zk);
+        EasyMock.verify(listener);
+    }
+
+    public void testInterfaceMonitorDefault() {
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
         EasyMock.replay(zk);
 
-        DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);
+        InterfaceMonitor dm = new InterfaceMonitor(zk, String.class.getName(), null);
         EasyMock.verify(zk);        
         
         EasyMock.reset(zk);
-        zk.exists(Util.getZooKeeperPath(String.class.getName()), true, dm, null);
+        zk.exists(Util.getZooKeeperPath(String.class.getName()), dm, dm, null);
         EasyMock.expectLastCall();
         EasyMock.replay(zk);
         // This should trigger a call to zookeeper.exists() as defined above

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 



Mime
View raw message