[jboss-cvs] JBossAS SVN: r62446 - in branches/JBoss_4_0_1_SP1_CP: testsuite/src/resources/cluster/http and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Apr 20 14:39:10 EDT 2007


Author: fnasser at redhat.com
Date: 2007-04-20 14:39:10 -0400 (Fri, 20 Apr 2007)
New Revision: 62446

Added:
   branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/BaseTest.java
   branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/SimpleTestCase.java
Modified:
   branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/getattribute.jsp
   branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/web.xml
   branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/ClusteredSession.java
   branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/JBossCacheManager.java
Log:
Merge of JBAS-2456 one-off branch (ASPATCH-198)

Added: branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/BaseTest.java
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/BaseTest.java	                        (rev 0)
+++ branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/BaseTest.java	2007-04-20 18:39:10 UTC (rev 62446)
@@ -0,0 +1,229 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.cluster.test;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.jboss.test.JBossClusteredTestCase;
+
+/**
+ * Base class to test HttpSessionReplication.
+ *
+ * @author Ben Wang
+ * @version $Revision: 1.0
+ */
+public abstract class BaseTest
+      extends JBossClusteredTestCase
+{
+   public static final long DEFAULT_SLEEP = 300;
+   
+   protected String[] servers_ = null;
+   protected String baseURL0_;
+   protected String baseURL1_;
+
+   public BaseTest(String name)
+   {
+      super(name);
+   }
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+
+      servers_ = super.getServers();
+      assertEquals("Server size " , servers_.length, 2);
+
+      String[] httpURLs  = super.getHttpURLs();
+      assertEquals("Url size " , httpURLs.length, 2);
+      baseURL0_ = httpURLs[0];
+      baseURL1_ = baseURL0_;
+      if( servers_.length > 1 )
+      {
+        baseURL1_ = httpURLs[1];
+      }
+   }
+
+   /**
+    * Sleep for specified time
+    *
+    * @param msecs
+    */
+   protected void sleepThread(long msecs)
+   {
+      try {
+         Thread.sleep(msecs);
+      } catch (InterruptedException e) {
+         e.printStackTrace();
+      }
+   }
+
+
+   /**
+    * Makes a http call to the jsp that retrieves the attribute stored on the
+    * session. When the attribute values mathes with the one retrieved earlier,
+    * we have HttpSessionReplication.
+    * Makes use of commons-httpclient library of Apache
+    *
+    * @param client
+    * @param url
+    * @return session attribute
+    */
+   protected String makeGet(HttpClient client, String url)
+   {
+      getLog().debug("makeGet(): trying to get from url " +url);
+
+      GetMethod method = new GetMethod(url);
+      int responseCode = 0;
+      try
+      {
+         responseCode = client.executeMethod(method);
+      } catch (IOException e)
+      {
+         e.printStackTrace();
+         fail("HttpClient executeMethod fails." +e.toString());
+      }
+      assertTrue("Get OK with url: " +url + " responseCode: " +responseCode
+        , responseCode == HttpURLConnection.HTTP_OK);
+
+      // Read the response body.
+      byte[] responseBody = method.getResponseBody();
+
+      // Release the connection.
+//      method.releaseConnection();
+
+      // Deal with the response.
+      // Use caution: ensure correct character encoding and is not binary data
+      return new String(responseBody);
+   }
+
+   /**
+    * Makes a http call to the jsp that retrieves the attribute stored on the
+    * session. When the attribute values mathes with the one retrieved earlier,
+    * we have HttpSessionReplication.
+    * Makes use of commons-httpclient library of Apache
+    *
+    * @param client
+    * @param url
+    * @return session attribute
+    */
+   protected String makeGetFailed(HttpClient client, String url)
+   {
+      getLog().debug("makeGetailed(): trying to get from url " +url);
+
+      GetMethod method = new GetMethod(url);
+      int responseCode = 0;
+      try
+      {
+         responseCode = client.executeMethod(method);
+      } catch (IOException e)
+      {
+         e.printStackTrace();
+      }
+      assertTrue("Should not be OK code with url: " +url + " responseCode: " +responseCode
+        , responseCode != HttpURLConnection.HTTP_OK);
+
+      // Read the response body.
+      byte[] responseBody = method.getResponseBody();
+
+      // Release the connection.
+//      method.releaseConnection();
+
+      // Deal with the response.
+      // Use caution: ensure correct character encoding and is not binary data
+      return new String(responseBody);
+   }
+
+
+   /**
+    * Makes a http call to the jsp that retrieves the attribute stored on the
+    * session. When the attribute values mathes with the one retrieved earlier,
+    * we have HttpSessionReplication.
+    * Makes use of commons-httpclient library of Apache
+    *
+    * @param client
+    * @param url
+    * @return session attribute
+    */
+   protected String makeGetWithState(HttpClient client, String url)
+   {
+      GetMethod method = new GetMethod(url);
+      int responseCode = 0;
+      try
+      {
+         HttpState state = client.getState();
+         responseCode = client.executeMethod(method.getHostConfiguration(),
+            method, state);
+      } catch (IOException e)
+      {
+         e.printStackTrace();
+         fail("HttpClient executeMethod fails." +e.toString());
+      }
+      assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
+
+      // Read the response body.
+      byte[] responseBody = method.getResponseBody();
+      /* Validate that the attribute was actually seen. An absence of the
+         header is treated as true since there are pages used that done't
+         add it.
+      */
+      Header hdr = method.getResponseHeader("X-SawTestHttpAttribute");
+      Boolean sawAttr = hdr != null ? Boolean.valueOf(hdr.getValue()) : Boolean.TRUE;
+      String attr = null;
+      if( sawAttr.booleanValue() )
+         attr = new String(responseBody);
+      // Release the connection.
+//      method.releaseConnection();
+
+      // Deal with the response.
+      // Use caution: ensure correct character encoding and is not binary data
+      return attr;
+   }
+
+   protected void setCookieDomainToThisServer(HttpClient client, String server)
+   {
+      // Get the state for the JSESSIONID
+      HttpState state = client.getState();
+      // Get the JSESSIONID so we can reset the host
+      Cookie[] cookies = state.getCookies();
+      Cookie sessionID = null;
+      for(int c = 0; c < cookies.length; c ++)
+      {
+         Cookie k = cookies[c];
+         if( k.getName().equalsIgnoreCase("JSESSIONID") )
+            sessionID = k;
+      }
+      if(sessionID == null)
+      {
+         fail("setCookieDomainToThisServer(): fail to find session id. Server name: " +server);
+      }
+      log.info("Saw JSESSIONID="+sessionID);
+      // Reset the domain so that the cookie will be sent to server1
+      sessionID.setDomain(server);
+      state.addCookie(sessionID);
+   }
+}

