geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sai_boorlaga...@apache.org
Subject geode git commit: Added tests for afterPrimary and afterRegionCreate
Date Tue, 06 Dec 2016 18:36:32 GMT
Repository: geode
Updated Branches:
  refs/heads/feature/GEODE-523 [created] 1ff5abb07


Added tests for afterPrimary and afterRegionCreate


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

Branch: refs/heads/feature/GEODE-523
Commit: 1ff5abb078a3b405de190f1199b582b05488b4d4
Parents: 0bd9fc9
Author: Sai Boorlagadda <sai_boorlagadda@apache.org>
Authored: Tue Dec 6 10:35:28 2016 -0800
Committer: Sai Boorlagadda <sai_boorlagadda@apache.org>
Committed: Tue Dec 6 10:35:28 2016 -0800

----------------------------------------------------------------------
 .../cache/partition/PartitionListener.java      |  2 -
 .../cache/PartitionListenerDUnitTest.java       | 94 +++++++++++++++++++-
 2 files changed, 92 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/1ff5abb0/geode-core/src/main/java/org/apache/geode/cache/partition/PartitionListener.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/cache/partition/PartitionListener.java
b/geode-core/src/main/java/org/apache/geode/cache/partition/PartitionListener.java
index 2a79459..3aee284 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/partition/PartitionListener.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/partition/PartitionListener.java
@@ -71,8 +71,6 @@ import org.apache.geode.cache.Region;
  * 
  * @see PartitionAttributesFactory#addPartitionListener(PartitionListener)
  * 
- *      Note : Please contact support@gemstone.com before using these APIs
- * 
  * @since GemFire 6.5
  * 
  */

http://git-wip-us.apache.org/repos/asf/geode/blob/1ff5abb0/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionListenerDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionListenerDUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionListenerDUnitTest.java
index 16bac77..14e3d11 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionListenerDUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/PartitionListenerDUnitTest.java
@@ -17,10 +17,11 @@ package org.apache.geode.internal.cache;
 import org.junit.experimental.categories.Category;
 import org.junit.Test;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.*;
+import static org.assertj.core.api.Assertions.*;
 
 import org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase;
-import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase;
 import org.apache.geode.test.junit.categories.DistributedTest;
 
 import java.util.ArrayList;
@@ -37,7 +38,6 @@ import org.apache.geode.cache.PartitionAttributesFactory;
 import org.apache.geode.cache.Region;
 import org.apache.geode.cache.control.RebalanceOperation;
 import org.apache.geode.cache.partition.PartitionListenerAdapter;
-import org.apache.geode.cache30.CacheTestCase;
 import org.apache.geode.distributed.DistributedMember;
 import org.apache.geode.test.dunit.Host;
 import org.apache.geode.test.dunit.SerializableCallable;
