You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by es...@apache.org on 2016/05/26 22:40:19 UTC

incubator-geode git commit: Mock TXManagerImpl instead of using real one in testing PartitionMessage and RemoteOperationMessage. Avoid Thead.sleep in the unit test.

Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-1400 a429d8f9c -> b7f5d31d8


Mock TXManagerImpl instead of using real one in testing PartitionMessage and RemoteOperationMessage.
Avoid Thead.sleep in the unit test.


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

Branch: refs/heads/feature/GEODE-1400
Commit: b7f5d31d8d608f4848d48e3a528db9e1b987275a
Parents: a429d8f
Author: eshu <es...@pivotal.io>
Authored: Thu May 26 15:32:47 2016 -0700
Committer: eshu <es...@pivotal.io>
Committed: Thu May 26 15:32:47 2016 -0700

----------------------------------------------------------------------
 .../internal/cache/RemoteOperationMessage.java  | 26 ++++++++----------
 .../gemfire/internal/cache/TXManagerImpl.java   |  8 ++++--
 .../cache/partitioned/PartitionMessage.java     | 28 +++++++++-----------
 .../cache/RemoteOperationMessageTest.java       | 14 ++++++++--
 .../internal/cache/TXManagerImplTest.java       | 26 ++++++++++--------
 .../cache/partitioned/PartitionMessageTest.java | 15 ++++++++---
 6 files changed, 68 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessage.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessage.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessage.java
index a041371..f7b8a1a 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessage.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessage.java
@@ -236,14 +236,18 @@ public abstract class RemoteOperationMessage extends DistributionMessage impleme
       
       // [bruce] r might be null here, so we have to go to the cache instance to get the txmgr
       TXManagerImpl txMgr = getTXManager(gfc);
-      TXStateProxy tx = null;
-      try {
-        tx = masqueradeAs(this, txMgr);
-        if (!hasTxAlreadyFinished(tx, txMgr, new TXId(getMemberToMasqueradeAs(), getTXUniqId()))) {
-          sendReply = operateOnRegion(dm, r, startTime);
+      TXStateProxy tx = txMgr.masqueradeAs(this);
+      if (tx == null) {
+        sendReply = operateOnRegion(dm, r, startTime);        
+      } else {
+        try {
+          TXId txid = new TXId(getMemberToMasqueradeAs(), getTXUniqId());
+          if (!hasTxAlreadyFinished(tx, txMgr, txid)) {
+            sendReply = operateOnRegion(dm, r, startTime);       
+          }  
+        } finally {
+          txMgr.unmasquerade(tx);
         }
-      } finally {
-        unmasquerade(txMgr, tx);
       }
       thr = null;
           
@@ -313,17 +317,9 @@ public abstract class RemoteOperationMessage extends DistributionMessage impleme
     }
   }
 
-  void unmasquerade(TXManagerImpl txMgr, TXStateProxy tx) {
-    txMgr.unmasquerade(tx);
-  }
-
   boolean hasTxAlreadyFinished(TXStateProxy tx, TXManagerImpl txMgr, TXId txid) {
     return txMgr.hasTxAlreadyFinished(tx, txid);
   }
