1 22 package org.jboss.test.testbeancluster.test; 23 24 25 import java.util.Date ; 26 import java.util.Properties ; 27 28 import javax.management.ObjectName ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 32 import junit.framework.Test; 33 34 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 35 import org.jboss.test.testbeancluster.interfaces.StatelessSession; 36 import org.jboss.test.testbeancluster.interfaces.StatelessSessionHome; 37 import org.jboss.test.testbeancluster.interfaces.StatefulSession; 38 import org.jboss.test.testbean.interfaces.StatefulSessionHome; 39 40 import org.jboss.test.JBossClusteredTestCase; 41 import org.jboss.test.testbeancluster.interfaces.NodeAnswer; 42 43 50 public class BeanUnitTestCase extends JBossClusteredTestCase 51 { 52 static boolean deployed = false; 53 public static int test = 0; 54 static Date startDate = new Date (); 55 56 protected final String namingFactory = 57 System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 58 59 protected final String providerURL = 60 System.getProperty(Context.PROVIDER_URL); 61 62 public BeanUnitTestCase (String name) 63 { 64 super(name); 65 } 66 67 public static Test suite() throws Exception 68 { 69 Test t1 = JBossClusteredTestCase.getDeploySetup(BeanUnitTestCase.class, "testbeancluster.jar"); 70 return t1; 71 } 72 73 public void testStatelessBeanLoadBalancing() 74 throws Exception 75 { 76 getLog().debug(++test+"- "+"Trying the context..."); 77 78 String [] urls = getNamingURLs(); 80 Properties env1 = new Properties (); 81 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 82 "org.jnp.interfaces.NamingContextFactory"); 83 env1.setProperty(Context.PROVIDER_URL, urls[0]); 84 InitialContext ctx = new InitialContext (env1); 85 86 getLog().debug("Test Stateless Bean load-balancing"); 87 getLog().debug("=================================="); 88 getLog().debug(++test+"- "+"Looking up the home nextgen.StatelessSession..."); 89 StatelessSessionHome statelessSessionHome = 90 (StatelessSessionHome) ctx.lookup("nextgen_StatelessSession"); 91 if (statelessSessionHome!= null ) getLog().debug("ok"); 92 getLog().debug(++test+"- "+"Calling create on StatelessSessionHome..."); 93 StatelessSession statelessSession = 94 (StatelessSession)statelessSessionHome.create(); 95 assertTrue("statelessSessionHome.create() != null", statelessSession != null); 96 getLog().debug("ok"); 97 98 getLog().debug(++test+"- "+"Calling getEJBHome() on StatelessSession..."); 99 assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null); 100 getLog().debug("ok"); 101 102 getLog().debug(++test+"- "+"Reseting the number of calls made on beans (making 2 calls)... "); 103 for (int i=0; i<6; i++) 104 { 105 getLog().debug(++test+"- "+" Reseting number... "); 106 statelessSession.resetNumberOfCalls (); 107 } 108 109 getLog().debug(++test+"- "+"Now making 20 calls on this remote... "); 110 for (int i=0; i<20; i++) 111 { 112 getLog().debug(++test+"- "+" Calling remote... "); 113 statelessSession.makeCountedCall (); 114 } 115 116 getLog().debug(++test+"- "+"Getting the number of calls that have been performed on each bean... "); 117 long node1 = statelessSession.getCallCount(); 118 getLog().debug(++test+"- "+"One node has received: " + node1); 119 120 long node2 = statelessSession.getCallCount(); 121 getLog().debug(++test+"- "+"The other node has received: " + node2); 122 123 if (node1 == node2 && 124 node1 == 10) 125 { 126 getLog().debug(++test+"- "+"Test is ok."); 127 } 128 else 129 { 130 getLog().debug(++test+"- "+"Something wrong has happened! Calls seems not to have been load-balanced."); 131 fail ("Calls have not been correctly load-balanced on the SLSB remote interface."); 132 } 133 134 statelessSession.remove(); 135 getLog().debug("ok"); 136 } 137 138 public void testRoundRobin() throws Exception 139 { 140 getLog().debug("+++ Enter testRoundRobin"); 141 142 String [] urls = getNamingURLs(); 144 Properties env1 = new Properties (); 145 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 146 "org.jnp.interfaces.NamingContextFactory"); 147 env1.setProperty(Context.PROVIDER_URL, urls[0]); 148 InitialContext ctx = new InitialContext (env1); 149 150 for (int i = 0; i < 4; i++) 152 { 153 StatelessSessionHome home = 154 (StatelessSessionHome) ctx.lookup("nextgen_StatelessSession"); 155 assertTrue("home != null", home != null); 156 getLog().debug("ok"); 157 158 String last = null; 159 for (int j = 0; j < 10; j++) 160 { 161 StatelessSession slsb = home.create(); 162 assertTrue("slsb != null", slsb != null); 163 getLog().debug("ok"); 164 165 for (int k = 0; k < 3; k++) 166 { 167 String cur = slsb.getBindAddress(); 168 if (cur == null) 169 { 170 getLog().debug("jboss.bind.address property not set; aborting"); 171 return; 172 } 173 174 assertFalse("Target switched", cur.equals(last)); 175 last = cur; 176 } 177 } 178 } 179 180 getLog().debug("+++ Exit testRoundRobin"); 181 } 182 183 public void testStatelessBeanColocation() throws Exception 184 { 185 getLog().debug(++test+"- "+"Trying the context..."); 186 187 String [] urls = getNamingURLs(); 189 Properties env1 = new Properties (); 190 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 191 "org.jnp.interfaces.NamingContextFactory"); 192 env1.setProperty(Context.PROVIDER_URL, urls[0]); 193 InitialContext ctx = new InitialContext (env1); 194 195 getLog().debug("Test Stateless Bean colocation"); 196 getLog().debug("=================================="); 197 getLog().debug(++test+"- "+"Looking up the home nextgen.StatelessSession..."); 198 StatelessSessionHome statelessSessionHome = 199 (StatelessSessionHome) ctx.lookup("nextgen_StatelessSession"); 200 if (statelessSessionHome!= null ) getLog().debug("ok"); 201 getLog().debug(++test+"- "+"Calling create on StatelessSessionHome..."); 202 StatelessSession statelessSession = (StatelessSession)statelessSessionHome.create(); 203 assertTrue("statelessSessionHome.create() != null", statelessSession != null); 204 getLog().debug("ok"); 205 206 getLog().debug(++test+"- "+"reset number of calls"); 207 statelessSession.resetNumberOfCalls(); 208 statelessSession.resetNumberOfCalls(); 209 210 getLog().debug(++test+"- "+"callBusinessMethodB"); 212 String jndiURL = urls[0] + "/nextgen_StatelessSession"; 213 statelessSession.callBusinessMethodB(jndiURL); 214 215 getLog().debug(++test+"- "+"Getting the number of calls that have been performed on each bean... "); 216 long node1 = statelessSession.getCallCount(); 217 getLog().debug(++test+"- "+"One node has received: " + node1); 218 219 long node2 = statelessSession.getCallCount(); 220 getLog().debug(++test+"- "+"The other node has received: " + node2); 221 222 if ((node1 == 2 && node2 == 0) || (node1 == 0 && node2 == 2)) 223 { 224 getLog().debug(++test+"- "+"Test is ok."); 225 } 226 else 227 { 228 getLog().debug(++test+"- "+"Something wrong has happened! Calls should have been colocated."); 229 fail ("Calls have not been correctly colocated."); 230 } 231 getLog().debug("ok"); 232 } 233 234 public void testStatefulBeanFailover() 235 throws Exception 236 { 237 getLog().debug(++test+"- "+"Trying the context..."); 238 239 String [] urls = getNamingURLs(); 241 Properties env1 = new Properties (); 242 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 243 "org.jnp.interfaces.NamingContextFactory"); 244 env1.setProperty(Context.PROVIDER_URL, urls[0]); 245 InitialContext ctx = new InitialContext (env1); 246 247 getLog().debug("Test Stateful Bean Failover"); 248 getLog().debug("=================================="); 249 getLog().debug(++test+"- "+"Looking up the home nextgen.StatefulSession..."); 250 StatefulSessionHome statefulSessionHome = 251 (StatefulSessionHome) ctx.lookup("nextgen_StatefulSession"); 252 if (statefulSessionHome!= null ) getLog().debug("ok"); 253 getLog().debug(++test+"- "+"Calling create on StatefulSessionHome..."); 254 StatefulSession statefulSession = 255 (StatefulSession)statefulSessionHome.create("Bupple-Dupple"); 256 assertTrue("statefulSessionHome.create() != null", statefulSession != null); 257 getLog().debug("ok"); 258 259 NodeAnswer node1 = statefulSession.getNodeState (); 260 getLog ().debug (node1); 261 262 System.setProperty ("JBossCluster-DoFail", "once"); 265 NodeAnswer node2 = statefulSession.getNodeState (); 266 getLog ().debug (node2); 267 268 assertTrue ("No failover has occured!", !node1.nodeId.equals (node2.nodeId)); 269 270 assertTrue ("Value is not identical on replicated node", "Bupple-Dupple".equals (node1.answer) && 271 node1.answer.equals (node2.answer) ); 272 273 statefulSession.setName ("Changed"); 276 277 System.setProperty ("JBossCluster-DoFail", "once"); 280 node1 = statefulSession.getNodeState (); 281 getLog ().debug (node1); 282 283 assertTrue ("No failover has occured!", !node1.nodeId.equals (node2.nodeId)); 284 285 assertTrue ("Value is not identical on replicated node", "Changed".equals (node1.answer) ); 286 287 statefulSession.remove(); 288 getLog().debug("ok"); 289 } 290 291 301 public void testStatefulPassivationExpiration() 302 throws Exception 303 { 304 log.info("+++ testStatefulPassivationExpiration"); 305 306 String [] urls = getNamingURLs(); 308 Properties env1 = new Properties (); 309 env1.setProperty(Context.INITIAL_CONTEXT_FACTORY, 310 "org.jnp.interfaces.NamingContextFactory"); 311 env1.setProperty(Context.PROVIDER_URL, urls[0]); 312 InitialContext ctx = new InitialContext (env1); 313 314 int beanCount = 500; 315 StatefulSessionHome home = 316 (StatefulSessionHome) ctx.lookup("nextgen_ExpiredStatefulSession"); 317 long start = System.currentTimeMillis(); 318 log.info("Start bean creation"); 319 StatefulSession[] beans = new StatefulSession[beanCount]; 320 long[] accessStamp = new long[beanCount]; 321 for(int n = 0; n < beans.length; n ++) 322 { 323 beans[n] = (StatefulSession) home.create("testStatefulPassivationExpiration#"+n); 324 accessStamp[n] = System.currentTimeMillis(); 325 } 326 long end = System.currentTimeMillis(); 327 log.info("End bean creation, elapsed="+(end - start)); 328 329 int N = 5000; 330 long min = 99999, max = 0, maxInactive = 0; 331 for(int n = 0; n < N; n ++) 332 { 333 int id = n % beans.length; 334 StatefulSession bean = beans[id]; 335 if (bean == null) 336 continue; 337 String name = "testStatefulPassiviationExpiration#"+id; 338 long callStart = System.currentTimeMillis(); 339 long inactive = callStart - accessStamp[id]; 340 try 341 { 342 bean.setName(name); 343 NodeAnswer node = bean.getNodeState(); 344 long now = System.currentTimeMillis(); 345 long elapsed = now - callStart; 346 accessStamp[id] = now; 347 assertTrue("NodeAnswer == "+name, node.answer.equals(name)); 348 min = Math.min(min, elapsed); 349 max = Math.max(max, elapsed); 350 maxInactive = Math.max(maxInactive, inactive); 351 log.debug(n+", elapsed="+elapsed+", inactive="+inactive); 352 } 353 catch (java.rmi.NoSuchObjectException nso) 354 { 355 log.debug(n+" Caught NoSuchObjectException on bean " + id + " -- inactive time = " + inactive); 356 beans[id] = null; 358 } 359 } 360 log.info(N+" calls complete, max="+max+", min="+min+", maxInactive="+maxInactive); 361 362 Thread.sleep(15000); 363 start = System.currentTimeMillis(); 364 for(int n = 0; n < beans.length; n ++) 365 { 366 beans[n] = (StatefulSession) home.create("testStatefulPassivationExpiration#"+n); 367 accessStamp[n] = System.currentTimeMillis(); 368 } 369 end = System.currentTimeMillis(); 370 log.info("End second round bean creation, elapsed="+(end - start)); 371 for(int n = 0; n < N; n ++) 372 { 373 int id = n % beans.length; 374 StatefulSession bean = beans[id]; 375 if (bean == null) 376 continue; 377 String name = "testStatefulPassiviationExpiration#"+id; 378 long callStart = System.currentTimeMillis(); 379 long inactive = callStart - accessStamp[id]; 380 try 381 { 382 bean.setName(name); 383 NodeAnswer node = bean.getNodeState(); 384 long now = System.currentTimeMillis(); 385 long elapsed = now - callStart; 386 accessStamp[id] = now; 387 assertTrue("NodeAnswer == "+name, node.answer.equals(name)); 388 min = Math.min(min, elapsed); 389 max = Math.max(max, elapsed); 390 maxInactive = Math.max(maxInactive, inactive); 391 log.debug(n+", elapsed="+elapsed+", inactive="+inactive); 392 } 393 catch (java.rmi.NoSuchObjectException nso) 394 { 395 log.debug(n+" Caught NoSuchObjectException on bean " + id + " -- inactive time = " + (callStart - accessStamp[id])); 396 beans[id] = null; 398 } 399 } 400 log.info(N+" calls complete, max="+max+", min="+min+", maxInactive="+maxInactive); 401 404 for(int n = 0; n < beans.length; n ++) 405 { 406 try 407 { 408 if (beans[n] != null) 409 beans[n].remove(); 410 } 411 catch (java.rmi.NoSuchObjectException nso) 412 { 413 log.debug("Caught NoSuchObjectException removing bean " + n); 414 } 415 } 416 417 String oNameS = "jboss.j2ee:jndiName=ExpiredStatefulSession,plugin=cache,service=EJB"; 419 ObjectName oName = new ObjectName (oNameS); 420 RMIAdaptor[] adaptors = getAdaptors(); 421 Long cacheCount = (Long ) adaptors[0].getAttribute(oName, "CacheSize"); 422 assertEquals("CacheSize is zero", 0, cacheCount.longValue()); 423 } 429 } 430 | Popular Tags |