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.http.HttpMethods;
25  import org.eclipse.jetty.client.ContentExchange;
26  import org.eclipse.jetty.client.HttpClient;
27  import org.testng.annotations.Test;
28  
29  /**
30   * AbstractLastAccessTimeTest
31   *
32   *
33   */
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 port1 = random.nextInt(50000) + 10000;
47          int maxInactivePeriod = 8;
48          int scavengePeriod = 2;
49          AbstractTestServer server1 = createServer(port1, maxInactivePeriod, scavengePeriod);
50          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
51          server1.start();
52          try
53          {
54              int port2 = random.nextInt(50000) + 10000;
55              AbstractTestServer server2 = createServer(port2, maxInactivePeriod, scavengePeriod);
56              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
57              server2.start();
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                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
72                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
73                      assert 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                          assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
92  
93                          Thread.sleep(requestInterval);
94                      }
95  
96                      System.out.println("Waiting for scavenging on node1...");
97  
98                      // At this point, session1 should be eligible for expiration.
99                      // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
100                     Thread.sleep(scavengePeriod * 2500L);
101 
102                     // Access again server1, and be sure we can
103                     exchange1 = new ContentExchange(true);
104                     exchange1.setMethod(HttpMethods.GET);
105                     exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
106                     exchange1.getRequestFields().add("Cookie", sessionCookie);
107                     client.send(exchange1);
108                     exchange1.waitForDone();
109                     assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
110                 }
111                 finally
112                 {
113                     client.stop();
114                 }
115             }
116             finally
117             {
118                 server2.stop();
119             }
120         }
121         finally
122         {
123             server1.stop();
124         }
125     }
126 
127     public static class TestServlet extends HttpServlet
128     {
129         @Override
130         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
131         {
132             String action = request.getParameter("action");
133             if ("init".equals(action))
134             {
135                 HttpSession session = request.getSession(true);
136                 session.setAttribute("test", "test");
137             }
138             else
139             {
140                 HttpSession session = request.getSession(false);
141                 session.setAttribute("test", "test");
142             }
143         }
144     }
145 }