-  
-  TXStateProxy masqueradeAs(TransactionMessage msg, TXManagerImpl txMgr) throws InterruptedException {
-    return txMgr.masqueradeAs(msg);
-  }
 
   TXManagerImpl getTXManager(GemFireCacheImpl cache) {
     return cache.getTxManager();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
index 25303c2..8ae6406 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/TXManagerImpl.java
@@ -84,7 +84,7 @@ import com.gemstone.gemfire.internal.util.concurrent.CustomEntryConcurrentHashMa
  * 
  * @see CacheTransactionManager
  */
-public final class TXManagerImpl implements CacheTransactionManager,
+public class TXManagerImpl implements CacheTransactionManager,
     MembershipListener {
 
   private static final Logger logger = LogService.getLogger();
@@ -735,7 +735,11 @@ public final class TXManagerImpl implements CacheTransactionManager,
       boolean success = getLock(val, key);
       while (!success) {
         val = getOrSetHostedTXState(key, msg);
-        success = getLock(val, key);
+        if (val != null) {
+          success = getLock(val, key);
+        } else {
+          break;
+        }
       }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessage.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessage.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessage.java
index 4f743d3..97fb973 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessage.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessage.java
@@ -297,10 +297,7 @@ public abstract class PartitionMessage extends DistributionMessage implements
   long getStartPartitionMessageProcessingTime(PartitionedRegion pr) {
     return pr.getPrStats().startPartitionMessageProcessing();
   }
-  
-  TXStateProxy masqueradeAs(TransactionMessage msg, TXManagerImpl txMgr) throws InterruptedException {
-    return txMgr.masqueradeAs(msg);
-  }
+
   
   /**
    * Upon receipt of the message, both process the message and send an
@@ -341,14 +338,18 @@ public abstract class PartitionMessage extends DistributionMessage implements
         throw new ForceReattemptException(LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString());
       }
       TXManagerImpl txMgr = getTXManagerImpl(cache);
-      TXStateProxy tx = null;
-      try {
-        tx = masqueradeAs(this, txMgr);
-        if (!hasTxAlreadyFinished(tx, txMgr, new TXId(getMemberToMasqueradeAs(), getTXUniqId()))) {
-          sendReply = operateOnPartitionedRegion(dm, pr, startTime);
+      TXStateProxy tx = txMgr.masqueradeAs(this);
+      if (tx == null) {
+        sendReply = operateOnPartitionedRegion(dm, pr, startTime);        
+      } else {
+        try {
+          TXId txid = new TXId(getMemberToMasqueradeAs(), getTXUniqId());
+          if (!hasTxAlreadyFinished(tx, txMgr, txid)) {
+            sendReply = operateOnPartitionedRegion(dm, pr, startTime);        
+          }  
+        } finally {
+          txMgr.unmasquerade(tx);
         }
-      } finally {
-        unmasquerade(txMgr, tx);
       }
       thr = null;
           
@@ -427,11 +428,6 @@ public abstract class PartitionMessage extends DistributionMessage implements
       } 
     }
   }
-
-
-  void unmasquerade(TXManagerImpl txMgr, TXStateProxy tx) {
-    txMgr.unmasquerade(tx);
-  }
   
   /** Send a generic ReplyMessage.  This is in a method so that subclasses can override the reply message type
    * @param pr the Partitioned Region for the message whose statistics are incremented

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessageTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessageTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessageTest.java
index 1ceac16..ecfc2b0 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessageTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/RemoteOperationMessageTest.java
@@ -50,7 +50,7 @@ public class RemoteOperationMessageTest {
     dm = mock(DistributionManager.class);  
     msg = mock(RemoteOperationMessage.class);
     r = mock(LocalRegion.class);
-    txMgr = new TXManagerImpl(null, cache);
+    txMgr = mock(TXManagerImpl.class);
     txid = new TXId(null, 0);
     tx = mock(TXStateProxyImpl.class);
     
@@ -59,13 +59,22 @@ public class RemoteOperationMessageTest {
     when(msg.getCache(dm)).thenReturn(cache);
     when(msg.getRegionByPath(cache)).thenReturn(r);
     when(msg.getTXManager(cache)).thenReturn(txMgr);
-    when(msg.masqueradeAs(msg, txMgr)).thenReturn(tx);
+    when(txMgr.hasTxAlreadyFinished(tx, txid)).thenCallRealMethod();
 
     doAnswer(new CallsRealMethods()).when(msg).process(dm);    
   }
+  
+  @Test
+  public void messageWithNoTXPerformsOnRegion() throws InterruptedException, RemoteOperationException {
+    when(txMgr.masqueradeAs(msg)).thenReturn(null);
+    msg.process(dm);
+
+    verify(msg, times(1)).operateOnRegion(dm, r, startTime);
+  }
 
   @Test
   public void messageForNotFinishedTXPerformsOnRegion() throws InterruptedException, RemoteOperationException {
+    when(txMgr.masqueradeAs(msg)).thenReturn(tx);
     when(msg.hasTxAlreadyFinished(tx, txMgr, txid)).thenCallRealMethod(); 
     msg.process(dm);
 
@@ -74,6 +83,7 @@ public class RemoteOperationMessageTest {
   
   @Test
   public void messageForFinishedTXDoesNotPerformOnRegion() throws InterruptedException, RemoteOperationException {
+    when(txMgr.masqueradeAs(msg)).thenReturn(tx);
     when(msg.hasTxAlreadyFinished(tx, txMgr, txid)).thenReturn(true); 
     msg.process(dm);
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/TXManagerImplTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/TXManagerImplTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/TXManagerImplTest.java
index ea702db..efbb271 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/TXManagerImplTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/TXManagerImplTest.java
@@ -20,6 +20,9 @@ package com.gemstone.gemfire.internal.cache;
 import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -29,6 +32,7 @@ import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedM
 import com.gemstone.gemfire.internal.cache.partitioned.DestroyMessage;
 import com.gemstone.gemfire.test.fake.Fakes;
 import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import com.jayway.awaitility.Awaitility;
 
 
 @Category(UnitTest.class)
@@ -42,6 +46,9 @@ public class TXManagerImplTest {
   TXId completedTxid;
   TXId notCompletedTxid;
   InternalDistributedMember member;
+  CountDownLatch latch = new CountDownLatch(1);
+  volatile int count = 0;
+  TXStateProxy tx1, tx2;
 
   @Before
   public void setUp() {
@@ -177,20 +184,16 @@ public class TXManagerImplTest {
     
     Thread t1 = new Thread(new Runnable() {
       public void run() {
-        TXStateProxy tx1, tx2;
-
         tx1 = txMgr.getHostedTXState(txid);
         assertNull(tx1);
         tx1 =txMgr.getOrSetHostedTXState(txid, msg);
         assertNotNull(tx1);
         assertTrue(txMgr.getLock(tx1, txid));
 
-        try {
-          Thread.sleep(20);
-        } catch (InterruptedException e) {
-          throw new RuntimeException(e);
-        }
-
+        latch.countDown();
+        
+        Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> tx1.getLock().hasQueuedThreads()); 
+        
         txMgr.removeHostedTXState(txid);
         
         tx2 =txMgr.getOrSetHostedTXState(txid, msg);
@@ -202,11 +205,12 @@ public class TXManagerImplTest {
       }
     });
     t1.start();
-    
-    Thread.sleep(10);
-
+            
+    latch.await(15, TimeUnit.SECONDS);
+  
     tx = txMgr.masqueradeAs(msg);
     assertNotNull(tx);
+    assertEquals(tx, tx2);
     tx.getLock().unlock();
 
     t1.join();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b7f5d31d/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessageTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessageTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessageTest.java
index 4b989d6..bbbf714 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessageTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/partitioned/PartitionMessageTest.java
@@ -57,7 +57,7 @@ public class PartitionMessageTest {
     dm = mock(DistributionManager.class);  
     msg = mock(PartitionMessage.class);
     pr = mock(PartitionedRegion.class);
-    txMgr = new TXManagerImpl(null, cache);
+    txMgr = mock(TXManagerImpl.class);
     tx = mock(TXStateProxyImpl.class);
     txid = new TXId(null, 0);
     
@@ -67,14 +67,22 @@ public class PartitionMessageTest {
     when(msg.getGemFireCacheImpl()).thenReturn(cache);
     when(msg.getStartPartitionMessageProcessingTime(pr)).thenReturn(startTime);
     when(msg.getTXManagerImpl(cache)).thenReturn(txMgr);
-    when(msg.masqueradeAs(msg, txMgr)).thenReturn(tx);
+    when(msg.hasTxAlreadyFinished(null, txMgr, txid)).thenCallRealMethod();
     
     doAnswer(new CallsRealMethods()).when(msg).process(dm);    
   }
 
   @Test
+  public void messageWithNoTXPerformsOnRegion() throws InterruptedException, CacheException, QueryException, DataLocationException, IOException {   
+    when(txMgr.masqueradeAs(msg)).thenReturn(null);
+    msg.process(dm);
+    
+    verify(msg, times(1)).operateOnPartitionedRegion(dm, pr, startTime);
+  }
+  
+  @Test
   public void messageForNotFinishedTXPerformsOnRegion() throws InterruptedException, CacheException, QueryException, DataLocationException, IOException {   
-    when(msg.hasTxAlreadyFinished(tx, txMgr, txid)).thenReturn(false);
+    when(txMgr.masqueradeAs(msg)).thenReturn(tx);
     msg.process(dm);
     
     verify(msg, times(1)).operateOnPartitionedRegion(dm, pr, startTime);
@@ -82,6 +90,7 @@ public class PartitionMessageTest {
   
   @Test
   public void messageForFinishedTXDoesNotPerformOnRegion() throws InterruptedException, CacheException, QueryException, DataLocationException, IOException {   
+    when(txMgr.masqueradeAs(msg)).thenReturn(tx);
     when(msg.hasTxAlreadyFinished(tx, txMgr, txid)).thenReturn(true);
     msg.process(dm);