Added: branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/SimpleTestCase.java
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/SimpleTestCase.java	                        (rev 0)
+++ branches/JBoss_4_0_1_SP1_CP/testsuite/src/main/org/jboss/test/cluster/test/SimpleTestCase.java	2007-04-20 18:39:10 UTC (rev 62446)
@@ -0,0 +1,164 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.cluster.test;
+
+import junit.framework.Test;
+import org.apache.commons.httpclient.HttpClient;
+import org.jboss.test.JBossClusteredTestCase;
+
+/**
+ * Simple clustering test case of get/set.
+ *
+ * @author Ben Wang
+ * @version $Revision: 1.0
+ */
+public class SimpleTestCase
+      extends BaseTest
+{
+
+   public SimpleTestCase(String name)
+   {
+      super(name);
+
+   }
+
+   public static Test suite() throws Exception
+   {
+      Test t1 = JBossClusteredTestCase.getDeploySetup(SimpleTestCase.class,
+            "http-sr.war");
+      return t1;
+   }
+
+   /**
+    * Main method that deals with the Http Session Replication Test
+    *
+    * @throws Exception
+    */
+   public void testHttpSessionReplication()
+         throws Exception
+   {
+      String attr = "";
+      getLog().debug("Enter testHttpSessionReplication");
+
+      String setURLName = "/http-sr/testsessionreplication.jsp";
+      String getURLName = "/http-sr/getattribute.jsp";
+
+      getLog().debug(setURLName + ":::::::" + getURLName);
+
+      // Create an instance of HttpClient.
+      HttpClient client = new HttpClient();
+
+      // Set the session attribute first
+      makeGet(client, baseURL0_ +setURLName);
+
+      // Get the Attribute set by testsessionreplication.jsp
+      attr = makeGetWithState(client, baseURL0_ +getURLName);
+
+      sleepThread(DEFAULT_SLEEP);
+
+      // Let's switch to server 2 to retrieve the session attribute.
+      setCookieDomainToThisServer(client, servers_[1]);
+      String attr2 = makeGet(client, baseURL1_ +getURLName);
+
+      // Check the result
+      assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2);
+
+      getLog().debug("Http Session Replication has happened");
+      getLog().debug("Exit testHttpSessionReplication");
+   }
+
+   public void testSessionTimeout()
+      throws Exception
+   {
+      String attr = "";
+      getLog().debug("Enter testSessionTimeout");
+
+      String setURLName = "/http-sr/testsessionreplication.jsp";
+      String getURLName = "/http-sr/getattribute.jsp";
+
+      getLog().debug(setURLName + ":::::::" + getURLName);
+
+      // Create an instance of HttpClient.
+      HttpClient client = new HttpClient();
+
+      // Set the session attribute first
+      makeGet(client, baseURL0_ +setURLName);
+
+      // Get the Attribute set by testsessionreplication.jsp
+      attr = makeGetWithState(client, baseURL0_ +getURLName);
+      assertNotNull("Http session get", attr);
+      sleepThread(65000);  // sleep for 65 seconds for session to expire
+      attr = makeGetWithState(client, baseURL0_ +getURLName);
+      if (attr == null)
+    	  return;  // success
+      // Wait some more as the expiration thread only runs once a minute
+      sleepThread(60000);
+      attr = makeGetWithState(client, baseURL0_ +getURLName);
+      assertNull("Http session not available", attr);
+   }
+
+   /**
+    * Test for JBAS-2403, ensuring that session activity on one node in
+    * the cluster prevents timeout of the session on another node.
+    * 
+    * @throws Exception
+    */
+   public void testReplicationPreventsTimeout()
+      throws Exception
+   {
+      String attr  = "";
+      String attr2 = "";
+      getLog().debug("Enter testReplicationPreventsTimeout");
+
+      String setURLName = "/http-sr/testsessionreplication.jsp";
+      String getURLName = "/http-sr/getattribute.jsp";
+
+      getLog().debug(setURLName + ":::::::" + getURLName);
+
+      // Create an instance of HttpClient.
+      HttpClient client = new HttpClient();
+
+      // Set the session attribute first
+      makeGet(client, baseURL0_ +setURLName);
+      
+      sleepThread(58000);
+      // Set it again to ensure replication occurs
+      attr = makeGetWithState(client, baseURL0_ +setURLName);
+      // Get the Attribute set by testsessionreplication.jsp
+      attr = makeGetWithState(client, baseURL0_ +getURLName);
+      assertNotNull("Http session get", attr);
+      
+      // Sleep 58 secs.  This plus the previous 58 secs is enough to
+      // expire the session on the other node if replication didn't
+      sleepThread(58000);
+      
+      // Switch to the other server and check the attribute
+      setCookieDomainToThisServer(client, servers_[1]);
+      attr2 = makeGetWithState(client, baseURL1_ +getURLName);
+      
+      // Check the result
+      assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2);
+      
+      getLog().debug("Replication has kept the session alive");
+      getLog().debug("Exit testReplicationPreventsTimeout");
+   }
+}

