Merge branch 'CURATOR-470' of github.com:JerryChin/curator into CURATOR-470


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

Branch: refs/heads/CURATOR-470
Commit: 6b1522c5f2892451be45df89e6537799f146ab51
Parents: ba8ade0 88e8d9a
Author: randgalt <[email protected]>
Authored: Sun Jun 24 09:26:35 2018 -0500
Committer: randgalt <[email protected]>
Committed: Sun Jun 24 09:26:35 2018 -0500

----------------------------------------------------------------------
 .../details/ServiceCacheEventListener.java      |  50 ++++++++
 .../x/discovery/details/ServiceCacheImpl.java   | 115 +++++++++++++------
 .../curator/x/discovery/TestServiceCache.java   |  80 +++++++++++++
 3 files changed, 213 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/6b1522c5/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceCacheImpl.java
----------------------------------------------------------------------
diff --cc 
curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceCacheImpl.java
index d1a31ad,df6696a..ffaf1a4
--- 
a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceCacheImpl.java
+++ 
b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/details/ServiceCacheImpl.java
@@@ -97,23 -91,9 +97,23 @@@ public class ServiceCacheImpl<T> implem
          Preconditions.checkState(state.compareAndSet(State.LATENT, 
State.STARTED), "Cannot be started more than once");
  
          cache.start(true);
 +        if ( debugStartLatch != null )
 +        {
 +            debugStartLatch.countDown();
 +            debugStartLatch = null;
 +        }
 +        if ( debugStartWaitLatch != null )
 +        {
 +            debugStartWaitLatch.await();
 +            debugStartWaitLatch = null;
 +        }
 +
          for ( ChildData childData : cache.getCurrentData() )
          {
 -            addInstanceOnlyIfAbsent(childData);
 +            if ( childData.getData() != null )  // else already processed by 
the cache listener
 +            {
-                 addInstance(childData, true);
++                addInstanceOnlyIfAbsent(childData);
 +            }
          }
          discovery.cacheOpened(this);
      }
@@@ -166,21 -146,69 +166,75 @@@
      @Override
      public void childEvent(CuratorFramework client, PathChildrenCacheEvent 
event) throws Exception
      {
-         boolean         notifyListeners = false;
-         switch ( event.getType() )
+           final Tuple<T> tuple;
+           switch ( event.getType() )
          {
              case CHILD_ADDED:
+                   tuple = addOrUpdateInstance(event.getData());
+                 listenerContainer.forEach(
+                     new Function<ServiceCacheListener, Void>()
+                     {
+                         @Override
+                         public Void apply(ServiceCacheListener listener)
+                         {
+                             listener.cacheChanged();
+ 
 -                            if(listener instanceof  
ServiceCacheEventListener) {
++                            if ( listener instanceof 
ServiceCacheEventListener )
++                            {
++                                //noinspection unchecked
+                                 ((ServiceCacheEventListener) 
listener).cacheAdded(tuple.newInstance);
+                             }
+ 
+                             return null;
+                         }
+                     }
+                 );
+                 break;
              case CHILD_UPDATED:
              {
-                 addInstance(event.getData(), false);
-                 notifyListeners = true;
+                   tuple = addOrUpdateInstance(event.getData());
+                   listenerContainer.forEach(
+                                   new Function<ServiceCacheListener, Void>()
+                                   {
+                                           @Override
+                                           public Void 
apply(ServiceCacheListener listener)
+                                           {
+                                                   listener.cacheChanged();
+ 
 -                                                  if(listener instanceof  
ServiceCacheEventListener) {
 -                                                          
((ServiceCacheEventListener) listener).cacheUpdated(tuple.oldInstance, 
tuple.newInstance);
++                                                  if( listener instanceof 
ServiceCacheEventListener )
++                                                  {
++                                    //noinspection unchecked
++                                    ((ServiceCacheEventListener) 
listener).cacheUpdated(tuple.oldInstance, tuple.newInstance);
+                                                   }
+ 
+                                                   return null;
+                                           }
+                                   }
+                   );
                  break;
              }
  
              case CHILD_REMOVED:
              {
-                 instances.remove(instanceIdFromData(event.getData()));
-                 notifyListeners = true;
+                 final ServiceInstance<T> serviceInstance = 
instances.remove(instanceIdFromData(event.getData()));
+                   listenerContainer.forEach(
+                                   new Function<ServiceCacheListener, Void>()
+                                   {
+                                           @Override
+                                           public Void 
apply(ServiceCacheListener listener)
+                                           {
+                                                   listener.cacheChanged();
+ 
 -                                                  if(listener instanceof 
ServiceCacheEventListener) {
 -                                                          
((ServiceCacheEventListener) listener).cacheDeleted(serviceInstance);
++                                                  if ( listener instanceof 
ServiceCacheEventListener )
++                                                  {
++                                    //noinspection unchecked
++                                    ((ServiceCacheEventListener) 
listener).cacheDeleted(serviceInstance);
+                                                   }
+ 
+                                                   return null;
+                                           }
+                                   }
+                   );
                  break;
              }
          }

Reply via email to