View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  package org.eclipse.jetty.server.session;
14  
15  import java.io.IOException;
16  import java.util.Random;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpSession;
23  
24  import org.eclipse.jetty.client.ContentExchange;
25  import org.eclipse.jetty.client.HttpClient;
26  import org.eclipse.jetty.http.HttpMethods;
27  import org.junit.Test;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  
31  
32  /**
33   * AbstractLastAccessTimeTest
34   */
35  public abstract class AbstractLastAccessTimeTest
36  {
37      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
38  
39      @Test
40      public void testLastAccessTime() throws Exception
41      {
42          Random random = new Random(System.nanoTime());
43  
44          String contextPath = "";
45          String servletMapping = "/server";
46          int maxInactivePeriod = 8;
47          int scavengePeriod = 2;
48          AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod);
49          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
50          server1.start();
51          int port1=server1.getPort();
52          try
53          {
54              AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod);
55              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
56              server2.start();
57              int port2=server2.getPort();
58              try
59              {
60                  HttpClient client = new HttpClient();
61                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
62                  client.start();
63                  try
64                  {
65                      // Perform one request to server1 to create a session
66                      ContentExchange exchange1 = new ContentExchange(true);
67                      exchange1.setMethod(HttpMethods.GET);
68                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
69                      client.send(exchange1);
70                      exchange1.waitForDone();
71                      assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
72                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
73                      assertTrue( sessionCookie != null );
74                      // Mangle the cookie, replacing Path with $Path, etc.
75                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
76  
77                      // Perform some request to server2 using the session cookie from the previous request
78                      // This should migrate the session from server1 to server2, and leave server1's
79                      // session in a very stale state, while server2 has a very fresh session.
80                      // We want to test that optimizations done to the saving of the shared lastAccessTime
81                      // do not break the correct working
82                      int requestInterval = 500;
83                      for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i)
84                      {
85                          ContentExchange exchange2 = new ContentExchange(true);
86                          exchange2.setMethod(HttpMethods.GET);
87                          exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping);
88                          exchange2.getRequestFields().add("Cookie", sessionCookie);
89                          client.send(exchange2);
90                          exchange2.waitForDone();
91                          assertEquals(HttpServletResponse.SC_OK , exchange2.getResponseStatus());
92                          String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
93                          if (setCookie!=null)                    
94                              sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
95                          
96                          Thread.sleep(requestInterval);
97                      }
98  
99                      System.out.println("Waiting for scavenging on node1...");
100 
101                     // At this point, session1 should be eligible for expiration.
102                     // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
103                     Thread.sleep(scavengePeriod * 2500L);
104 
105                     // Access again server1, and be sure we can
106                     exchange1 = new ContentExchange(true);
107                     exchange1.setMethod(HttpMethods.GET);
108                     exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
109                     exchange1.getRequestFields().add("Cookie", sessionCookie);
110                     client.send(exchange1);
111                     exchange1.waitForDone();
112                     assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus());
113                     
114                     // TODO shouldn't the session be expired????
115                 }
116                 finally
117                 {
118                     client.stop();
119                 }
120             }
121             finally
122             {
123                 server2.stop();
124             }
125         }
126         finally
127         {
128             server1.stop();
129         }
130     }
131 
132     public static class TestServlet extends HttpServlet
133     {
134         @Override
135         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
136         {
137             String action = request.getParameter("action");
138             if ("init".equals(action))
139             {
140                 HttpSession session = request.getSession(true);
141                 session.setAttribute("test", "test");
142             }
143             else
144             {
145                 HttpSession session = request.getSession(false);
146                 session.setAttribute("test", "test");
147             }
148         }
149     }
150 }