KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 package org.jboss.test.testbeancluster.test;
24
25 import java.util.Date JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
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 /**
46  * Tests of stateless/stateful HA behavior using the pooled invoker
47  *
48  * @author <a HREF="mailto:sacha.labourey@jboss.org">Sacha Labourey</a>.
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 55456 $
51  */

52 public class PooledHAUnitTestCase extends JBossClusteredTestCase
53 {
54    static boolean deployed = false;
55    static Date JavaDoc startDate = new Date JavaDoc();
56
57    protected final String JavaDoc namingFactory =
58    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
59
60    protected final String JavaDoc providerURL =
61    System.getProperty(Context.PROVIDER_URL);
62
63    public PooledHAUnitTestCase (String JavaDoc name)
64    {
65       super(name);
66    }
67
68    public static Test suite() throws Exception JavaDoc
69    {
70       Test t1 = JBossClusteredTestCase.getDeploySetup(PooledHAUnitTestCase.class, "pooledha.jar");
71       return t1;
72    }
73
74    /**
75     * Test the equals/hashCode behavior for ServerAddress
76     * @throws Exception
77     */

78    public void testServerAddressHashEquals()
79       throws Exception JavaDoc
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc();
87       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
88       oos.writeObject(sa1);
89       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
90       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
91       sa2 = (ServerAddress) ois.readObject();
92       assertEquals(sa1, sa2);
93       assertEquals(sa1.hashCode(), sa2.hashCode());
94
95       // Different tcpNoDelay should not be equal
96
sa2 = new ServerAddress("127.0.0.1", 4445, true, 61, null);
97       assertNotSame(sa1, sa2);
98       // Different ports should not be equal
99
sa2 = new ServerAddress("127.0.0.1", 4446, false, 60, null);
100       assertNotSame(sa1, sa2);
101       // Different host should not be equal
102
sa2 = new ServerAddress("127.0.0.2", 4445, false, 60, null);
103       assertNotSame(sa1, sa2);
104    }
105
106    public void testStatelessBeanLoadBalancing()
107       throws Exception JavaDoc
108    {
109       log.debug("testStatelessBeanLoadBalancing - Trying the context...");
110
111       // Connect to the server0 JNDI
112
String JavaDoc[] urls = getNamingURLs();
113       Properties JavaDoc env1 = new Properties JavaDoc();
114       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
115          "org.jnp.interfaces.NamingContextFactory");
116       env1.setProperty(Context.PROVIDER_URL, urls[0]);
117       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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       // Simple connection count stress test
127
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          // N cluster nodes + 1 for unclustered
139
int expectedCount = urls.length + 1;
140          assertEquals("TotalPoolCount", expectedCount, totalCount);
141          long usedPooled = PooledInvokerProxy.getUsedPooled();
142          // iter * Ncalls - expectedCount (for the initial conn creation)
143
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 JavaDoc
192    {
193       log.debug("testStatelessBeanLoadBalancing - Trying the context...");
194
195       // Connect to the server0 JNDI
196
String JavaDoc[] urls = getNamingURLs();
197       Properties JavaDoc env1 = new Properties JavaDoc();
198       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
199          "org.jnp.interfaces.NamingContextFactory");
200       env1.setProperty(Context.PROVIDER_URL, urls[0]);
201       InitialContext JavaDoc ctx = new InitialContext JavaDoc(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       // Simple connection count stress test
211
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          // N cluster nodes + 1 for unclustered
226
int expectedCount = urls.length + 1;
227          assertEquals("TotalPoolCount", expectedCount, totalCount);
228          long usedPooled = PooledInvokerProxy.getUsedPooled();
229          // iter * Ncalls - expectedCount (for the initial conn creation)
230
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       // Now we switch to the other node, simulating a failure on node 1
247
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       // we change our name to see if it replicates to node 1
254
statefulSession.setName ("Changed");
255
256       // now we travel back on node 1
257
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       // N cluster nodes + 1 for unclustered
270
int expectedCount = urls.length + 1;
271       assertEquals("TotalPoolCount", expectedCount, totalCount);
272    }
273 }
274
Popular Tags