geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [08/50] [abbrv] incubator-geode git commit: [GEODE-619]: Add GMSMemberJUnitTest Adding unit tests that should improve code coverage of GMSMember to ~90% Fixing array index out of bounds issue when one address is longer than the other
Date Fri, 11 Dec 2015 21:22:42 GMT
[GEODE-619]: Add GMSMemberJUnitTest
Adding unit tests that should improve code coverage of GMSMember to ~90%
Fixing array index out of bounds issue when one address is longer than the other


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

Branch: refs/heads/feature/GEODE-217
Commit: 2258d74cac3e13a162a06079b78eb83141eaa641
Parents: 442faa0
Author: Jason Huynh <huynhja@gmail.com>
Authored: Tue Dec 1 14:01:53 2015 -0800
Committer: Jason Huynh <huynhja@gmail.com>
Committed: Tue Dec 1 14:06:15 2015 -0800

----------------------------------------------------------------------
 .../internal/membership/gms/GMSMember.java      |  20 +--
 .../membership/gms/GMSMemberJUnitTest.java      | 148 +++++++++++++++++++
 2 files changed, 159 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/2258d74c/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMember.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMember.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMember.java
index f4784ed..05b3aee 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMember.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMember.java
@@ -211,7 +211,7 @@ public class GMSMember implements NetMember, DataSerializableFixedID {
     byte[] hisAddr = his.inetAddr.getAddress();
     if (myAddr != hisAddr) {
       for (int idx=0; idx < myAddr.length; idx++) {
-        if (idx > hisAddr.length) {
+        if (idx >= hisAddr.length) {
           return 1;
         } else if (myAddr[idx] > hisAddr[idx]) {
           return 1;
@@ -219,6 +219,10 @@ public class GMSMember implements NetMember, DataSerializableFixedID
{
           return -1;
         }
       }
+      //After checking both addresses we have only gone up to myAddr.length, their address
could be longer
+      if (hisAddr.length > myAddr.length) {
+        return -1;
+      }
     }
     if (udpPort < his.udpPort) return -1;
     if (his.udpPort < udpPort) return 1;
@@ -226,16 +230,14 @@ public class GMSMember implements NetMember, DataSerializableFixedID
{
 
     // bug #41983, address of kill-9'd member is reused
     // before it can be ejected from membership
-    if (result == 0) {
-      if (this.vmViewId >= 0 && his.vmViewId >= 0) {
-        if (this.vmViewId < his.vmViewId) {
-          result = -1;
-        } else if (his.vmViewId < this.vmViewId) {
-          result = 1;
-        }
+    if (this.vmViewId >= 0 && his.vmViewId >= 0) {
+      if (this.vmViewId < his.vmViewId) {
+        result = -1;
+      } else if (his.vmViewId < this.vmViewId) {
+        result = 1;
       }
     }
-    if (result == 0 && this.uuidMSBs != 0 && his.uuidMSBs != 0) {
+    if (result == 0 && this.uuidMSBs != 0 && his.uuidMSBs != 0) { 
       if (this.uuidMSBs < his.uuidMSBs) {
         result = -1;
       } else if (his.uuidMSBs < this.uuidMSBs) {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/2258d74c/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMemberJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMemberJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMemberJUnitTest.java
new file mode 100644
index 0000000..0b75d3d
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/GMSMemberJUnitTest.java
@@ -0,0 +1,148 @@
+package com.gemstone.gemfire.distributed.internal.membership.gms;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.InetAddress;
+
+import org.jgroups.util.UUID;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.distributed.internal.membership.MemberAttributes;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class GMSMemberJUnitTest {
+
+  @Test
+  public void testEqualsNotSameType() {
+    GMSMember member = new GMSMember();
+    Assert.assertFalse(member.equals("Not a GMSMember"));
+  }
+  
+  @Test
+  public void testEqualsIsSame() {
+    GMSMember member = new GMSMember();
+    Assert.assertTrue(member.equals(member));
+  }
+  
+  @Test
+  public void testCompareToIsSame() {
+    GMSMember member = new GMSMember();
+    UUID uuid = new UUID(0, 0);
+    member.setUUID(uuid);
+    Assert.assertEquals(0, member.compareTo(member));
+  }
+  
+  private GMSMember createGMSMember(byte[] inetAddress, int viewId, long msb, long lsb) {
+    GMSMember member = new GMSMember();
+    InetAddress addr1 = mock(InetAddress.class);
+    when(addr1.getAddress()).thenReturn(inetAddress);
+    member.setInetAddr(addr1);
+    member.setBirthViewId(viewId);
+    member.setUUID(new UUID(msb, lsb));
+    return member;
+  }
+  
+  @Test
+  public void testCompareToInetAddressIsLongerThan() {
+    GMSMember member1 = createGMSMember(new byte[] {1, 1, 1, 1, 1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1, 1, 1, 1}, 1, 1, 1);
+    Assert.assertEquals(1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToInetAddressIsShorterThan() {
+    GMSMember member1 = createGMSMember(new byte[] {1, 1, 1, 1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1, 1, 1, 1, 1}, 1, 1, 1);
+    Assert.assertEquals(-1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToInetAddressIsGreater() {
+    GMSMember member1 = createGMSMember(new byte[] {1, 2, 1, 1, 1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1, 1, 1, 1, 1}, 1, 1, 1);
+    Assert.assertEquals(1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToInetAddressIsLessThan() {
+    GMSMember member1 = createGMSMember(new byte[] {1, 1, 1, 1, 1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1, 2, 1, 1, 1}, 1, 1, 1);
+    Assert.assertEquals(-1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToMyViewIdLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 2, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    Assert.assertEquals(1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToTheirViewIdLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 2, 1, 1);
+    Assert.assertEquals(-1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToMyMSBLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 1, 2, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    Assert.assertEquals(1, member1.compareTo(member2));
+  }
+
+  @Test
+  public void testCompareToTheirMSBLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 1, 2, 1);
+    Assert.assertEquals(-1, member1.compareTo(member2));
+  }
+
+  @Test
+  public void testCompareToMyLSBLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 1, 1, 2);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    Assert.assertEquals(1, member1.compareTo(member2));
+  }
+  
+  @Test
+  public void testCompareToTheirLSBLarger() {
+    GMSMember member1 = createGMSMember(new byte[] {1}, 1, 1, 1);
+    GMSMember member2 = createGMSMember(new byte[] {1}, 1, 1, 2);
+    Assert.assertEquals(-1, member1.compareTo(member2));
+  }
+
+  
+  //Makes sure a NPE is not thrown
+  @Test
+  public void testNoNPEWhenSetAttributesWithNull() {
+    GMSMember member = new GMSMember();
+    member.setAttributes(null);
+    MemberAttributes attrs = member.getAttributes(); 
+    MemberAttributes invalid = MemberAttributes.INVALID;
+    Assert.assertEquals(attrs.getVmKind(), invalid.getVmKind());
+    Assert.assertEquals(attrs.getPort(), invalid.getPort());
+    Assert.assertEquals(attrs.getVmViewId(), invalid.getVmViewId());
+    Assert.assertEquals(attrs.getName(), invalid.getName());
+  }
+  
+  @Test
+  public void testGetUUIDReturnsNullWhenUUIDIs0() {
+    GMSMember member = new GMSMember();
+    UUID uuid = new UUID(0, 0);
+    member.setUUID(uuid);
+    Assert.assertNull(member.getUUID());
+  }
+  
+  @Test
+  public void testGetUUID() {
+    GMSMember member = new GMSMember();
+    UUID uuid = new UUID(1, 1);
+    member.setUUID(uuid);
+    Assert.assertNotNull(member.getUUID());
+  }
+}


Mime
View raw message