View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.session;
20  
21  import java.io.IOException;
22  import java.util.Random;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.servlet.http.HttpSession;
29  
30  import org.eclipse.jetty.client.ContentExchange;
31  import org.eclipse.jetty.client.HttpClient;
32  import org.eclipse.jetty.http.HttpMethods;
33  import org.eclipse.jetty.server.Request;
34  import org.eclipse.jetty.server.SessionManager;
35  import org.junit.Test;
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   * AbstractLocalSessionScavengingTest
41   */
42  public abstract class AbstractLocalSessionScavengingTest
43  {
44      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
45  
46      public void pause(int scavengePeriod)
47      {
48          try
49          {
50              Thread.sleep(scavengePeriod * 2500L);
51          }
52          catch (InterruptedException e)
53          {
54              e.printStackTrace();
55          }
56      }
57      
58      @Test
59      public void testLocalSessionsScavenging() throws Exception
60      {
61          String contextPath = "";
62          String servletMapping = "/server";
63          int inactivePeriod = 1;
64          int scavengePeriod = 2;
65          AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod);
66          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
67          server1.start();
68          int port1 = server1.getPort();
69          try
70          {
71              AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod * 3);
72              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
73              server2.start();
74              int port2 = server2.getPort();
75              try
76              {
77                  HttpClient client = new HttpClient();
78                  client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
79                  client.start();
80                  try
81                  {
82                      String[] urls = new String[2];
83                      urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
84                      urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
85  
86                      // Create the session on node1
87                      ContentExchange exchange1 = new ContentExchange(true);
88                      exchange1.setMethod(HttpMethods.GET);
89                      exchange1.setURL(urls[0] + "?action=init");
90                      client.send(exchange1);
91                      exchange1.waitForDone();
92                      assertEquals(HttpServletResponse.SC_OK,exchange1.getResponseStatus());
93                      String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie");
94                      assertTrue(sessionCookie != null);
95                      // Mangle the cookie, replacing Path with $Path, etc.
96                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
97  
98                      // Be sure the session is also present in node2
99                      ContentExchange exchange2 = new ContentExchange(true);
100                     exchange2.setMethod(HttpMethods.GET);
101                     exchange2.setURL(urls[1] + "?action=test");
102                     exchange2.getRequestFields().add("Cookie", sessionCookie);
103                     client.send(exchange2);
104                     exchange2.waitForDone();
105                     assertEquals(HttpServletResponse.SC_OK,exchange2.getResponseStatus());
106                     
107                     
108                     // Wait for the scavenger to run on node1, waiting 2.5 times the scavenger period
109                     pause(scavengePeriod);
110                     
111                     // Check that node1 does not have any local session cached
112                     exchange1 = new ContentExchange(true);
113                     exchange1.setMethod(HttpMethods.GET);
114                     exchange1.setURL(urls[0] + "?action=check");
115                     client.send(exchange1);
116                     exchange1.waitForDone();
117                     assertEquals(HttpServletResponse.SC_OK,exchange1.getResponseStatus());
118 
119 
120                     // Wait for the scavenger to run on node2, waiting 2 times the scavenger period
121                     // This ensures that the scavenger on node2 runs at least once.
122                     pause(scavengePeriod);
123 
124                     // Check that node2 does not have any local session cached   
125                     exchange2 = new ContentExchange(true);
126                     exchange2.setMethod(HttpMethods.GET);
127                     exchange2.setURL(urls[1] + "?action=check");
128                     client.send(exchange2);
129                     exchange2.waitForDone();
130                     assertEquals(HttpServletResponse.SC_OK,exchange2.getResponseStatus());
131                 }
132                 finally
133                 {
134                     client.stop();
135                 }
136             }
137             finally
138             {
139                 server2.stop();
140             }
141         }
142         finally
143         {
144             server1.stop();
145         }
146     }
147 
148     public static class TestServlet extends HttpServlet
149     {
150         private SessionManager sessionManager;
151 
152         @Override
153         protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
154         {
155             String action = request.getParameter("action");
156             if ("init".equals(action))
157             {
158                 HttpSession session = request.getSession(true);
159                 session.setAttribute("test", "test");
160                 this.sessionManager = ((Request)request).getSessionManager();
161             }
162             else if ("test".equals(action))
163             {
164                 HttpSession session = request.getSession(false);
165                 session.setAttribute("test", "test");
166                 this.sessionManager = ((Request)request).getSessionManager();
167             }
168             else if ("check".equals(action))
169             {
170                 HttpSession session = request.getSession(false);
171                 assertTrue(session == null);
172             }
173         }
174     }
175 }