1 22 23 package org.jboss.test.testbeancluster.test; 24 25 import java.util.Date ; 26 import java.util.Properties ; 27 import java.io.ObjectOutputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.ByteArrayInputStream ; 30 import java.io.ObjectInputStream ; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 35 import org.jboss.test.JBossClusteredTestCase; 36 import org.jboss.test.testbean.interfaces.StatefulSessionHome; 37 import org.jboss.test.testbeancluster.interfaces.StatelessSessionHome; 38 import org.jboss.test.testbeancluster.interfaces.StatelessSession; 39 import org.jboss.test.testbeancluster.interfaces.StatefulSession; 40 import org.jboss.test.testbeancluster.interfaces.NodeAnswer; 41 import org.jboss.invocation.pooled.interfaces.PooledInvokerProxy; 42 import org.jboss.invocation.pooled.interfaces.ServerAddress; 43 import junit.framework.Test; 44 45 52 public class PooledHAUnitTestCase extends JBossClusteredTestCase 53 { 54 static boolean deployed = false; 55 static Date startDate = new Date (); 56 57 protected final String namingFactory = 58 System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 59 60 protected final String providerURL = 61 System.getProperty(Context.PROVIDER_URL); 62 63 public PooledHAUnitTestCase (String name) 64 { 65 super(name); 66 } 67 68 public static Test suite() throws Exception 69 { 70 Test t1 = JBossClusteredTestCase.getDeploySetup(PooledHAUnitTestCase.class, "pooledha.jar"); 71 return t1; 72 } 73 74 78 public void testServerAddressHashEquals() 79 throws Exception 80 { 81 ServerAddress sa1 = new ServerAddress("127.0.0.1", 4445, false, 60, null); 82 ServerAddress sa2 = new ServerAddress("127.0.0.1", 4445, false, 61, null); 83 assertEquals(sa1, sa2); 84 assertEquals(sa1.hashCode(), sa2.hashCode()); 85 86 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 87 ObjectOutputStream oos = new ObjectOutputStream (baos); 88 oos.writeObject(sa1); 89 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 90 ObjectInputStream ois = new ObjectInputStream (bais); 91 sa2 = (ServerAddress) ois.readObject(); 92 assertEquals(sa1, sa2); 93 assertEquals(sa1.hashCode(), sa2.hashCode()); 94 95 sa2 = new ServerAddress("127.0.0.1", 4445, true, 61, null); 97 assertNotSame(sa1, sa2); 98 sa2 = new ServerAddress("127.0.0.1", 4446, false, 60, null); 100 assertNotSame(sa1, sa2); 101 sa2 = new ServerAddress("127.0.0.2", 4445, false, 60, null); 103 assertNotSame(sa1, sa2); 104 } 105 106 public void testStatelessBeanLoadBalancing() 107 throws Exception 108 { 109 log.debug("testStatelessBeanLoadBalancing - Trying the context..."); 110 111 String [] urls = getNamingURLs(); 113 Properties env1 = new Properties (); 114 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 115 "org.jnp.interfaces.NamingContextFactory"); 116 env1.setProperty(Context.PROVIDER_URL, urls[0]); 117 InitialContext ctx = new InitialContext (env1); 118 119 log.debug("Looking up the home pooledha_StatelessSession..."+urls[0]); 120 StatelessSessionHome homeHA = 121 (StatelessSessionHome) ctx.lookup("pooledha_StatelessSession"); 122 log.debug("Looking up the home pooled_StatelessSession..."+urls[0]); 123 StatelessSessionHome home = 124 (StatelessSessionHome) ctx.lookup("pooled_StatelessSession"); 125 126 PooledInvokerProxy.clearStats(); 128 PooledInvokerProxy.clearPools(); 129 for(int n = 0; n < 100; n ++) 130 { 131 StatelessSession tmp = homeHA.create(); 132 StatelessSession tmp2 = home.create(); 133 tmp.getCallCount(); 134 tmp2.getCallCount(); 135 tmp.getCallCount(); 136 tmp2.getCallCount(); 137 int totalCount = PooledInvokerProxy.getTotalPoolCount(); 138 int expectedCount = urls.length + 1; 140 assertEquals("TotalPoolCount", expectedCount, totalCount); 141 long usedPooled = PooledInvokerProxy.getUsedPooled(); 142 int expectedUsedPooled = (n+1) * 6 - expectedCount; 144 assertEquals("UsedPooled", expectedUsedPooled, usedPooled); 145 } 146 long inUseCount = PooledInvokerProxy.getInUseCount(); 147 assertEquals("InUseCount", 0, inUseCount); 148 149 log.debug("Calling create on StatelessSessionHome..."); 150 StatelessSession statelessSession = homeHA.create(); 151 assertTrue("homeHA.create() != null", statelessSession != null); 152 log.debug("Calling getEJBHome() on StatelessSession..."); 153 assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null); 154 155 log.debug("Reseting the number of calls made on beans (making 2 calls)... "); 156 for (int i=0; i<6; i++) 157 { 158 log.debug("Reseting number... "); 159 statelessSession.resetNumberOfCalls (); 160 } 161 162 log.debug("- "+"Now making 20 calls on this remote... "); 163 for (int i=0; i<20; i++) 164 { 165 log.debug("- "+" Calling remote... "); 166 statelessSession.makeCountedCall (); 167 } 168 169 log.debug("- "+"Getting the number of calls that have been performed on each bean... "); 170 long node1 = statelessSession.getCallCount(); 171 log.debug("- "+"One node has received: " + node1); 172 173 long node2 = statelessSession.getCallCount(); 174 log.debug("- "+"The other node has received: " + node2); 175 176 if (node1 == node2 && node1 == 10) 177 { 178 log.debug("- "+"Test is ok."); 179 } 180 else if( urls.length > 1 ) 181 { 182 log.debug("- "+"Something wrong has happened! Calls seems not to have been load-balanced."); 183 fail ("call count mismatch: "+node1+" != "+node2); 184 } 185 186 statelessSession.remove(); 187 log.debug("ok"); 188 } 189 190 public void testStatefulBeanFailover() 191 throws Exception 192 { 193 log.debug("testStatelessBeanLoadBalancing - Trying the context..."); 194 195 String [] urls = getNamingURLs(); 197 Properties env1 = new Properties (); 198 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 199 "org.jnp.interfaces.NamingContextFactory"); 200 env1.setProperty(Context.PROVIDER_URL, urls[0]); 201 InitialContext ctx = new InitialContext (env1); 202 203 log.debug("Looking up the home pooledha_StatefulSession..."+urls[0]); 204 StatefulSessionHome homeHA = 205 (StatefulSessionHome) ctx.lookup("pooledha_StatefulSession"); 206 log.debug("Looking up the home pooled_StatelessSession..."+urls[0]); 207 StatelessSessionHome home = 208 (StatelessSessionHome) ctx.lookup("pooled_StatelessSession"); 209 210 PooledInvokerProxy.clearStats(); 212 PooledInvokerProxy.clearPools(); 213 for(int n = 0; n < 100; n ++) 214 { 215 StatefulSession tmp = (StatefulSession) homeHA.create(); 216 tmp.remove(); 217 tmp = (StatefulSession) homeHA.create(); 218 StatelessSession tmp2 = home.create(); 219 tmp.getNodeState(); 220 tmp2.getCallCount(); 221 tmp.getNodeState(); 222 tmp2.getCallCount(); 223 tmp.remove(); 224 int totalCount = PooledInvokerProxy.getTotalPoolCount(); 225 int expectedCount = urls.length + 1; 227 assertEquals("TotalPoolCount", expectedCount, totalCount); 228 long usedPooled = PooledInvokerProxy.getUsedPooled(); 229 int expectedUsedPooled = (n+1) * 9 - expectedCount; 231 assertEquals("UsedPooled", expectedUsedPooled, usedPooled); 232 } 233 long inUseCount = PooledInvokerProxy.getInUseCount(); 234 assertEquals("InUseCount", 0, inUseCount); 235 236 log.debug("Test Stateful Bean Failover"); 237 log.debug("Looking up the home nextgen.StatefulSession..."); 238 log.debug("Calling create on StatefulSessionHome..."); 239 StatefulSession statefulSession = 240 (StatefulSession)homeHA.create("Bupple-Dupple"); 241 assertTrue("statefulSessionHome.create() != null", statefulSession != null); 242 243 NodeAnswer node1 = statefulSession.getNodeState (); 244 getLog ().debug (node1); 245 246 System.setProperty ("JBossCluster-DoFail", "once"); 248 NodeAnswer node2 = statefulSession.getNodeState (); 249 log.debug (node2); 250 assertEquals("Value is identical on replicated node1", "Bupple-Dupple", node1.answer); 251 assertEquals("Value is identical on replicated node2", "Bupple-Dupple", node2.answer); 252 253 statefulSession.setName ("Changed"); 255 256 System.setProperty ("JBossCluster-DoFail", "once"); 258 node1 = statefulSession.getNodeState (); 259 log.debug(node1); 260 261 assertEquals("Value is identical on replicated node1", "Changed", node1.answer); 262 node2 = statefulSession.getNodeState (); 263 log.debug(node2); 264 assertEquals("Value is identical on replicated node2", "Changed", node2.answer); 265 266 statefulSession.remove(); 267 268 int totalCount = PooledInvokerProxy.getTotalPoolCount(); 269 int expectedCount = urls.length + 1; 271 assertEquals("TotalPoolCount", expectedCount, totalCount); 272 } 273 } 274 | Popular Tags |