[jboss-cvs] JBossAS SVN: r76724 - in projects/cluster/ha-server-api/trunk: src/test/java/org/jboss/test/ha/framework/server and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Aug 6 15:06:01 EDT 2008


Author: pferraro
Date: 2008-08-06 15:06:01 -0400 (Wed, 06 Aug 2008)
New Revision: 76724

Added:
   projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HAServiceTestCase.java
   projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HASingletonTestCase.java
Modified:
   projects/cluster/ha-server-api/trunk/pom.xml
Log:
[JBCLUSTER-209] Added unit tests for HAService and HASingleton impls.
Added dependency on latest EasyMock.

Modified: projects/cluster/ha-server-api/trunk/pom.xml
===================================================================
--- projects/cluster/ha-server-api/trunk/pom.xml	2008-08-06 18:27:37 UTC (rev 76723)
+++ projects/cluster/ha-server-api/trunk/pom.xml	2008-08-06 19:06:01 UTC (rev 76724)
@@ -37,6 +37,7 @@
     <version.jboss.cache.pojo>2.2.0.CR5</version.jboss.cache.pojo>
     <version.jboss.aop>2.0.0.CR13</version.jboss.aop>
     <version.junit>3.8.1</version.junit>
+    <version.easymock>2.4</version.easymock>
   </properties>
   
   <build>
@@ -180,6 +181,12 @@
       <version>${version.junit}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <version>${version.easymock}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>  
 
 </project>

