Author: pferraro
Date: 2008-09-09 16:30:20 -0400 (Tue, 09 Sep 2008)
New Revision: 1790
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ModClusterServiceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ha/HASingletonAwareResetRequestSourceTestCase.java
Log:
Unit test
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ModClusterServiceTestCase.java
===================================================================
--- trunk/mod_cluster/src/test/java/org/jboss/modcluster/ModClusterServiceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ModClusterServiceTestCase.java 2008-09-09
20:30:20 UTC (rev 1790)
@@ -0,0 +1,544 @@
+/*
+ * 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.modcluster;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
+import org.apache.catalina.connector.Connector;
+import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.jboss.ha.framework.interfaces.ClusterNode;
+import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.ha.framework.interfaces.HASingletonElectionPolicy;
+import org.jboss.modcluster.config.BalancerConfiguration;
+import org.jboss.modcluster.config.MCMPHandlerConfiguration;
+import org.jboss.modcluster.config.NodeConfiguration;
+import org.jboss.modcluster.ha.ClusteredMCMPHandler;
+import org.jboss.modcluster.ha.HASingletonAwareResetRequestSource;
+import org.jboss.modcluster.ha.ModClusterServiceDRMEntry;
+import org.jboss.modcluster.ha.rpc.MCMPServerDiscoveryEvent;
+import org.jboss.modcluster.ha.rpc.ModClusterServiceStateGroupRpcResponse;
+import org.jboss.modcluster.ha.rpc.PeerMCMPDiscoveryStatus;
+import org.jboss.modcluster.load.LoadBalanceFactorProvider;
+import org.jboss.modcluster.mcmp.AddressPort;
+import org.jboss.modcluster.mcmp.MCMPHandler;
+import org.jboss.modcluster.mcmp.MCMPRequest;
+import org.jboss.modcluster.mcmp.MCMPRequestType;
+import org.jboss.modcluster.mcmp.MCMPServerState;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class ModClusterServiceTestCase extends TestCase
+{
+ private HAPartition partition = EasyMock.createStrictMock(HAPartition.class);
+ private NodeConfiguration nodeConfig = EasyMock.createMock(NodeConfiguration.class);
+ private BalancerConfiguration balancerConfig =
EasyMock.createMock(BalancerConfiguration.class);
+ private MCMPHandlerConfiguration mcmpHandlerConfig =
EasyMock.createMock(MCMPHandlerConfiguration.class);
+ private MCMPHandler mcmpHandler = EasyMock.createStrictMock(MCMPHandler.class);
+ private HASingletonAwareResetRequestSource resetRequestSource =
EasyMock.createStrictMock(HASingletonAwareResetRequestSource.class);
+ private ClusteredMCMPHandler clusteredMCMPHandler =
EasyMock.createStrictMock(ClusteredMCMPHandler.class);
+ private LoadBalanceFactorProvider lbfProvider =
EasyMock.createStrictMock(LoadBalanceFactorProvider.class);
+ private JBossWebEventHandler eventHandler =
EasyMock.createStrictMock(JBossWebEventHandler.class);
+ private HASingletonElectionPolicy electionPolicy =
EasyMock.createStrictMock(HASingletonElectionPolicy.class);
+ private ClusterNode node = EasyMock.createStrictMock(ClusterNode.class);
+
+ private static final boolean MASTER_PER_DOMAIN = true;
+ private static final String SERVICE_HA_NAME = "myservice";
+ private static final String DOMAIN = "domain";
+
+ private ModClusterService modClusterService;
+
+ /**
+ * @{inheritDoc}
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception
+ {
+
EasyMock.expect(this.mcmpHandlerConfig.isMasterPerDomain()).andReturn(MASTER_PER_DOMAIN);
+ EasyMock.expect(this.nodeConfig.getDomain()).andReturn(DOMAIN);
+
+ EasyMock.expect(this.partition.getClusterNode()).andReturn(this.node).times(2);
+
+ EasyMock.replay(this.mcmpHandlerConfig, this.nodeConfig, this.partition);
+
+ this.modClusterService = new ModClusterService(this.partition, this.nodeConfig,
this.balancerConfig, this.mcmpHandlerConfig, this.mcmpHandler, this.resetRequestSource,
this.clusteredMCMPHandler, this.lbfProvider, this.eventHandler, this.electionPolicy);
+ this.modClusterService.setServiceHAName(SERVICE_HA_NAME);
+
+ EasyMock.verify(this.mcmpHandlerConfig, this.nodeConfig, this.partition);
+ EasyMock.reset(this.mcmpHandlerConfig, this.nodeConfig, this.partition);
+ }
+
+ public void testAddProxy()
+ {
+ String host = "172.0.0.1";
+ int port = 0;
+
+ this.clusteredMCMPHandler.addProxy(host, port);
+
+ EasyMock.replay(this.clusteredMCMPHandler);
+
+ this.modClusterService.addProxy(host, port);
+
+ EasyMock.verify(this.clusteredMCMPHandler);
+ EasyMock.reset(this.clusteredMCMPHandler);
+ }
+
+ public void testRemoveProxy()
+ {
+ String host = "172.0.0.1";
+ int port = 0;
+
+ this.clusteredMCMPHandler.removeProxy(host, port);
+
+ EasyMock.replay(this.clusteredMCMPHandler);
+
+ this.modClusterService.removeProxy(host, port);
+
+ EasyMock.verify(this.clusteredMCMPHandler);
+ EasyMock.reset(this.clusteredMCMPHandler);
+ }
+
+ public void testGetProxyConfiguration()
+ {
+
EasyMock.expect(this.clusteredMCMPHandler.getProxyConfiguration()).andReturn("configuration");
+
+ EasyMock.replay(this.clusteredMCMPHandler);
+
+ String result = this.modClusterService.getProxyConfiguration();
+
+ EasyMock.verify(this.clusteredMCMPHandler);
+
+ assertEquals("configuration", result);
+
+ EasyMock.reset(this.clusteredMCMPHandler);
+ }
+
+ public void testReset()
+ {
+ this.clusteredMCMPHandler.reset();
+
+ EasyMock.replay(this.clusteredMCMPHandler);
+
+ this.modClusterService.reset();
+
+ EasyMock.verify(this.clusteredMCMPHandler);
+ EasyMock.reset(this.clusteredMCMPHandler);
+ }
+
+ public void testRefresh()
+ {
+ this.clusteredMCMPHandler.markProxiesInError();
+
+ EasyMock.replay(this.clusteredMCMPHandler);
+
+ this.modClusterService.refresh();
+
+ EasyMock.verify(this.clusteredMCMPHandler);
+ EasyMock.reset(this.clusteredMCMPHandler);
+ }
+
+ public void testInit()
+ {
+ this.eventHandler.init();
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.init();
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testStartServer()
+ {
+ Server server = EasyMock.createMock(Server.class);
+
+ this.resetRequestSource.setJbossWebServer(server);
+ this.eventHandler.startServer(server);
+
+ EasyMock.replay(this.resetRequestSource, this.eventHandler);
+
+ this.modClusterService.startServer(server);
+
+ EasyMock.verify(this.resetRequestSource, this.eventHandler);
+ EasyMock.reset(this.resetRequestSource, this.eventHandler);
+ }
+
+ public void testStopServer()
+ {
+ Server server = EasyMock.createStrictMock(Server.class);
+
+ this.eventHandler.stopServer(server);
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.stopServer(server);
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testConfig() throws Exception
+ {
+ ModClusterServiceDRMEntry drmEntry =
this.verifyConfig(EasyMock.createStrictMock(Engine.class), "host1");
+
+ assertEquals(Collections.singleton("host1"), drmEntry.getJvmRoutes());
+ assertNull(drmEntry.getMCMPServerStates());
+ assertSame(this.node, drmEntry.getPeer());
+ }
+
+ private ModClusterServiceDRMEntry verifyConfig(Engine engine, String host) throws
Exception
+ {
+ Service service = EasyMock.createStrictMock(Service.class);
+ DistributedReplicantManager drm =
EasyMock.createStrictMock(DistributedReplicantManager.class);
+ Capture<ModClusterServiceDRMEntry> capturedDRMEntry = new
Capture<ModClusterServiceDRMEntry>();
+ Connector connector = new Connector("AJP/1.3");
+
+ EasyMock.expect(engine.getService()).andReturn(service);
+ EasyMock.expect(service.findConnectors()).andReturn(new Connector[] { connector
});
+ EasyMock.expect(engine.getJvmRoute()).andReturn(null);
+ Set<MCMPServerState> states = Collections.emptySet();
+ EasyMock.expect(this.clusteredMCMPHandler.getProxyStates()).andReturn(states);
+
+ EasyMock.expect(engine.getJvmRoute()).andReturn(host);
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+ drm.add(EasyMock.eq(SERVICE_HA_NAME + ":" + DOMAIN),
EasyMock.capture(capturedDRMEntry));
+ this.eventHandler.config(engine);
+
+ EasyMock.replay(engine, service, drm, this.clusteredMCMPHandler, this.partition,
this.eventHandler);
+
+ this.modClusterService.config(engine);
+
+ EasyMock.verify(engine, service, drm, this.clusteredMCMPHandler, this.partition,
this.eventHandler);
+ EasyMock.reset(engine, service, drm, this.clusteredMCMPHandler, this.partition,
this.eventHandler);
+
+ return capturedDRMEntry.getValue();
+ }
+
+ public void testRemoveAll() throws Exception
+ {
+ Engine engine = EasyMock.createStrictMock(Engine.class);
+
+ // First config
+ ModClusterServiceDRMEntry drmEntry = this.verifyConfig(engine, "host1");
+
+ assertEquals(Collections.singleton("host1"), drmEntry.getJvmRoutes());
+ assertNull(drmEntry.getMCMPServerStates());
+ assertSame(this.node, drmEntry.getPeer());
+
+ Capture<ModClusterServiceDRMEntry> capturedDRMEntry = new
Capture<ModClusterServiceDRMEntry>();
+ DistributedReplicantManager drm =
EasyMock.createStrictMock(DistributedReplicantManager.class);
+
+ this.eventHandler.removeAll(engine);
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host1");
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+ drm.add(EasyMock.eq(SERVICE_HA_NAME + ":" + DOMAIN),
EasyMock.capture(capturedDRMEntry));
+
+ EasyMock.replay(drm, this.eventHandler, engine, this.partition);
+
+ this.modClusterService.removeAll(engine);
+
+ EasyMock.verify(drm, this.eventHandler, engine, this.partition);
+
+ drmEntry = capturedDRMEntry.getValue();
+
+ assertEquals(Collections.emptySet(), drmEntry.getJvmRoutes());
+ assertNull(drmEntry.getMCMPServerStates());
+ assertSame(this.node, drmEntry.getPeer());
+
+ EasyMock.reset(drm, this.eventHandler, engine, this.partition);
+ }
+
+ public void testAddContext()
+ {
+ Context context = EasyMock.createMock(Context.class);
+
+ this.eventHandler.addContext(context);
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.addContext(context);
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testStartContext()
+ {
+ Context context = EasyMock.createMock(Context.class);
+
+ this.eventHandler.startContext(context);
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.startContext(context);
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testStopContext()
+ {
+ Context context = EasyMock.createMock(Context.class);
+
+ this.eventHandler.stopContext(context);
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.stopContext(context);
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testRemoveContext()
+ {
+ Context context = EasyMock.createMock(Context.class);
+
+ this.eventHandler.removeContext(context);
+
+ EasyMock.replay(this.eventHandler);
+
+ this.modClusterService.removeContext(context);
+
+ EasyMock.verify(this.eventHandler);
+ EasyMock.reset(this.eventHandler);
+ }
+
+ public void testStatus() throws Exception
+ {
+ Engine engine = EasyMock.createMock(Engine.class);
+
+ // Test non-master status
+ EasyMock.expect(this.lbfProvider.getLoadBalanceFactor()).andReturn(10);
+
+ EasyMock.replay(this.lbfProvider);
+
+ this.modClusterService.status(engine);
+
+ EasyMock.verify(this.lbfProvider);
+ EasyMock.reset(this.lbfProvider);
+
+
+ // Make master
+ String key = SERVICE_HA_NAME + ":" + DOMAIN;
+ DistributedReplicantManager drm =
EasyMock.createStrictMock(DistributedReplicantManager.class);
+
+ this.modClusterService.setElectionPolicy(null);
+
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+ EasyMock.expect(drm.isMasterReplica(key)).andReturn(true);
+
+ this.partition.callAsynchMethodOnCluster(EasyMock.eq("myservice"),
EasyMock.eq("stopOldMaster"), EasyMock.aryEq(new Object[0]), EasyMock.aryEq(new
Class[0]), EasyMock.eq(true));
+
+ EasyMock.replay(this.partition, drm);
+
+ this.modClusterService.replicantsChanged(key, Collections.EMPTY_LIST, 1, false);
+
+ EasyMock.verify(this.partition, drm);
+
+ assertTrue(this.modClusterService.isMasterNode());
+
+ EasyMock.reset(this.partition, drm);
+
+ // Create drm entries
+ ClusterNode remoteNode1 = EasyMock.createMock(ClusterNode.class);
+ MCMPServerState remoteState1 = EasyMock.createMock(MCMPServerState.class);
+
+ EasyMock.expect(remoteState1.getState()).andReturn(MCMPServerState.State.OK);
+ EasyMock.expect(remoteState1.isEstablished()).andReturn(true);
+
+ EasyMock.replay(remoteState1);
+
+ ModClusterServiceDRMEntry drmEntry1 = new ModClusterServiceDRMEntry(remoteNode1,
Collections.singleton(remoteState1));
+ drmEntry1.addJvmRoute("host1");
+
+ EasyMock.verify(remoteState1);
+ EasyMock.reset(remoteState1);
+
+
+ MCMPServerState remoteState2 = EasyMock.createMock(MCMPServerState.class);
+ ClusterNode remoteNode2 = EasyMock.createMock(ClusterNode.class);
+
+ EasyMock.expect(remoteState2.getState()).andReturn(MCMPServerState.State.DOWN);
+ EasyMock.expect(remoteState2.isEstablished()).andReturn(false);
+
+ EasyMock.replay(remoteState2);
+
+ ModClusterServiceDRMEntry drmEntry2 = new ModClusterServiceDRMEntry(remoteNode2,
Collections.singleton(remoteState2));
+ drmEntry2.addJvmRoute("host2");
+
+ EasyMock.verify(remoteState2);
+ EasyMock.reset(remoteState2);
+
+ // Test master status
+ MCMPServerState localState = EasyMock.createMock(MCMPServerState.class);
+ ModClusterServiceDRMEntry drmEntry = new ModClusterServiceDRMEntry(this.node,
null);
+ InetAddress address1 = InetAddress.getByName("172.0.0.1");
+ InetAddress address2 = InetAddress.getByName("172.0.1.1");
+ MCMPServerDiscoveryEvent event1 = new MCMPServerDiscoveryEvent(remoteNode1, new
AddressPort(address1, 1), true, 1);
+ MCMPServerDiscoveryEvent event2 = new MCMPServerDiscoveryEvent(remoteNode2, new
AddressPort(address2, 2), false, 2);
+
+ MCMPRequest request1 = new MCMPRequest(MCMPRequestType.ENABLE_APP, false, new
TreeMap<String, String>());
+ MCMPRequest request2 = new MCMPRequest(MCMPRequestType.DISABLE_APP, false, new
TreeMap<String, String>());
+ Capture<List<MCMPRequest>> capturedRequests = new
Capture<List<MCMPRequest>>();
+ Capture<ModClusterServiceDRMEntry> capturedEntry = new
Capture<ModClusterServiceDRMEntry>();
+ Capture<Object[]> capturedArgs = new Capture<Object[]>();
+
+ ModClusterServiceStateGroupRpcResponse response1 = new
ModClusterServiceStateGroupRpcResponse(remoteNode1, 10, new
TreeSet<MCMPServerState>(), Collections.singletonList(event1), new
ArrayList<MCMPRequest>());
+ ModClusterServiceStateGroupRpcResponse response2 = new
ModClusterServiceStateGroupRpcResponse(remoteNode2, 20, new
TreeSet<MCMPServerState>(), Collections.singletonList(event2), new
ArrayList<MCMPRequest>());
+
+ EasyMock.expect(this.lbfProvider.getLoadBalanceFactor()).andReturn(10);
+
+ this.mcmpHandler.status();
+
+ EasyMock.expect(this.mcmpHandler.getProxyStates()).andReturn(new
TreeSet<MCMPServerState>());
+
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+
EasyMock.expect(drm.lookupReplicants(key)).andReturn(Collections.singletonList(drmEntry));
+ EasyMock.expect(this.partition.getClusterNode()).andReturn(this.node);
+ EasyMock.expect(this.partition.callMethodOnCluster(EasyMock.eq(SERVICE_HA_NAME),
EasyMock.eq("getClusterCoordinatorState"), EasyMock.aryEq(new Object[] { new
TreeSet<MCMPServerState>() }), EasyMock.aryEq(new Class[] { Set.class }),
EasyMock.eq(true))).andReturn(new ArrayList<Object>(Arrays.asList(response1,
response2)));
+
+ // Process discovery events
+ this.mcmpHandler.addProxy(address1, 1);
+ this.mcmpHandler.removeProxy(address2, 2);
+
+ // Start over - this time with no discovery events
+ response1 = new ModClusterServiceStateGroupRpcResponse(remoteNode1, 10,
Collections.singleton(remoteState1), new ArrayList<MCMPServerDiscoveryEvent>(),
Collections.singletonList(request1));
+ response2 = new ModClusterServiceStateGroupRpcResponse(remoteNode2, 20,
Collections.singleton(remoteState2), new ArrayList<MCMPServerDiscoveryEvent>(),
Collections.singletonList(request2));
+
+ this.mcmpHandler.status();
+
+ Set<MCMPServerState> states = new
LinkedHashSet<MCMPServerState>(Arrays.asList(remoteState1, remoteState2));
+
+ EasyMock.expect(this.mcmpHandler.getProxyStates()).andReturn(states);
+
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+ EasyMock.expect(drm.lookupReplicants(key)).andReturn(Arrays.asList(drmEntry1,
drmEntry2));
+ EasyMock.expect(this.partition.getClusterNode()).andReturn(this.node);
+ EasyMock.expect(this.partition.callMethodOnCluster(EasyMock.eq(SERVICE_HA_NAME),
EasyMock.eq("getClusterCoordinatorState"), EasyMock.aryEq(new Object[] { states
}), EasyMock.aryEq(new Class[] { Set.class }), EasyMock.eq(true))).andReturn(new
ArrayList<Object>(Arrays.asList(response1, response2)));
+
+ EasyMock.expect(remoteState1.getState()).andReturn(MCMPServerState.State.OK);
+ EasyMock.expect(remoteState1.isEstablished()).andReturn(true);
+ EasyMock.expect(remoteState2.getState()).andReturn(MCMPServerState.State.DOWN);
+ EasyMock.expect(remoteState2.isEstablished()).andReturn(false);
+
+ this.mcmpHandler.sendRequests(Arrays.asList(request1, request2));
+ this.mcmpHandler.sendRequests(EasyMock.capture(capturedRequests));
+
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+ EasyMock.expect(drm.lookupLocalReplicant(key)).andReturn(drmEntry);
+
+ EasyMock.expect(this.partition.getClusterNode()).andReturn(this.node);
+
+ EasyMock.expect(remoteState1.getState()).andReturn(MCMPServerState.State.OK);
+ EasyMock.expect(remoteState1.isEstablished()).andReturn(true);
+ EasyMock.expect(remoteState2.getState()).andReturn(MCMPServerState.State.DOWN);
+ EasyMock.expect(remoteState2.isEstablished()).andReturn(false);
+
+ EasyMock.expect(this.partition.getDistributedReplicantManager()).andReturn(drm);
+
+ drm.add(EasyMock.eq(key), EasyMock.capture(capturedEntry));
+
+
EasyMock.expect(this.partition.callMethodOnCluster(EasyMock.eq("myservice"),
EasyMock.eq("clusterStatusComplete"), EasyMock.capture(capturedArgs),
EasyMock.aryEq(new Class[] { Map.class }), EasyMock.eq(true))).andReturn(null);
+
+ EasyMock.replay(this.lbfProvider, this.mcmpHandler, this.partition, drm,
localState, this.node, remoteState1, remoteState2, remoteNode1, remoteNode2);
+
+ this.modClusterService.status(engine);
+
+ EasyMock.verify(this.lbfProvider, this.mcmpHandler, this.partition, drm,
localState, this.node, remoteState1, remoteState2, remoteNode1, remoteNode2);
+
+ List<MCMPRequest> requests = capturedRequests.getValue();
+
+ assertEquals(2, requests.size());
+
+ assertSame(MCMPRequestType.STATUS, requests.get(0).getRequestType());
+ assertFalse(requests.get(0).isWildcard());
+ assertEquals(2, requests.get(0).getParameters().size());
+ assertEquals("host2",
requests.get(0).getParameters().get("JVMRoute"));
+ assertEquals("20",
requests.get(0).getParameters().get("Load"));
+
+ assertSame(MCMPRequestType.STATUS, requests.get(1).getRequestType());
+ assertFalse(requests.get(1).isWildcard());
+ assertEquals(2, requests.get(1).getParameters().size());
+ assertEquals("host1",
requests.get(1).getParameters().get("JVMRoute"));
+ assertEquals("10",
requests.get(1).getParameters().get("Load"));
+
+ ModClusterServiceDRMEntry entry = capturedEntry.getValue();
+
+ assertSame(this.node, entry.getPeer());
+ assertEquals(states, entry.getMCMPServerStates());
+ assertTrue(entry.getJvmRoutes().isEmpty());
+
+ Object[] args = capturedArgs.getValue();
+ assertEquals(1, args.length);
+ assertTrue(args[0] instanceof Map);
+
+ Map<ClusterNode, PeerMCMPDiscoveryStatus> map = (Map) args[0];
+
+ assertEquals(2, map.size());
+ assertTrue(map.containsKey(remoteNode1));
+ assertTrue(map.containsKey(remoteNode2));
+
+ PeerMCMPDiscoveryStatus status1 = map.get(remoteNode1);
+
+ assertSame(remoteNode1, status1.getPeer());
+ assertEquals(Collections.singleton(remoteState1), status1.getMCMPServerStates());
+ assertTrue(status1.getJvmRoutes().isEmpty());
+ assertNull(status1.getLatestDiscoveryEvent());
+
+ EasyMock.reset(this.lbfProvider, this.mcmpHandler, this.partition, drm, localState,
this.node, remoteState1, remoteState2, remoteNode1, remoteNode2);
+
+
+ // Test master status, but off-frequency
+ this.modClusterService.setProcessStatusFrequency(2);
+
+ EasyMock.expect(this.lbfProvider.getLoadBalanceFactor()).andReturn(10);
+
+ EasyMock.replay(this.lbfProvider);
+
+ this.modClusterService.status(engine);
+
+ EasyMock.verify(this.lbfProvider);
+ EasyMock.reset(this.lbfProvider);
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ha/HASingletonAwareResetRequestSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ha/HASingletonAwareResetRequestSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/ha/HASingletonAwareResetRequestSourceTestCase.java 2008-09-09
20:30:20 UTC (rev 1790)
@@ -0,0 +1,312 @@
+/*
+ * 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.modcluster.ha;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Container;
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
+import org.apache.catalina.connector.Connector;
+import org.easymock.EasyMock;
+import org.jboss.ha.framework.interfaces.ClusterNode;
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.ha.framework.interfaces.HAServiceKeyProvider;
+import org.jboss.ha.framework.interfaces.HASingletonMBean;
+import org.jboss.modcluster.config.BalancerConfiguration;
+import org.jboss.modcluster.config.NodeConfiguration;
+import org.jboss.modcluster.ha.rpc.ResetRequestGroupRpcResponse;
+import org.jboss.modcluster.mcmp.MCMPRequest;
+import org.jboss.modcluster.mcmp.MCMPRequestType;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class HASingletonAwareResetRequestSourceTestCase extends TestCase
+{
+ private NodeConfiguration nodeConfig =
EasyMock.createStrictMock(NodeConfiguration.class);
+ private BalancerConfiguration balancerConfig =
EasyMock.createStrictMock(BalancerConfiguration.class);
+ private HAServiceKeyProvider key =
EasyMock.createStrictMock(HAServiceKeyProvider.class);
+ private HASingletonMBean singleton =
EasyMock.createStrictMock(HASingletonMBean.class);
+ private HAPartition partition = EasyMock.createStrictMock(HAPartition.class);
+
+ private HASingletonAwareResetRequestSource source = new
HASingletonAwareResetRequestSourceImpl(this.nodeConfig, this.balancerConfig,
this.singleton, this.key);
+
+ public void testGetResetRequests() throws Exception
+ {
+ MCMPRequest request1 = new MCMPRequest(MCMPRequestType.ENABLE_APP, false,
Collections.singletonMap("JVMRoute", "host1"));
+ MCMPRequest request2 = new MCMPRequest(MCMPRequestType.REMOVE_APP, false,
Collections.singletonMap("JVMRoute", "host2"));
+
+ ResetRequestGroupRpcResponse response1 = new
ResetRequestGroupRpcResponse(EasyMock.createMock(ClusterNode.class),
Collections.singletonList(request1));
+ ResetRequestGroupRpcResponse response2 = new
ResetRequestGroupRpcResponse(EasyMock.createMock(ClusterNode.class),
Collections.singletonList(request2));
+
+ // Test master - w/out server
+ EasyMock.expect(this.singleton.isMasterNode()).andReturn(true);
+
+ EasyMock.expect(this.key.getHAPartition()).andReturn(this.partition);
+ EasyMock.expect(this.key.getHAServiceKey()).andReturn("service:domain");
+
EasyMock.expect(this.partition.callMethodOnCluster(EasyMock.eq("service:domain"),
EasyMock.eq("getResetRequests"), EasyMock.aryEq(new Object[0]),
EasyMock.aryEq(new Class[0]), EasyMock.eq(true))).andReturn(new
ArrayList<ResetRequestGroupRpcResponse>(Arrays.asList(response1, response2)));
+
+ EasyMock.replay(this.singleton, this.key, this.partition);
+
+ List<MCMPRequest> results = this.source.getResetRequests();
+
+ EasyMock.verify(this.singleton, this.key, this.partition);
+
+ assertEquals(2, results.size());
+ assertSame(request1, results.get(0));
+ assertSame(request2, results.get(1));
+
+ EasyMock.reset(this.singleton, this.key, this.partition);
+
+
+ // Test master - w/server
+ Server server = EasyMock.createStrictMock(Server.class);
+ Service service = EasyMock.createStrictMock(Service.class);
+ Engine engine = EasyMock.createStrictMock(Engine.class);
+ Container container = EasyMock.createStrictMock(Container.class);
+ Context context = EasyMock.createStrictMock(Context.class);
+ Host host = EasyMock.createStrictMock(Host.class);
+ Connector connector = new Connector("AJP/1.3");
+
+ this.source.setJbossWebServer(server);
+
+ EasyMock.expect(this.singleton.isMasterNode()).andReturn(true);
+
+ EasyMock.expect(this.key.getHAPartition()).andReturn(this.partition);
+ EasyMock.expect(this.key.getHAServiceKey()).andReturn("service:domain");
+
EasyMock.expect(this.partition.callMethodOnCluster(EasyMock.eq("service:domain"),
EasyMock.eq("getResetRequests"), EasyMock.aryEq(new Object[0]),
EasyMock.aryEq(new Class[0]), EasyMock.eq(true))).andReturn(new
ArrayList<ResetRequestGroupRpcResponse>(Arrays.asList(response1, response2)));
+
+ EasyMock.expect(server.findServices()).andReturn(new Service[] { service });
+ EasyMock.expect(service.getContainer()).andReturn(engine);
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host").times(2);
+
+ EasyMock.expect(engine.getService()).andReturn(service);
+ EasyMock.expect(service.findConnectors()).andReturn(new Connector[] { connector
});
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host1");
+
+ EasyMock.expect(this.nodeConfig.getDomain()).andReturn("domain");
+ EasyMock.expect(this.nodeConfig.getFlushPackets()).andReturn(Boolean.TRUE);
+ EasyMock.expect(this.nodeConfig.getFlushWait()).andReturn(1);
+ EasyMock.expect(this.nodeConfig.getPing()).andReturn(2);
+ EasyMock.expect(this.nodeConfig.getSmax()).andReturn(3);
+ EasyMock.expect(this.nodeConfig.getTtl()).andReturn(4);
+ EasyMock.expect(this.nodeConfig.getNodeTimeout()).andReturn(5);
+ EasyMock.expect(this.nodeConfig.getBalancer()).andReturn("S");
+
+ EasyMock.expect(this.balancerConfig.getStickySession()).andReturn(Boolean.FALSE);
+
EasyMock.expect(this.balancerConfig.getStickySessionRemove()).andReturn(Boolean.TRUE);
+
EasyMock.expect(this.balancerConfig.getStickySessionForce()).andReturn(Boolean.FALSE);
+ EasyMock.expect(this.balancerConfig.getWorkerTimeout()).andReturn(6);
+ EasyMock.expect(this.balancerConfig.getMaxAttempts()).andReturn(7);
+
+ EasyMock.expect(engine.findChildren()).andReturn(new Container[] { container });
+ EasyMock.expect(container.findChildren()).andReturn(new Container[] { context });
+ EasyMock.expect(context.isStarted()).andReturn(true);
+
+ EasyMock.expect(context.getParent()).andReturn(container);
+ EasyMock.expect(container.getParent()).andReturn(engine);
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host1");
+ EasyMock.expect(context.getPath()).andReturn("/context");
+ EasyMock.expect(context.getParent()).andReturn(host);
+ EasyMock.expect(host.getName()).andReturn("host");
+ EasyMock.expect(host.findAliases()).andReturn(new String[] { "alias1",
"alias2" });
+
+ EasyMock.replay(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+
+ results = this.source.getResetRequests();
+
+ EasyMock.verify(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+
+ assertEquals(5, results.size());
+
+ MCMPRequest request = results.get(0);
+ Map<String, String> parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.REMOVE_APP, request.getRequestType());
+ assertTrue(request.isWildcard());
+ assertEquals(1, parameters.size());
+ assertEquals("host", parameters.get("JVMRoute"));
+
+ request = results.get(1);
+ parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.CONFIG, request.getRequestType());
+ assertFalse(request.isWildcard());
+ assertEquals(17, parameters.size());
+ assertEquals("host1", parameters.get("JVMRoute"));
+ assertEquals("172.0.0.1", parameters.get("Host"));
+ assertEquals("0", parameters.get("Port"));
+ assertEquals("ajp", parameters.get("Type"));
+ assertEquals("domain", parameters.get("Domain"));
+ assertEquals("On", parameters.get("flushpackets"));
+ assertEquals("1", parameters.get("flushwait"));
+ assertEquals("2", parameters.get("ping"));
+ assertEquals("3", parameters.get("smax"));
+ assertEquals("4", parameters.get("ttl"));
+ assertEquals("5", parameters.get("Timeout"));
+ assertEquals("S", parameters.get("Balancer"));
+ assertEquals("No", parameters.get("StickySession"));
+ assertEquals("Yes", parameters.get("StickySessionRemove"));
+ assertEquals("No", parameters.get("StickySessionForce"));
+ assertEquals("6", parameters.get("WaitWorker"));
+ assertEquals("7", parameters.get("Maxattempts"));
+
+ request = results.get(2);
+ parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.ENABLE_APP, request.getRequestType());
+ assertFalse(request.isWildcard());
+ assertEquals(3, parameters.size());
+ assertEquals("host1", parameters.get("JVMRoute"));
+ assertEquals("/context", parameters.get("Context"));
+ assertEquals("host,alias1,alias2", parameters.get("Alias"));
+
+ assertSame(request1, results.get(3));
+ assertSame(request2, results.get(4));
+
+ EasyMock.reset(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+
+
+ // Test non-master
+ results = this.source.getResetRequests();
+
+ assertTrue(results.isEmpty());
+ }
+
+ public void testGetLocalResetRequests() throws Exception
+ {
+ // Test w/out server
+ List<MCMPRequest> results = this.source.getLocalResetRequests();
+
+ assertTrue(results.isEmpty());
+
+
+ // Test w/server
+ Server server = EasyMock.createStrictMock(Server.class);
+ Service service = EasyMock.createStrictMock(Service.class);
+ Engine engine = EasyMock.createStrictMock(Engine.class);
+ Container container = EasyMock.createStrictMock(Container.class);
+ Context context = EasyMock.createStrictMock(Context.class);
+ Host host = EasyMock.createStrictMock(Host.class);
+ Connector connector = new Connector("AJP/1.3");
+
+ this.source.setJbossWebServer(server);
+
+ EasyMock.expect(server.findServices()).andReturn(new Service[] { service });
+ EasyMock.expect(service.getContainer()).andReturn(engine);
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host").times(2);
+
+ EasyMock.expect(engine.getService()).andReturn(service);
+ EasyMock.expect(service.findConnectors()).andReturn(new Connector[] { connector
});
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host1");
+
+ EasyMock.expect(this.nodeConfig.getDomain()).andReturn("domain");
+ EasyMock.expect(this.nodeConfig.getFlushPackets()).andReturn(Boolean.TRUE);
+ EasyMock.expect(this.nodeConfig.getFlushWait()).andReturn(1);
+ EasyMock.expect(this.nodeConfig.getPing()).andReturn(2);
+ EasyMock.expect(this.nodeConfig.getSmax()).andReturn(3);
+ EasyMock.expect(this.nodeConfig.getTtl()).andReturn(4);
+ EasyMock.expect(this.nodeConfig.getNodeTimeout()).andReturn(5);
+ EasyMock.expect(this.nodeConfig.getBalancer()).andReturn("S");
+
+ EasyMock.expect(this.balancerConfig.getStickySession()).andReturn(Boolean.FALSE);
+
EasyMock.expect(this.balancerConfig.getStickySessionRemove()).andReturn(Boolean.TRUE);
+
EasyMock.expect(this.balancerConfig.getStickySessionForce()).andReturn(Boolean.FALSE);
+ EasyMock.expect(this.balancerConfig.getWorkerTimeout()).andReturn(6);
+ EasyMock.expect(this.balancerConfig.getMaxAttempts()).andReturn(7);
+
+ EasyMock.expect(engine.findChildren()).andReturn(new Container[] { container });
+ EasyMock.expect(container.findChildren()).andReturn(new Container[] { context });
+ EasyMock.expect(context.isStarted()).andReturn(true);
+
+ EasyMock.expect(context.getParent()).andReturn(container);
+ EasyMock.expect(container.getParent()).andReturn(engine);
+ EasyMock.expect(engine.getJvmRoute()).andReturn("host1");
+ EasyMock.expect(context.getPath()).andReturn("/context");
+ EasyMock.expect(context.getParent()).andReturn(host);
+ EasyMock.expect(host.getName()).andReturn("host");
+ EasyMock.expect(host.findAliases()).andReturn(new String[] { "alias1",
"alias2" });
+
+ EasyMock.replay(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+
+ results = this.source.getLocalResetRequests();
+
+ EasyMock.verify(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+
+ assertEquals(3, results.size());
+
+ MCMPRequest request = results.get(0);
+ Map<String, String> parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.REMOVE_APP, request.getRequestType());
+ assertTrue(request.isWildcard());
+ assertEquals(1, parameters.size());
+ assertEquals("host", parameters.get("JVMRoute"));
+
+ request = results.get(1);
+ parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.CONFIG, request.getRequestType());
+ assertFalse(request.isWildcard());
+ assertEquals(17, parameters.size());
+ assertEquals("host1", parameters.get("JVMRoute"));
+ assertEquals("172.0.0.1", parameters.get("Host"));
+ assertEquals("0", parameters.get("Port"));
+ assertEquals("ajp", parameters.get("Type"));
+ assertEquals("domain", parameters.get("Domain"));
+ assertEquals("On", parameters.get("flushpackets"));
+ assertEquals("1", parameters.get("flushwait"));
+ assertEquals("2", parameters.get("ping"));
+ assertEquals("3", parameters.get("smax"));
+ assertEquals("4", parameters.get("ttl"));
+ assertEquals("5", parameters.get("Timeout"));
+ assertEquals("S", parameters.get("Balancer"));
+ assertEquals("No", parameters.get("StickySession"));
+ assertEquals("Yes", parameters.get("StickySessionRemove"));
+ assertEquals("No", parameters.get("StickySessionForce"));
+ assertEquals("6", parameters.get("WaitWorker"));
+ assertEquals("7", parameters.get("Maxattempts"));
+
+ request = results.get(2);
+ parameters = request.getParameters();
+
+ assertSame(MCMPRequestType.ENABLE_APP, request.getRequestType());
+ assertFalse(request.isWildcard());
+ assertEquals(3, parameters.size());
+ assertEquals("host1", parameters.get("JVMRoute"));
+ assertEquals("/context", parameters.get("Context"));
+ assertEquals("host,alias1,alias2", parameters.get("Alias"));
+
+ EasyMock.reset(this.singleton, this.key, this.partition, server, service, engine,
container, context, host, this.nodeConfig, this.balancerConfig);
+ }
+}