geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jasonhu...@apache.org
Subject incubator-geode git commit: Adding GMSJoinLeaveJUnitTest and adding Mockito as dependency
Date Mon, 10 Aug 2015 22:16:46 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-77 e71780aea -> 14d378647


Adding GMSJoinLeaveJUnitTest and adding Mockito as dependency


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

Branch: refs/heads/feature/GEODE-77
Commit: 14d37864730a00fca77c7dbe527a9cf399eb5735
Parents: e71780a
Author: Jason Huynh <jhuynh@Jason-Huynh-MacBook-Pro.local>
Authored: Thu Aug 6 10:37:25 2015 -0700
Committer: Jason Huynh <jhuynh@pdx2-office-dhcp17.eng.vmware.com>
Committed: Mon Aug 10 15:17:00 2015 -0700

----------------------------------------------------------------------
 gemfire-core/build.gradle                       |   3 +-
 .../membership/InternalDistributedMember.java   |  24 ++
 .../membership/gms/membership/GMSJoinLeave.java |   7 +
 .../gms/membership/GMSJoinLeaveJUnitTest.java   | 239 +++++++++++++++++++
 4 files changed, 272 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/14d37864/gemfire-core/build.gradle
----------------------------------------------------------------------
diff --git a/gemfire-core/build.gradle b/gemfire-core/build.gradle
index aa30022..4f7ab5c 100755
--- a/gemfire-core/build.gradle
+++ b/gemfire-core/build.gradle
@@ -78,7 +78,8 @@ dependencies {
   testRuntime 'org.apache.derby:derby:10.2.2.0'
   testCompile 'net.spy:spymemcached:2.9.0'
   testCompile 'redis.clients:jedis:2.7.2'
-
+  
+  testCompile 'org.mockito:mockito-core:1.10.19'
   jcaCompile sourceSets.main.output
 
   provided project(path: ':gemfire-junit', configuration: 'testOutput')

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/14d37864/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
index d19a23b..470fe3d 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/InternalDistributedMember.java
@@ -277,9 +277,33 @@ public final class InternalDistributedMember
    * @throws UnknownHostException if the given hostname cannot be resolved
    */
   public InternalDistributedMember(String i, int p) throws UnknownHostException {
+    this(i, p, Version.CURRENT);
+  }
+  
+  /**
+   * Create a InternalDistributedMember referring to the current host (as defined by the
given
+   * string).<p>
+   *
+   * <b>
+   * [bruce]THIS METHOD IS FOR TESTING ONLY.  DO NOT USE IT TO CREATE IDs FOR
+   * USE IN THE PRODUCT.  IT DOES NOT PROPERLY INITIALIZE ATTRIBUTES NEEDED
+   * FOR P2P FUNCTIONALITY.
+   * </b>
+   *
+   * 
+   * @param i
+   *          the hostname, must be for the current host
+   * @param p
+   *          the membership listening port
+   * @param version
+   *          the version of this member
+   * @throws UnknownHostException if the given hostname cannot be resolved
+   */
+  public InternalDistributedMember(String i, int p, Version version) throws UnknownHostException
{
     netMbr = MemberFactory.newNetMember(i, p);
     defaultToCurrentHost();
     this.vmKind = DistributionManager.NORMAL_DM_TYPE;
+    this.versionObj = version;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/14d37864/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeave.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeave.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeave.java
index 3aa80ab..e3c2ba2 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeave.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeave.java
@@ -337,6 +337,12 @@ public class GMSJoinLeave implements JoinLeave, MessageHandler {
     }
   }
   
+  //for testing purposes, returns a copy of the view requests for verification
+  List<DistributionMessage> getViewRequests() {
+    synchronized(viewRequests) {
+      return new LinkedList<DistributionMessage>(viewRequests);
+    }
+  }
   
   /**
    * Yippeee - I get to be the coordinator
@@ -624,6 +630,7 @@ public class GMSJoinLeave implements JoinLeave, MessageHandler {
       preparedView = null;
       lastConflictingView = null;
       services.installView(newView);
+      
       if (!newView.getCreator().equals(this.localAddress)) {
         if (newView.shouldBeCoordinator(this.localAddress)) {
           becomeCoordinator();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/14d37864/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeaveJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeaveJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeaveJUnitTest.java
new file mode 100644
index 0000000..c738367
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/distributed/internal/membership/gms/membership/GMSJoinLeaveJUnitTest.java
@@ -0,0 +1,239 @@
+package com.gemstone.gemfire.distributed.internal.membership.gms.membership;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import com.gemstone.gemfire.distributed.internal.DistributionConfig;
+import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
+import com.gemstone.gemfire.distributed.internal.membership.NetView;
+import com.gemstone.gemfire.distributed.internal.membership.gms.ServiceConfig;
+import com.gemstone.gemfire.distributed.internal.membership.gms.Services;
+import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.Authenticator;
+import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.Manager;
+import com.gemstone.gemfire.distributed.internal.membership.gms.interfaces.Messenger;
+import com.gemstone.gemfire.distributed.internal.membership.gms.messages.InstallViewMessage;
+import com.gemstone.gemfire.distributed.internal.membership.gms.messages.JoinRequestMessage;
+import com.gemstone.gemfire.distributed.internal.membership.gms.messages.JoinResponseMessage;
+import com.gemstone.gemfire.distributed.internal.membership.gms.messages.ViewAckMessage;
+import com.gemstone.gemfire.internal.Version;
+import com.gemstone.gemfire.security.AuthenticationFailedException;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class GMSJoinLeaveJUnitTest {
+  private Services services;
+  private ServiceConfig mockConfig;
+  private DistributionConfig mockDistConfig;
+  private Authenticator authenticator;
+  private InternalDistributedMember gmsJoinLeaveMemberId;
+  private InternalDistributedMember[] mockMembers;
+  private InternalDistributedMember mockOldMember;
+  private Object credentials = new Object();
+  private Messenger messenger;
+  private GMSJoinLeave gmsJoinLeave;
+  
+  public void initMocks() throws IOException {
+    initMocks(false);
+  }
+  
+  public void initMocks(boolean enableNetworkPartition) throws UnknownHostException {
+    mockDistConfig = mock(DistributionConfig.class);
+    when(mockDistConfig.getEnableNetworkPartitionDetection()).thenReturn(enableNetworkPartition);
+    mockConfig = mock(ServiceConfig.class);
+    when(mockConfig.getDistributionConfig()).thenReturn(mockDistConfig);
+    
+    authenticator = mock(Authenticator.class);
+    gmsJoinLeaveMemberId = new InternalDistributedMember("localhost", 8887);
+    
+    messenger = mock(Messenger.class);
+    when(messenger.getMemberID()).thenReturn(gmsJoinLeaveMemberId);
+    
+    services = mock(Services.class);
+    when(services.getConfig()).thenReturn(mockConfig);
+    when(services.getMessenger()).thenReturn(messenger);
+
+    mockMembers = new InternalDistributedMember[4];
+    for (int i = 0; i < mockMembers.length; i++) {
+      mockMembers[i] = new InternalDistributedMember("localhost", 8888 + i);
+    }
+    mockOldMember = new InternalDistributedMember("localhost", 8700, Version.GFE_56);
+
+    gmsJoinLeave = new GMSJoinLeave();
+    gmsJoinLeave.init(services);
+    gmsJoinLeave.start();
+    gmsJoinLeave.started();
+  }
+  
+  @Test
+  public void testProcessJoinMessageRejectOldMemberVersion() throws IOException {
+    initMocks();
+    MethodExecuted messageSent = new MethodExecuted();
+    when(messenger.send(any(JoinResponseMessage.class))).thenAnswer(messageSent);
+ 
+    gmsJoinLeave.processMessage(new JoinRequestMessage(mockOldMember, mockOldMember, null));
+    Assert.assertTrue("JoinRequest should not have been added to view request", gmsJoinLeave.getViewRequests().size()
== 0);
+    Assert.assertTrue("Join Response should have been sent", messageSent.isMethodExecuted());
+  }
+  
+  @Test
+  public void testProcessJoinMessageWithBadAuthentication() throws IOException {
+    initMocks();
+    MethodExecuted messageSent = new MethodExecuted();
+    when(services.getAuthenticator()).thenReturn(authenticator);
+    when(authenticator.authenticate(mockMembers[0], credentials)).thenThrow(new AuthenticationFailedException("we
want to fail auth here"));
+    when(services.getMessenger()).thenReturn(messenger);
+    when(messenger.send(any(JoinResponseMessage.class))).thenAnswer(messageSent);
+         
+    gmsJoinLeave.processMessage(new JoinRequestMessage(mockMembers[0], mockMembers[0], credentials));
+    Assert.assertTrue("JoinRequest should not have been added to view request", gmsJoinLeave.getViewRequests().size()
== 0);
+    Assert.assertTrue("Join Response should have been sent", messageSent.isMethodExecuted());
+  }
+  
+  @Test
+  public void testProcessJoinMessageWithAuthenticationButNullCredentials() throws IOException
{
+    initMocks();
+    MethodExecuted messageSent = new MethodExecuted();
+    when(services.getAuthenticator()).thenReturn(authenticator);
+    when(authenticator.authenticate(mockMembers[0], null)).thenThrow(new AuthenticationFailedException("we
want to fail auth here"));
+    when(services.getMessenger()).thenReturn(messenger);
+    when(messenger.send(any(JoinResponseMessage.class))).thenAnswer(messageSent);
+      
+    gmsJoinLeave.processMessage(new JoinRequestMessage(mockMembers[0], mockMembers[0], credentials));
+    Assert.assertTrue("JoinRequest should not have been added to view request", gmsJoinLeave.getViewRequests().size()
== 0);
+    Assert.assertTrue("Join Response should have been sent", messageSent.isMethodExecuted());
+  }
+  
+  
+  private void prepareView() throws IOException {
+    int viewId = 1;
+    List<InternalDistributedMember> mbrs = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> shutdowns = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> crashes = new LinkedList<InternalDistributedMember>();
+    mbrs.add(mockMembers[0]);
+    
+    when(services.getMessenger()).thenReturn(messenger);
+    MethodExecuted viewAckMessageSent = new MethodExecuted();
+    when(messenger.send(any(ViewAckMessage.class))).thenAnswer(viewAckMessageSent);
+    
+    //prepare the view
+    NetView netView = new NetView(mockMembers[0], viewId, mbrs, shutdowns, crashes);
+    InstallViewMessage installViewMessage = new InstallViewMessage(netView, credentials,
true);
+    gmsJoinLeave.processMessage(installViewMessage);
+    Assert.assertTrue("View should have been acknowledged", viewAckMessageSent.isMethodExecuted());
+  }
+  
+  private void prepareAndInstallView() throws IOException {
+    int viewId = 1;
+    List<InternalDistributedMember> mbrs = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> shutdowns = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> crashes = new LinkedList<InternalDistributedMember>();
+    mbrs.add(mockMembers[0]);
+    
+    when(services.getMessenger()).thenReturn(messenger);
+    MethodExecuted viewAckMessageSent = new MethodExecuted();
+    when(messenger.send(any(ViewAckMessage.class))).thenAnswer(viewAckMessageSent);    
+    
+    //prepare the view
+    NetView netView = new NetView(mockMembers[0], viewId, mbrs, shutdowns, crashes);
+    InstallViewMessage installViewMessage = new InstallViewMessage(netView, credentials,
true);
+    gmsJoinLeave.processMessage(installViewMessage);
+    Assert.assertTrue("View should have been acknowledged", viewAckMessageSent.isMethodExecuted());
+    
+    //install the view
+    installViewMessage = new InstallViewMessage(netView, credentials, false);
+    gmsJoinLeave.processMessage(installViewMessage);
+    Assert.assertEquals(netView, gmsJoinLeave.getView());
+  }
+  
+  @Test 
+  public void testRejectOlderView() throws IOException {
+    initMocks();
+    prepareAndInstallView();
+    
+    List<InternalDistributedMember> mbrs = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> shutdowns = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> crashes = new LinkedList<InternalDistributedMember>();
+    mbrs.add(mockMembers[0]);
+    mbrs.add(mockMembers[1]);
+  
+    //try to install an older view where viewId < currentView.viewId
+    NetView olderNetView = new NetView(mockMembers[0], 0, mbrs, shutdowns, crashes);
+    InstallViewMessage installViewMessage = new InstallViewMessage(olderNetView, credentials,
false);
+    gmsJoinLeave.processMessage(installViewMessage);
+    Assert.assertNotEquals(gmsJoinLeave.getView(), olderNetView);
+  }
+  
+  @Test 
+  public void testForceDisconnectedFromNewView() throws IOException {
+    initMocks(true);//enabledNetworkPartition;
+    Manager mockManager = mock(Manager.class);
+    MethodExecuted forceDisconnectTriggered = new MethodExecuted();
+    Mockito.doAnswer(forceDisconnectTriggered).when(mockManager).forceDisconnect(any(String.class));
+    when(services.getManager()).thenReturn(mockManager);
+    prepareAndInstallView();
+
+    int viewId = 2;
+    List<InternalDistributedMember> mbrs = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> shutdowns = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> crashes = new LinkedList<InternalDistributedMember>();
+    mbrs.add(mockMembers[1]);
+    mbrs.add(mockMembers[2]);
+    mbrs.add(mockMembers[3]);
+   
+    //install the view
+    NetView netView = new NetView(mockMembers[0], viewId, mbrs, shutdowns, crashes);
+    InstallViewMessage installViewMessage = new InstallViewMessage(netView, credentials,
false);
+    gmsJoinLeave.processMessage(installViewMessage);
+    
+    Assert.assertNotEquals(netView, gmsJoinLeave.getView());
+    Assert.assertTrue("Expected to be forced disconnected", forceDisconnectTriggered.isMethodExecuted());
+  }
+  
+  public void testOlderPreparedViewBeforeFirstViewInstalled() throws IOException {
+    initMocks();
+    prepareView();
+    
+    int viewId = 0;
+    List<InternalDistributedMember> mbrs = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> shutdowns = new LinkedList<InternalDistributedMember>();
+    List<InternalDistributedMember> crashes = new LinkedList<InternalDistributedMember>();
+    mbrs.add(mockMembers[1]);
+    mbrs.add(mockMembers[2]);
+    mbrs.add(mockMembers[3]);
+   
+    //install the view
+    NetView netView = new NetView(mockMembers[0], viewId, mbrs, shutdowns, crashes);
+    InstallViewMessage installViewMessage = new InstallViewMessage(netView, credentials,
true);
+    gmsJoinLeave.processMessage(installViewMessage);
+    
+    Assert.assertNotEquals(netView, gmsJoinLeave.getView());
+  }
+  
+  private class MethodExecuted implements Answer {
+    private boolean methodExecuted = false;
+    @Override
+    public Object answer(InvocationOnMock invocation) {
+      //do we only expect a join response on a failure?
+      methodExecuted = true;
+      return null;
+    }
+    
+    public boolean isMethodExecuted() {
+      return methodExecuted;
+    }
+  } 
+}
+


Mime
View raw message