Added: projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HAServiceTestCase.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HAServiceTestCase.java	                        (rev 0)
+++ projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HAServiceTestCase.java	2008-08-06 19:06:01 UTC (rev 76724)
@@ -0,0 +1,238 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.ha.framework.server;
+
+import java.util.EventObject;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
+import org.jboss.ha.framework.interfaces.EventListener;
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.ha.framework.server.HAServiceEvent;
+import org.jboss.ha.framework.server.HAServiceEventFactory;
+import org.jboss.ha.framework.server.HAServiceImpl;
+import org.jboss.ha.framework.server.HAServiceRpcHandler;
+
+/**
+ * Unit test for {@link HAServiceImpl}.
+ * @author Paul Ferraro
+ */
+public class HAServiceTestCase extends TestCase
+{
+   protected static final String SERVICE_HA_NAME = "test";
+   
+   protected final HAPartition partition = EasyMock.createStrictMock(HAPartition.class);
+   protected final DistributedReplicantManager drm = EasyMock.createStrictMock(DistributedReplicantManager.class);
+   
+   private final HAServiceImpl<HAServiceEvent> customService = new HAServiceImpl<HAServiceEvent>(new HAServiceEventFactory())
+   {
+      @Override
+      protected void partitionTopologyChanged(List<?> newReplicants, int newReplicantsViewId, boolean merge)
+      {
+         newReplicants.clear();
+      }
+   };
+
+   private HAServiceImpl<HAServiceEvent> service = this.getHAService();
+   
+   /**
+    * @see junit.framework.TestCase#setUp()
+    */
+   @Override
+   protected void setUp() throws Exception
+   {
+      this.service = this.getHAService();
+      
+      this.service.setHAPartition(this.partition);
+      this.service.setServiceHAName(SERVICE_HA_NAME);
+   }
+   
+   protected Object getRpcHandler()
+   {
+      return EasyMock.isA(HAServiceRpcHandler.class);
+   }
+   
+   protected HAServiceImpl<HAServiceEvent> getHAService()
+   {
+      return this.customService;
+   }
+   
+   public void testStart() throws Exception
+   {
+      // Test isRegisterThreadContextClassLoader() = default value
+      this.partition.registerRPCHandler(EasyMock.same(SERVICE_HA_NAME), this.getRpcHandler());
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      this.drm.registerListener(SERVICE_HA_NAME, this.service);
+      this.drm.add(SERVICE_HA_NAME, "");
+      
+      EasyMock.replay(this.partition, this.drm);
+      
+      this.service.start();
+      
+      EasyMock.verify(this.partition, this.drm);
+      EasyMock.reset(this.partition, this.drm);
+      
+      // Test isRegisterThreadContextClassLoader() = true
+      this.service.setRegisterThreadContextClassLoader(true);
+      
+      this.partition.registerRPCHandler(EasyMock.same(SERVICE_HA_NAME), this.getRpcHandler(), EasyMock.same(Thread.currentThread().getContextClassLoader()));
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      this.drm.registerListener(SERVICE_HA_NAME, this.service);
+      this.drm.add(SERVICE_HA_NAME, "");
+      
+      EasyMock.replay(this.partition, this.drm);
+      
+      this.service.start();
+      
+      EasyMock.verify(this.partition, this.drm);
+      EasyMock.reset(this.partition, this.drm);
+   }
+   
+   public void testStop() throws Exception
+   {
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      this.drm.remove(SERVICE_HA_NAME);
+      this.drm.unregisterListener(SERVICE_HA_NAME, this.service);
+
+      this.partition.unregisterRPCHandler(EasyMock.same(SERVICE_HA_NAME), this.getRpcHandler());
+      
+      EasyMock.replay(this.partition, this.drm);
+      
+      this.service.stop();
+      
+      EasyMock.verify(this.partition, this.drm);
+      EasyMock.reset(this.partition, this.drm);
+   }
+   
+   public void testHAPartition()
+   {
+      HAPartition partition = this.service.getHAPartition();
+      
+      assert partition == this.partition;
+      
+      HAPartition partition2 = EasyMock.createMock(HAPartition.class);
+      
+      this.service.setHAPartition(partition2);
+      
+      partition = this.service.getHAPartition();
+      
+      assert partition == partition2;
+   }
+   
+   public void testHAServiceName()
+   {
+      String name = this.service.getServiceHAName();
+      
+      assert name == SERVICE_HA_NAME : name;
+      
+      final String newName = "new";
+      
+      this.service.setServiceHAName(newName);
+      
+      name = this.service.getServiceHAName();
+      
+      assert name == newName;
+   }
+   
+   public void testHandleEvent() throws Exception
+   {
+      HAServiceEvent event = new HAServiceEvent("", "");
+      
+      this.partition.callAsynchMethodOnCluster(EasyMock.same(SERVICE_HA_NAME), EasyMock.eq("handleEvent"), EasyMock.aryEq(new Object[] { event }), EasyMock.aryEq(new Class[] { EventObject.class }), EasyMock.eq(true));
+      
+      EasyMock.replay(this.partition);
+      
+      this.service.handleEvent(event);
+      
+      EasyMock.verify(this.partition);
+      EasyMock.reset(this.partition);
+   }
+   
+   public void testRegisterThreadContextClassLoader()
+   {
+      boolean register = this.service.isRegisterThreadContextClassLoader();
+      
+      assert !register;
+      
+      this.service.setRegisterThreadContextClassLoader(true);
+      
+      register = this.service.isRegisterThreadContextClassLoader();
+      
+      assert register;
+   }
+   
+   @SuppressWarnings("unchecked")
+   public void testNotifyListeners()
+   {
+      HAServiceEvent event = new HAServiceEvent("", "");
+      EventListener<HAServiceEvent> listener1 = EasyMock.createStrictMock(EventListener.class);
+      EventListener<HAServiceEvent> listener2 = EasyMock.createStrictMock(EventListener.class);
+      
+      try
+      {
+         // Verify that next listener is notified even if 1st throws exception
+         listener1.handleEvent(event);
+         EasyMock.expectLastCall().andThrow(new Exception());
+         
+         listener2.handleEvent(event);
+      }
+      catch (Exception e)
+      {
+         assert false;
+      }
+      
+      EasyMock.replay(listener1, listener2);
+      
+      this.service.addEventListener(listener1);
+      this.service.addEventListener(listener2);
+      this.service.notifyListeners(event);
+      
+      EasyMock.verify(listener1, listener2);
+      EasyMock.reset(listener1, listener2);
+   }
+   
+   public void testReplicantsChanged() throws Exception
+   {
+      List<?> list = EasyMock.createStrictMock(List.class);
+      
+      EasyMock.replay(list);
+      
+      this.service.replicantsChanged("", list, 100, false);
+      
+      EasyMock.verify(list);
+      EasyMock.reset(list);
+      
+      list.clear();
+      
+      EasyMock.replay(list);
+      
+      this.service.replicantsChanged(SERVICE_HA_NAME, list, 101, false);
+      
+      EasyMock.verify(list);
+      EasyMock.reset(list);
+   }
+}

