You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by as...@apache.org on 2015/09/23 00:07:04 UTC

incubator-geode git commit: GEODE-346: Fix race condition and simplify junit

Repository: incubator-geode
Updated Branches:
  refs/heads/develop 4708d4e18 -> 71e8dc85f


GEODE-346: Fix race condition and simplify junit

Rebalancer junit were using mocks for testing distributed lock state changes.
Avoid mocks and use DistributedLockService directly


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/71e8dc85
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/71e8dc85
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/71e8dc85

Branch: refs/heads/develop
Commit: 71e8dc85fd40c91ef53c2f1ee7dca5cfb082b070
Parents: 4708d4e
Author: Ashvin Agrawal <as...@apache.org>
Authored: Tue Sep 22 14:59:53 2015 -0700
Committer: Ashvin Agrawal <as...@apache.org>
Committed: Tue Sep 22 15:06:35 2015 -0700

----------------------------------------------------------------------
 .../cache/util/AutoBalancerJUnitTest.java       | 55 +++++---------------
 1 file changed, 12 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/71e8dc85/gemfire-rebalancer/src/test/java/com/gemstone/gemfire/cache/util/AutoBalancerJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-rebalancer/src/test/java/com/gemstone/gemfire/cache/util/AutoBalancerJUnitTest.java b/gemfire-rebalancer/src/test/java/com/gemstone/gemfire/cache/util/AutoBalancerJUnitTest.java
index bae5f11..f0bcded 100644
--- a/gemfire-rebalancer/src/test/java/com/gemstone/gemfire/cache/util/AutoBalancerJUnitTest.java
+++ b/gemfire-rebalancer/src/test/java/com/gemstone/gemfire/cache/util/AutoBalancerJUnitTest.java
@@ -11,6 +11,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -146,35 +147,21 @@ public class AutoBalancerJUnitTest {
   public void testLockSuccess() throws InterruptedException {
     cache = createBasicCache();
 
-    final DistributedLockService mockDLS = mockContext.mock(DistributedLockService.class);
-    mockContext.checking(new Expectations() {
-      {
-        oneOf(mockDLS).lock(AutoBalancer.AUTO_BALANCER_LOCK, 0L, -1L);
-        will(new CustomAction("acquire lock") {
-          @Override
-          public Object invoke(Invocation invocation) throws Throwable {
-            DistributedLockService dls = new GeodeCacheFacade().getDLS();
-            return dls.lock(AutoBalancer.AUTO_BALANCER_LOCK, 0L, -1L);
-          }
-        });
-      }
-    });
-
-    final AtomicBoolean success = new AtomicBoolean(false);
+    final CountDownLatch locked = new CountDownLatch(1);
+    final AtomicBoolean success = new AtomicBoolean(true);
     Thread thread = new Thread(new Runnable() {
       @Override
       public void run() {
-        CacheOperationFacade cacheFacade = new GeodeCacheFacade() {
-          public DistributedLockService getDLS() {
-            return mockDLS;
-          };
-        };
+        CacheOperationFacade cacheFacade = new GeodeCacheFacade();
         success.set(cacheFacade.acquireAutoBalanceLock());
+        locked.countDown();
       }
     });
     thread.start();
-    thread.join();
+    locked.await(1, TimeUnit.SECONDS);
     assertTrue(success.get());
+    DistributedLockService dls = new GeodeCacheFacade().getDLS();
+    assertFalse(dls.lock(AutoBalancer.AUTO_BALANCER_LOCK, 0, -1));
   }
 
   @Test
@@ -184,29 +171,11 @@ public class AutoBalancerJUnitTest {
     DistributedLockService dls = new GeodeCacheFacade().getDLS();
     assertTrue(dls.lock(AutoBalancer.AUTO_BALANCER_LOCK, 0, -1));
 
-    final DistributedLockService mockDLS = mockContext.mock(DistributedLockService.class);
-    mockContext.checking(new Expectations() {
-      {
-        oneOf(mockDLS).lock(AutoBalancer.AUTO_BALANCER_LOCK, 0L, -1L);
-        will(new CustomAction("acquire lock") {
-          @Override
-          public Object invoke(Invocation invocation) throws Throwable {
-            DistributedLockService dls = new GeodeCacheFacade().getDLS();
-            return dls.lock(AutoBalancer.AUTO_BALANCER_LOCK, 0L, -1L);
-          }
-        });
-      }
-    });
-
     final AtomicBoolean success = new AtomicBoolean(true);
     Thread thread = new Thread(new Runnable() {
       @Override
       public void run() {
-        CacheOperationFacade cacheFacade = new GeodeCacheFacade() {
-          public DistributedLockService getDLS() {
-            return mockDLS;
-          }
-        };
+        CacheOperationFacade cacheFacade = new GeodeCacheFacade();
         success.set(cacheFacade.acquireAutoBalanceLock());
       }
     });
@@ -263,7 +232,7 @@ public class AutoBalancerJUnitTest {
   @Test
   public void testAcquireLockAfterReleasedRemotely() throws InterruptedException {
     cache = createBasicCache();
-    
+
     final CacheOperationFacade mockCacheFacade = mockContext.mock(CacheOperationFacade.class);
     final Sequence sequence = mockContext.sequence("sequence");
     mockContext.checking(new Expectations() {
@@ -279,13 +248,13 @@ public class AutoBalancerJUnitTest {
         will(returnValue(0L));
       }
     });
-    
+
     AutoBalancer balancer = new AutoBalancer();
     balancer.setCacheOperationFacade(mockCacheFacade);
     balancer.getOOBAuditor().execute();
     balancer.getOOBAuditor().execute();
   }
-  
+
   @Test
   public void testFailExecuteIfLockedElsewhere() throws InterruptedException {
     cache = createBasicCache();