001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cluster.test;
023:
024: import java.net.HttpURLConnection;
025:
026: import junit.framework.Test;
027:
028: import org.apache.commons.httpclient.Cookie;
029: import org.apache.commons.httpclient.Header;
030: import org.apache.commons.httpclient.HttpClient;
031: import org.apache.commons.httpclient.HttpState;
032: import org.apache.commons.httpclient.methods.GetMethod;
033: import org.jboss.test.JBossClusteredTestCase;
034:
035: /** Tests of http session replication
036: *
037: * @author Scott.Stark@jboss.org
038: * @version $Revision: 57211 $
039: */
040: public class WebSessionTestCase extends JBossClusteredTestCase {
041: /**
042: * Standard number of ms to pause between http requests
043: * to give session time to replicate
044: */
045: public static final long DEFAULT_SLEEP = BaseTest.DEFAULT_SLEEP;
046:
047: public WebSessionTestCase(String name) {
048: super (name);
049: }
050:
051: public static Test suite() throws Exception {
052: Test t1 = getDeploySetup(WebSessionTestCase.class,
053: "dist-ss.war");
054: return t1;
055: }
056:
057: /** This makes 2 requests to the jbosstest.cluster.node0 /dist-ss/StatefulSessionServlet
058: * followed by 2 requests to the jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet
059: * using the same session ID to validate that the session is replicated
060: * with the current value and updated correctly. The session AccessCount
061: * value is returned via the X-AccessCount header which should be 4 after
062: * the last request.
063: *
064: * @throws Exception
065: */
066: public void testServletSessionFailover() throws Exception {
067: getLog().debug("+++ testServletSessionFailover");
068:
069: String[] servers = super .getServers();
070: String[] httpURLs = super .getHttpURLs();
071: // Access the StatefulSessionServlet of dist-ss.war@server0 twice
072: String baseURL0 = httpURLs[0];
073: HttpClient httpConn = new HttpClient();
074: GetMethod servletGet0 = new GetMethod(baseURL0
075: + "/dist-ss/StatefulSessionServlet/");
076: int responseCode = httpConn.executeMethod(servletGet0);
077: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
078: Header accessCount = servletGet0
079: .getResponseHeader("X-AccessCount");
080: int count = Integer.parseInt(accessCount.getValue());
081: assertEquals("X-AccessCount ", 1, count);
082: // Get the state for the JSESSIONID
083: HttpState state = httpConn.getState();
084: servletGet0 = new GetMethod(baseURL0
085: + "/dist-ss/StatefulSessionServlet/");
086: responseCode = httpConn.executeMethod(servletGet0
087: .getHostConfiguration(), servletGet0, state);
088: accessCount = servletGet0.getResponseHeader("X-AccessCount");
089: count = Integer.parseInt(accessCount.getValue());
090: assertEquals("X-AccessCount ", 2, count);
091: // Get the JSESSIONID so we can reset the host
092: Cookie[] cookies = state.getCookies();
093: Cookie sessionID = null;
094: for (int c = 0; c < cookies.length; c++) {
095: Cookie k = cookies[c];
096: if (k.getName().equalsIgnoreCase("JSESSIONID"))
097: sessionID = k;
098: }
099: log.info("Saw JSESSIONID=" + sessionID);
100: // Reset the domain so that the cookie will be sent to server1
101: sessionID.setDomain(servers[1]);
102: state.addCookie(sessionID);
103: _sleep(DEFAULT_SLEEP);
104:
105: // Access the StatefulSessionServlet of dist-ss.war@server1 twice
106: String baseURL1 = httpURLs[1];
107: GetMethod servletGet1 = new GetMethod(baseURL1
108: + "/dist-ss/StatefulSessionServlet/");
109: responseCode = httpConn.executeMethod(servletGet1
110: .getHostConfiguration(), servletGet1, state);
111: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
112: accessCount = servletGet1.getResponseHeader("X-AccessCount");
113: count = Integer.parseInt(accessCount.getValue());
114: assertEquals("X-AccessCount ", 3, count);
115: servletGet1 = new GetMethod(baseURL1
116: + "/dist-ss/StatefulSessionServlet/");
117: responseCode = httpConn.executeMethod(servletGet1
118: .getHostConfiguration(), servletGet1, state);
119: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
120: accessCount = servletGet1.getResponseHeader("X-AccessCount");
121: count = Integer.parseInt(accessCount.getValue());
122: assertEquals("X-AccessCount ", 4, count);
123: }
124:
125: /** This makes 4 requests alternating between the jbosstest.cluster.node0
126: * /dist-ss/StatefulSessionServlet and jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet
127: * using the same session ID to validate that the session is replicated
128: * with the current value and updated correctly. The session AccessCount
129: * value is returned via the X-AccessCount header which should be 4 after
130: * the last request.
131: *
132: * Note: this test is not currently working since current http session
133: * replication assumes sticky session. It does not support random load
134: * balancing.
135: * bwang.
136: *
137: * @throws Exception
138: */
139: public void testServletSessionLoadBalancing() throws Exception {
140: getLog().debug("+++ testServletSessionLoadBalancing");
141:
142: String[] servers = getServers();
143: String[] httpURLs = super .getHttpURLs();
144: String baseURL0 = httpURLs[0];
145: String baseURL1 = baseURL0;
146: if (servers.length > 1) {
147: baseURL1 = httpURLs[1];
148: }
149: // Access the StatefulSessionServlet of dist-ss.war@server0 twice
150: HttpClient httpConn = new HttpClient();
151: GetMethod servletGet0 = new GetMethod(baseURL0
152: + "/dist-ss/StatefulSessionServlet/");
153: int responseCode = httpConn.executeMethod(servletGet0);
154: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
155: Header accessCount = servletGet0
156: .getResponseHeader("X-AccessCount");
157: int count = Integer.parseInt(accessCount.getValue());
158: assertEquals("X-AccessCount ", 1, count);
159: // Get the state for the JSESSIONID
160: HttpState state = httpConn.getState();
161: // Get the JSESSIONID so we can reset the host
162: Cookie[] cookies = state.getCookies();
163: Cookie sessionID = null;
164: for (int c = 0; c < cookies.length; c++) {
165: Cookie k = cookies[c];
166: if (k.getName().equalsIgnoreCase("JSESSIONID"))
167: sessionID = k;
168: }
169: log.info("Saw JSESSIONID=" + sessionID);
170: // Reset the domain so that the cookie will be sent to server1
171: sessionID.setDomain(servers[1]);
172: state.addCookie(sessionID);
173: _sleep(DEFAULT_SLEEP);
174: // Access the StatefulSessionServlet of dist-ss.war@server1 twice
175: GetMethod servletGet1 = new GetMethod(baseURL1
176: + "/dist-ss/StatefulSessionServlet/");
177: responseCode = httpConn.executeMethod(servletGet1
178: .getHostConfiguration(), servletGet1, state);
179: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
180: accessCount = servletGet1.getResponseHeader("X-AccessCount");
181: count = Integer.parseInt(accessCount.getValue());
182: assertEquals("X-AccessCount ", 2, count);
183:
184: _sleep(DEFAULT_SLEEP);
185: // Reset the domain so that the cookie will be sent to server0
186: sessionID.setDomain(servers[0]);
187: state.addCookie(sessionID);
188: servletGet0 = new GetMethod(baseURL0
189: + "/dist-ss/StatefulSessionServlet/");
190: responseCode = httpConn.executeMethod(servletGet0
191: .getHostConfiguration(), servletGet0, state);
192: accessCount = servletGet0.getResponseHeader("X-AccessCount");
193: count = Integer.parseInt(accessCount.getValue());
194: assertEquals("X-AccessCount ", 3, count);
195:
196: _sleep(DEFAULT_SLEEP);
197: // Reset the domain so that the cookie will be sent to server1
198: sessionID.setDomain(servers[1]);
199: state.addCookie(sessionID);
200: servletGet1 = new GetMethod(baseURL1
201: + "/dist-ss/StatefulSessionServlet/");
202: responseCode = httpConn.executeMethod(servletGet1
203: .getHostConfiguration(), servletGet1, state);
204: assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
205: accessCount = servletGet1.getResponseHeader("X-AccessCount");
206: count = Integer.parseInt(accessCount.getValue());
207: assertEquals("X-AccessCount ", 4, count);
208: }
209:
210: /**
211: * Sleep for specified time
212: *
213: * @param msecs
214: */
215: protected void _sleep(long msecs) {
216: try {
217: Thread.sleep(msecs);
218: } catch (InterruptedException e) {
219: e.printStackTrace();
220: }
221: }
222:
223: }
|