Modified: branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/getattribute.jsp
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/getattribute.jsp	2007-04-20 18:19:09 UTC (rev 62445)
+++ branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/getattribute.jsp	2007-04-20 18:39:10 UTC (rev 62446)
@@ -1 +1,7 @@
-<%=session.getAttribute("TEST_HTTP") %>
+<%
+   // Use a custom header to indicate if the session attribute was seen
+   String TEST_HTTP = (String) session.getAttribute("TEST_HTTP");
+   String flag = TEST_HTTP != null ? "true" : "false";
+   response.setHeader("X-SawTestHttpAttribute", flag);
+%>
+<%= TEST_HTTP %></p>
\ No newline at end of file

Modified: branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/web.xml
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/web.xml	2007-04-20 18:19:09 UTC (rev 62445)
+++ branches/JBoss_4_0_1_SP1_CP/testsuite/src/resources/cluster/http/web.xml	2007-04-20 18:39:10 UTC (rev 62446)
@@ -10,4 +10,10 @@
   <description>
      Welcome to JBoss
   </description>
+
+
+<!-- timeout is set to 60 seconds -->
+  <session-config>
+     <session-timeout>1</session-timeout>
+  </session-config>
 </web-app>

Modified: branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/ClusteredSession.java
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/ClusteredSession.java	2007-04-20 18:19:09 UTC (rev 62445)
+++ branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/ClusteredSession.java	2007-04-20 18:39:10 UTC (rev 62446)
@@ -294,8 +294,60 @@
       }
 
    }