@@ -69,11 +69,29 @@ public class PartitionListenerDUnitTest extends JUnit4CacheTestCase {
     createPR(vm0, regionName, true);
     createData(vm0, 0, 1000, "A", regionName);
 
+    //Assert that afterPrimary is invoked for every primary created on the vm
+    List<Integer> vm1PrimariesCreated = getPrimariesCreated(vm1, regionName);
+    List<Integer> vm2PrimariesCreated = getPrimariesCreated(vm2, regionName);
+    List<Integer> vm1ActualPrimaries = getPrimariesOn(vm1, regionName);
+    List<Integer> vm2ActualPrimaries = getPrimariesOn(vm2, regionName);
+
+    vm1PrimariesCreated.removeAll(vm1ActualPrimaries);
+    vm2PrimariesCreated.removeAll(vm2ActualPrimaries);
+
+    assertThat(vm1PrimariesCreated).isEmpty();
+    assertThat(vm2PrimariesCreated).isEmpty();
+
     // Create the PR in a third JVM and rebalance
     createPR(vm3, regionName, false);
     rebalance(vm3);
 
     // Verify listener invocations
+    //Assert afterRegionCreate is invoked on every VM.
+    assertEquals(regionName, getRegionNameFromListener(vm0, regionName));
+    assertEquals(regionName, getRegionNameFromListener(vm1, regionName));
+    assertEquals(regionName, getRegionNameFromListener(vm2, regionName));
+    assertEquals(regionName, getRegionNameFromListener(vm3, regionName));
+
     // Get all buckets and keys removed from VM1 and VM2
     Map<Integer, List<Integer>> allBucketsAndKeysRemoved = new HashMap<Integer,
List<Integer>>();
     allBucketsAndKeysRemoved.putAll(getBucketsAndKeysRemoved(vm1, regionName));
@@ -84,6 +102,13 @@ public class PartitionListenerDUnitTest extends JUnit4CacheTestCase {
 
     // Verify that they are equal
     assertEquals(allBucketsAndKeysRemoved, vm3BucketsAndKeysAdded);
+
+    // Verify afterPrimary is invoked after rebalance.
+    List<Integer> vm3PrimariesCreated = getPrimariesCreated(vm3, regionName);
+    List<Integer> vm3ActualPrimaries = getPrimariesOn(vm3, regionName);
+
+    vm3ActualPrimaries.removeAll(vm3PrimariesCreated);
+    assertThat(vm3ActualPrimaries).isEmpty();
   }
 
   protected DistributedMember createPR(VM vm, final String regionName, final boolean isAccessor)
@@ -124,6 +149,50 @@ public class PartitionListenerDUnitTest extends JUnit4CacheTestCase {
     vm.invoke(createData);
   }
 
+  protected List<Integer> getPrimariesOn(VM vm, final String regionName) {
+    SerializableCallable getPrimariesOn =
+        new SerializableCallable("getPrimariesOn") {
+
+          public Object call() {
+            Cache cache = getCache();
+            Region region = cache.getRegion(regionName);
+            return new ArrayList<>(((PartitionedRegion) region).getDataStore()
+                .getAllLocalPrimaryBucketIds());
+          }
+        };
+    return (List<Integer>) vm.invoke(getPrimariesOn);
+  }
+
+  protected List<Integer> getPrimariesCreated(VM vm, final String regionName) {
+    SerializableCallable getPrimariesCreated =
+        new SerializableCallable("getPrimariesCreated") {
+
+          public Object call() {
+            Cache cache = getCache();
+            Region region = cache.getRegion(regionName);
+            TestPartitionListener listener = (TestPartitionListener) region.getAttributes()
+                .getPartitionAttributes().getPartitionListeners()[0];
+            return listener.getPrimariesCreated();
+          }
+        };
+    return (List<Integer>) vm.invoke(getPrimariesCreated);
+  }
+
+  protected String getRegionNameFromListener(VM vm, final String regionName) {
+    SerializableCallable getRegionName =
+        new SerializableCallable("getRegionName") {
+
+          public Object call() {
+            Cache cache = getCache();
+            Region region = cache.getRegion(regionName);
+            TestPartitionListener listener = (TestPartitionListener) region.getAttributes()
+                .getPartitionAttributes().getPartitionListeners()[0];
+            return listener.getRegionName();
+          }
+        };
+    return (String) vm.invoke(getRegionName);
+  }
+
   protected Map<Integer, List<Integer>> getBucketsAndKeysRemoved(VM vm, final
String regionName) {
     SerializableCallable getBucketsAndKeysRemoved =
         new SerializableCallable("getBucketsAndKeysRemoved") {
@@ -168,11 +237,16 @@ public class PartitionListenerDUnitTest extends JUnit4CacheTestCase
{
 
   protected static class TestPartitionListener extends PartitionListenerAdapter {
 
+    private String regionName;
+
+    private final List<Integer> primariesCreated;
+
     private final Map<Integer, List<Integer>> bucketsAndKeysRemoved;
 
     private final Map<Integer, List<Integer>> bucketsAndKeysAdded;
 
     public TestPartitionListener() {
+      this.primariesCreated = new ArrayList<>();
       this.bucketsAndKeysRemoved = new HashMap<Integer, List<Integer>>();
       this.bucketsAndKeysAdded = new HashMap<Integer, List<Integer>>();
     }
@@ -185,6 +259,22 @@ public class PartitionListenerDUnitTest extends JUnit4CacheTestCase {
       return this.bucketsAndKeysAdded;
     }
 
+    public List<Integer> getPrimariesCreated() {
+      return this.primariesCreated;
+    }
+
+    public String getRegionName() {
+      return this.regionName;
+    }
+
+    public  void afterRegionCreate(Region<?, ?> region) {
+      this.regionName = region.getName();
+    }
+
+    public void afterPrimary(int bucketId) {
+      this.primariesCreated.add(bucketId);
+    }
+
     public void afterBucketRemoved(int bucketId, Iterable<?> keys) {
       Collection<Integer> keysCol = (Collection) keys;
       // If the keys collection is not empty, create a serializable list to hold


Mime
View raw message