KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbeancluster > test > BeanUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.testbeancluster.test;
23
24
25 import java.util.Date JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
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 /**
44  * Test SLSB for load-balancing behaviour
45  *
46  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 56926 $
49  */

50 public class BeanUnitTestCase extends JBossClusteredTestCase
51 {
52    static boolean deployed = false;
53    public static int test = 0;
54    static Date JavaDoc startDate = new Date JavaDoc();
55    
56    protected final String JavaDoc namingFactory =
57    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
58    
59    protected final String JavaDoc providerURL =
60    System.getProperty(Context.PROVIDER_URL);
61    
62    public BeanUnitTestCase (String JavaDoc name)
63    {
64       super(name);
65    }
66
67    public static Test suite() throws Exception JavaDoc
68    {
69       Test t1 = JBossClusteredTestCase.getDeploySetup(BeanUnitTestCase.class, "testbeancluster.jar");
70       return t1;
71    }
72
73    public void testStatelessBeanLoadBalancing()
74       throws Exception JavaDoc
75    {
76       getLog().debug(++test+"- "+"Trying the context...");
77       
78       // Connect to the server0 JNDI
79
String JavaDoc[] urls = getNamingURLs();
80       Properties JavaDoc env1 = new Properties JavaDoc();
81       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
82          "org.jnp.interfaces.NamingContextFactory");
83       env1.setProperty(Context.PROVIDER_URL, urls[0]);
84       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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 JavaDoc
139    {
140       getLog().debug("+++ Enter testRoundRobin");
141       
142       // Connect to the server0 JNDI
143
String JavaDoc[] urls = getNamingURLs();
144       Properties JavaDoc env1 = new Properties JavaDoc();
145       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
146          "org.jnp.interfaces.NamingContextFactory");
147       env1.setProperty(Context.PROVIDER_URL, urls[0]);
148       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env1);
149       
150       // Do 4 home lookups in case different lookups lead to different patterns
151
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 JavaDoc 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 JavaDoc 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 JavaDoc
184    {
185       getLog().debug(++test+"- "+"Trying the context...");
186       
187       // Connect to the server0 JNDI
188
String JavaDoc[] urls = getNamingURLs();
189       Properties JavaDoc env1 = new Properties JavaDoc();
190       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
191          "org.jnp.interfaces.NamingContextFactory");
192       env1.setProperty(Context.PROVIDER_URL, urls[0]);
193       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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       // This should make two calls on the same node
211
getLog().debug(++test+"- "+"callBusinessMethodB");
212       String JavaDoc 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 JavaDoc
236    {
237       getLog().debug(++test+"- "+"Trying the context...");
238       
239       // Connect to the server0 JNDI
240
String JavaDoc[] urls = getNamingURLs();
241       Properties JavaDoc env1 = new Properties JavaDoc();
242       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
243          "org.jnp.interfaces.NamingContextFactory");
244       env1.setProperty(Context.PROVIDER_URL, urls[0]);
245       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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       // Now we switch to the other node, simulating a failure on node 1
263
//
264
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       // we change our name to see if it replicates to node 1
274
//
275
statefulSession.setName ("Changed");
276       
277       // now we travel back on node 1
278
//
279
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    /**
292     * The test creates 500 stateful session beans, executes some calls to
293     * stress state replication, waits for passivation and exipration to kick
294     * in, and then updates the sessions to produce the session removal
295     * conflict seen in JBAS-1560. This is sensative to timing issues so a
296     * failure in activation can show up; we catch any NoSuchObjectExceptio
297     * to handle this.
298     *
299     * @throws Exception
300     */

301    public void testStatefulPassivationExpiration()
302       throws Exception JavaDoc
303    {
304       log.info("+++ testStatefulPassivationExpiration");
305
306       // Connect to the server0 JNDI
307
String JavaDoc[] urls = getNamingURLs();
308       Properties JavaDoc env1 = new Properties JavaDoc();
309       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
310          "org.jnp.interfaces.NamingContextFactory");
311       env1.setProperty(Context.PROVIDER_URL, urls[0]);
312       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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 JavaDoc 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 JavaDoc nso)
354          {
355             log.debug(n+" Caught NoSuchObjectException on bean " + id + " -- inactive time = " + inactive);
356             // Remove from list so we don't keep fruitlessly calling the removed bean
357
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 JavaDoc 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 JavaDoc nso)
394          {
395             log.debug(n+" Caught NoSuchObjectException on bean " + id + " -- inactive time = " + (callStart - accessStamp[id]));
396             // Remove from list so we don't keep fruitlessly calling the removed bean
397
beans[id] = null;
398          }
399       }
400       log.info(N+" calls complete, max="+max+", min="+min+", maxInactive="+maxInactive);
401       // BES -- max call time check removed, as it can randomly fail if there is a long GC pause
402
// assertTrue("max < 3000", max < 3000 );
403

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 JavaDoc nso)
412          {
413             log.debug("Caught NoSuchObjectException removing bean " + n);
414          }
415       }
416       
417       // Confirm that the bean cache is empty
418
String JavaDoc oNameS = "jboss.j2ee:jndiName=ExpiredStatefulSession,plugin=cache,service=EJB";
419       ObjectName JavaDoc oName = new ObjectName JavaDoc(oNameS);
420       RMIAdaptor[] adaptors = getAdaptors();
421       Long JavaDoc cacheCount = (Long JavaDoc) adaptors[0].getAttribute(oName, "CacheSize");
422       assertEquals("CacheSize is zero", 0, cacheCount.longValue());
423       // Checking the passivated count is invalid, as it doesn't get reduced
424
// when remove() is called on a bean -- only when the passivation cleanup
425
// thread runs
426
//cacheCount = (Long) adaptors[0].getAttribute(oName, "PassivatedCount");
427
//assertEquals("PassivatedCount is zero", 0, cacheCount.longValue());
428
}
429 }
430
Popular Tags