+   
+  
+   /**
+    * Override the {@link StandardSession#isValid() superclass method}
+    * to call {@ #isValid(boolean) isValid(true)}.
+    */
+   public boolean isValid()
+   {
+      return isValid(true);
+   }
+   
+   /**
+    * Returns whether the current session is still valid, but
+    * only calls {@link #expire(boolean)} for timed-out sessions
+    * if <code>expireIfInvalid</code> is <code>true</code>.
+    * 
+    * @param expireIfInvalid  <code>true</code> if sessions that have
+    *                         been timed out should be expired
+    */
+   public boolean isValid(boolean expireIfInvalid)
+   {
+      if (this.expiring)
+      {
+         return true;
+      }
 
+      if (!this.isValid)
+      {
+         return false;
+      }
 
+      if (accessCount > 0)
+      {
+         return true;
+      }
+
+      if (maxInactiveInterval >= 0)
+      {
+         long timeNow = System.currentTimeMillis();
+         int timeIdle = (int) ((timeNow - thisAccessedTime) / 1000L);
+         if (timeIdle >= maxInactiveInterval)
+         {
+            if (expireIfInvalid)
+               expire(true);
+            else
+               return false;
+         }
+      }
+
+      return (this.isValid);
+      
+   }  
+
+
    /**
     * Override here to reverse the order of manager session removal and
     * attribute removal.

Modified: branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/JBossCacheManager.java
===================================================================
--- branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/JBossCacheManager.java	2007-04-20 18:19:09 UTC (rev 62445)
+++ branches/JBoss_4_0_1_SP1_CP/tomcat/src/main/org/jboss/web/tomcat/tc5/session/JBossCacheManager.java	2007-04-20 18:39:10 UTC (rev 62446)
@@ -524,7 +524,16 @@
                continue;
             }
 
-            // We only look at valid sessions. This will remove session if not valid already.
+            // JBAS-2403. Check for outdated sessions where we think
+            // the local copy has timed out.  If found, refresh the
+            // session from the cache in case that might change the timeout
+            if (session.isOutdated() && session.isValid(false) == false) {
+               String realId = getRealId(session.getId());
+               session = (ClusteredSession) loadSession(realId);
+            }
+               
+            // Do a normal invalidation check that will expire any
+            // sessions that have timed out. This will remove session if not valid.
             if (!session.isValid()) continue;
          }
       }




More information about the jboss-cvs-commits mailing list