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  
29  /**
30   * AbstractLastAccessTimeTest
31   */
32  public abstract class AbstractLastAccessTimeTest
33  {
34      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
35  
36      @Test
37      public void testLastAccessTime() throws Exception
38      {
39          Random random = new Random(System.nanoTime());
40  
41          String contextPath = "";
42          String servletMapping = "/server";
43          int port1 = random.nextInt(50000) + 10000;
44          int maxInactivePeriod = 8;
45          int scavengePeriod = 2;
46          AbstractTestServer server1 = createServer(port1, maxInactivePeriod, scavengePeriod);
47          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
48          server1.start();
49          try
50          {
51              int port2 = random.nextInt(50000) + 10000;
52              AbstractTestServer server2 = createServer(port2, maxInactivePeriod, scavengePeriod);
53              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
54              server2.start();
55              try
56              {
57                  HttpClient client = new HttpClient();
58                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
59                  client.start();
60                  try
61                  {
62                      // Perform one request to server1 to create a session
63                      ContentExchange exchange1 = new ContentExchange(true);
64                      exchange1.setMethod(HttpMethods.GET);
65                      exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
66                      client.send(exchange1);
67                      exchange1.waitForDone();
68                      assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
69                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
70                      assert sessionCookie != null;
71                      // Mangle the cookie, replacing Path with $Path, etc.
72                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
73  
74                      // Perform some request to server2 using the session cookie from the previous request
75                      // This should migrate the session from server1 to server2, and leave server1's
76                      // session in a very stale state, while server2 has a very fresh session.
77                      // We want to test that optimizations done to the saving of the shared lastAccessTime
78                      // do not break the correct working
79                      int requestInterval = 500;
80                      for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i)
81                      {
82                          ContentExchange exchange2 = new ContentExchange(true);
83                          exchange2.setMethod(HttpMethods.GET);
84                          exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping);
85                          exchange2.getRequestFields().add("Cookie", sessionCookie);
86                          client.send(exchange2);
87                          exchange2.waitForDone();
88                          assert exchange2.getResponseStatus() == HttpServletResponse.SC_OK;
89  
90                          Thread.sleep(requestInterval);
91                      }
92  
93                      System.out.println("Waiting for scavenging on node1...");
94  
95                      // At this point, session1 should be eligible for expiration.
96                      // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
97                      Thread.sleep(scavengePeriod * 2500L);
98  
99                      // Access again server1, and be sure we can
100                     exchange1 = new ContentExchange(true);
101                     exchange1.setMethod(HttpMethods.GET);
102                     exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
103                     exchange1.getRequestFields().add("Cookie", sessionCookie);
104                     client.send(exchange1);
105                     exchange1.waitForDone();
106                     assert exchange1.getResponseStatus() == HttpServletResponse.SC_OK;
107                 }
108                 finally
109                 {
110                     client.stop();
111                 }
112             }
113             finally
114             {
115                 server2.stop();
116             }
117         }
118         finally
119         {
120             server1.stop();
121         }
122     }
123 
124     public static class TestServlet extends HttpServlet
125     {
126         @Override
127         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
128         {
129             String action = request.getParameter("action");
130             if ("init".equals(action))
131             {
132                 HttpSession session = request.getSession(true);
133                 session.setAttribute("test", "test");
134             }
135             else
136             {
137                 HttpSession session = request.getSession(false);
138                 session.setAttribute("test", "test");
139             }
140         }
141     }
142 }