Added: projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HASingletonTestCase.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HASingletonTestCase.java	                        (rev 0)
+++ projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/HASingletonTestCase.java	2008-08-06 19:06:01 UTC (rev 76724)
@@ -0,0 +1,464 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.ha.framework.server;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.easymock.EasyMock;
+import org.jboss.ha.framework.interfaces.ClusterNode;
+import org.jboss.ha.framework.interfaces.HASingletonElectionPolicy;
+import org.jboss.ha.framework.interfaces.HASingletonLifecycle;
+import org.jboss.ha.framework.server.HAServiceEvent;
+import org.jboss.ha.framework.server.HAServiceEventFactory;
+import org.jboss.ha.framework.server.HAServiceImpl;
+import org.jboss.ha.framework.server.HASingletonImpl;
+import org.jboss.ha.framework.server.HASingletonRpcHandler;
+
+/**
+ * Unit test for {@link HASingletonImpl}.
+ * @author Paul Ferraro
+ */
+public class HASingletonTestCase extends HAServiceTestCase
+{
+   private static final String SERVICE_HA_NAME = "test";
+   
+   private final HASingletonElectionPolicy electionPolicy = EasyMock.createStrictMock(HASingletonElectionPolicy.class);
+   final HASingletonLifecycle lifecycle = EasyMock.createStrictMock(HASingletonLifecycle.class);
+   
+   private final HASingletonImpl<HAServiceEvent> customSingleton = new HASingletonImpl<HAServiceEvent>(new HAServiceEventFactory())
+   {
+      public void startSingleton()
+      {
+         HASingletonTestCase.this.lifecycle.startSingleton();
+      }
+      
+      public void stopSingleton()
+      {
+         HASingletonTestCase.this.lifecycle.stopSingleton();
+      }
+   };
+
+   private HASingletonImpl<HAServiceEvent> singleton;
+   
+   /**
+    * @see org.jboss.test.ha.framework.server.HAServiceTestCase#setUp()
+    */
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      
+      this.singleton = this.getHASingleton();
+   }
+
+   protected HASingletonImpl<HAServiceEvent> getHASingleton()
+   {
+      return this.customSingleton;
+   }
+   
+   @Override
+   protected HAServiceImpl<HAServiceEvent> getHAService()
+   {
+      return this.customSingleton;
+   }
+
+   @Override
+   protected Object getRpcHandler()
+   {
+      return EasyMock.isA(HASingletonRpcHandler.class);
+   }
+   
+   public void testRestartOnMerge()
+   {
+      boolean restart = this.singleton.getRestartOnMerge();
+      
+      assert restart;
+      
+      this.singleton.setRestartOnMerge(false);
+      
+      restart = this.singleton.getRestartOnMerge();
+      
+      assert !restart;
+   }
+   
+   public void testElectionPolicy()
+   {
+      HASingletonElectionPolicy policy = this.singleton.getElectionPolicy();
+      
+      assert policy == null;
+      
+      this.singleton.setElectionPolicy(this.electionPolicy);
+      
+      policy = this.singleton.getElectionPolicy();
+      
+      assert policy == this.electionPolicy;
+   }
+   
+   public void testIsMaster()
+   {
+      boolean master = this.singleton.isMasterNode();
+      
+      assert !master;
+   }
+   
+   public void testStopOldMaster() throws Exception
+   {
+      this.partition.callAsynchMethodOnCluster(EasyMock.same(SERVICE_HA_NAME), EasyMock.eq("stopOldMaster"), EasyMock.aryEq(new Object[0]), EasyMock.aryEq(new Class[0]), EasyMock.eq(true));
+      
+      EasyMock.replay(this.partition);
+      
+      this.singleton.stopOldMaster();
+      
+      EasyMock.verify(this.partition);
+      EasyMock.reset(this.partition);
+   }
+   
+   @Override
+   public void testReplicantsChanged() throws Exception
+   {
+      List<?> list = Collections.emptyList();
+      
+      // Test non-matching service name
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged("", list, 100, false);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test no election policy, was not old master, is not new master
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(false);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test no election policy, was not old master, is new master
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(true);
+      
+      this.partition.callAsynchMethodOnCluster(EasyMock.same(SERVICE_HA_NAME), EasyMock.eq("stopOldMaster"), EasyMock.aryEq(new Object[0]), EasyMock.aryEq(new Class[0]), EasyMock.eq(true));
+      this.lifecycle.startSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+
+      
+      // Test no election policy, was old master, is new master, restart on merge, not merged
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(true);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, false);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+
+      
+      // Test no election policy, was old master, is new master, restart on merge, merged
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(true);
+
+      this.lifecycle.stopSingleton();
+      this.lifecycle.startSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+
+      
+      // Test no election policy, was old master, is new master, no restart on merge
+      
+      this.singleton.setRestartOnMerge(false);
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(true);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test no election policy, was old master, not new master
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.isMasterReplica(SERVICE_HA_NAME)).andReturn(false);
+
+      this.lifecycle.stopSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, false);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      // Set election policy
+      this.singleton.setElectionPolicy(this.electionPolicy);
+
+      
+      // Test null replicants, was not old master, is not new master
+      
+      List<ClusterNode> nodes = null;
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test empty replicants, was not old master, is not new master
+      
+      nodes = Collections.emptyList();
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test one replicant, was not old master, is not new master
+      
+      ClusterNode ourNode = EasyMock.createMock("ours", ClusterNode.class);
+      ClusterNode otherNode = EasyMock.createMock("other", ClusterNode.class);
+      
+      nodes = Collections.singletonList(otherNode);
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle);
+      
+      
+      // Test many replicants, was not old master, is not new master
+      nodes = Arrays.asList(ourNode, otherNode);
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(otherNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      
+      // Test many replicants, was not old master, is new master
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(ourNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      this.partition.callAsynchMethodOnCluster(EasyMock.same(SERVICE_HA_NAME), EasyMock.eq("stopOldMaster"), EasyMock.aryEq(new Object[0]), EasyMock.aryEq(new Class[0]), EasyMock.eq(true));
+      this.lifecycle.startSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+
+      
+      // Test many replicants, was old master, is new master, no restart on merge
+      
+      this.singleton.setRestartOnMerge(false);
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(ourNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      
+      // Test one replicant, was old master, is new master, no restart on merge
+      
+      nodes = Collections.singletonList(ourNode);
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      
+      // Test many replicants, was old master, is new master, restart on merge, not merged
+      
+      nodes = Arrays.asList(ourNode, otherNode);
+      
+      this.singleton.setRestartOnMerge(true);
+
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(ourNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, false);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+
+      
+      // Test many replicants, was old master, is new master, restart on merge, merged
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(ourNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+
+      System.out.println("restart on merge = " + this.singleton.getRestartOnMerge());
+      this.lifecycle.stopSingleton();
+      this.lifecycle.startSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+
+      
+      // Test many replicants, was old master, not new master
+      
+      EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(this.drm);
+      EasyMock.expect(this.drm.lookupReplicantsNodes(SERVICE_HA_NAME)).andReturn(nodes);
+      EasyMock.expect(this.electionPolicy.elect(nodes)).andReturn(otherNode);
+      EasyMock.expect(this.partition.getClusterNode()).andReturn(ourNode);
+
+      this.lifecycle.stopSingleton();
+      
+      EasyMock.replay(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      this.singleton.replicantsChanged(SERVICE_HA_NAME, list, 101, true);
+      
+      EasyMock.verify(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+      
+      assert !this.singleton.isMasterNode();
+      
+      EasyMock.reset(this.partition, this.drm, this.lifecycle, this.electionPolicy);
+   }
+}




More information about the jboss-cvs-commits mailing list