View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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 static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.concurrent.Future;
26  import java.util.concurrent.TimeUnit;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpSession;
33  
34  import org.eclipse.jetty.client.HttpClient;
35  import org.eclipse.jetty.client.api.ContentResponse;
36  import org.eclipse.jetty.client.api.Request;
37  import org.junit.Test;
38  
39  /**
40   * AbstractOrphanedSessionTest
41   */
42  public abstract class AbstractOrphanedSessionTest
43  {
44  
45      public abstract AbstractTestServer createServer(int port, int max, int scavenge);
46  
47      /**
48       * If nodeA creates a session, and just afterwards crashes, it is the only node that knows about the session.
49       * We want to test that the session data is gone after scavenging.
50       */
51      @Test
52      public void testOrphanedSession() throws Exception
53      {
54          // Disable scavenging for the first server, so that we simulate its "crash".
55          String contextPath = "";
56          String servletMapping = "/server";
57          int inactivePeriod = 5;
58          AbstractTestServer server1 = createServer(0, inactivePeriod, -1);
59          server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
60          server1.start();
61          int port1 = server1.getPort();
62          try
63          {
64              int scavengePeriod = 2;
65              AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod);
66              server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
67              server2.start();
68              int port2 = server2.getPort();
69              try
70              {
71                  HttpClient client = new HttpClient();
72                  client.start();
73                  try
74                  {
75                      // Connect to server1 to create a session and get its session cookie
76                      Future<ContentResponse> future = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init");
77                      ContentResponse response1 = future.get();
78                      assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
79                      String sessionCookie = response1.getHeaders().getStringField("Set-Cookie");
80                      assertTrue(sessionCookie != null);
81                      // Mangle the cookie, replacing Path with $Path, etc.
82                      sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
83  
84                      // Wait for the session to expire.
85                      // The first node does not do any scavenging, but the session
86                      // must be removed by scavenging done in the other node.
87                      Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
88  
89                      // Perform one request to server2 to be sure that the session has been expired
90                      Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");
91                      request.header("Cookie", sessionCookie);
92                      future = request.send();
93                      ContentResponse response2 = future.get();
94                      assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
95                  }
96                  finally
97                  {
98                      client.stop();
99                  }
100             }
101             finally
102             {
103                 server2.stop();
104             }
105         }
106         finally
107         {
108             server1.stop();
109         }
110     }
111 
112     public static class TestServlet extends HttpServlet
113     {
114         @Override
115         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
116         {
117             String action = request.getParameter("action");
118             if ("init".equals(action))
119             {
120                 HttpSession session = request.getSession(true);
121                 session.setAttribute("A", "A");
122             }
123             else if ("remove".equals(action))
124             {
125                 HttpSession session = request.getSession(false);
126                 session.invalidate();
127                 //assertTrue(session == null);
128             }
129             else if ("check".equals(action))
130             {
131                 HttpSession session = request.getSession(false);
132                 assertTrue(session == null);
133             }
134         }
135     }
136 }