Issue #6138 Fix flakey DuplicateCookieTest (#6307) (#6315)

* Issue #6138 Fix flakey DuplicateCookieTest

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2021-05-25 11:12:57 +10:00 committed by GitHub
parent 6f148bb057
commit a5bd1ced8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 2 deletions

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.server.session;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@ -38,6 +39,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test having multiple session cookies in a request.
@ -59,6 +61,8 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
@ -75,6 +79,8 @@ public class DuplicateCookieTest
assertEquals(0, s4422.getRequests());
//make a request with another session cookie in there that does not exist
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=123"); //doesn't exist
request.header("Cookie", "JSESSIONID=4422"); //does exist
@ -82,6 +88,10 @@ public class DuplicateCookieTest
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("4422", response.getContentAsString());
//ensure request has finished processing so session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check session is drained of requests
assertEquals(0, s4422.getRequests());
}
finally
@ -106,6 +116,8 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
@ -132,6 +144,8 @@ public class DuplicateCookieTest
assertEquals(0, s2255.getRequests());
//make a request where the valid session cookie is first
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=1122"); //is valid
request.header("Cookie", "JSESSIONID=2233"); //is invalid
@ -140,6 +154,10 @@ public class DuplicateCookieTest
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("1122", response.getContentAsString());
//ensure request has finished processing so session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check valid session is drained of requests
assertEquals(0, s1122.getRequests());
}
finally
@ -164,6 +182,8 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
@ -190,6 +210,9 @@ public class DuplicateCookieTest
assertEquals(0, s2255.getRequests());
//make a request with the valid session cookie last
// Create the session
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=2233"); //is invalid
request.header("Cookie", "JSESSIONID=2255"); //is invalid
@ -198,6 +221,10 @@ public class DuplicateCookieTest
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("1122", response.getContentAsString());
//ensure request has completed so session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check valid session drained of requests
assertEquals(0, s1122.getRequests());
}
finally
@ -222,6 +249,8 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
@ -248,6 +277,8 @@ public class DuplicateCookieTest
assertEquals(0, s2255.getRequests());
//make a request with another session cookie with the valid session surrounded by invalids
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=2233"); //is invalid
request.header("Cookie", "JSESSIONID=1122"); //is valid
@ -256,6 +287,10 @@ public class DuplicateCookieTest
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("1122", response.getContentAsString());
//ensure request has completed so session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check valid session drained of requests
assertEquals(0, s1122.getRequests());
}
finally
@ -280,12 +315,14 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session")))
{
//create some of unexpired sessions
//create some unexpired sessions
Session s1234 = createUnExpiredSession(contextHandler.getSessionHandler().getSessionCache(),
contextHandler.getSessionHandler().getSessionCache().getSessionDataStore(),
"1234");
@ -305,13 +342,18 @@ public class DuplicateCookieTest
assertEquals(0, s9111.getRequests());
//make a request with multiple valid session ids
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=1234");
request.header("Cookie", "JSESSIONID=5678");
ContentResponse response = request.send();
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
//check that all valid sessions have their request counts decremented correctly after the request, back to 0
//ensure request has completed so any session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check that all sessions have their request counts decremented correctly after the request, back to 0
assertEquals(0, s1234.getRequests());
assertEquals(0, s5678.getRequests());
assertEquals(0, s9111.getRequests());
@ -338,6 +380,8 @@ public class DuplicateCookieTest
ServletHolder holder = new ServletHolder(servlet);
ServletContextHandler contextHandler = server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
server1.getServerConnector().addBean(scopeListener);
server1.start();
int port1 = server1.getPort();
@ -355,12 +399,17 @@ public class DuplicateCookieTest
assertEquals(0, s1234.getRequests());
//make a request with multiple valid session ids
CountDownLatch latch = new CountDownLatch(1);
scopeListener.setExitSynchronizer(latch);
Request request = client.newRequest("http://localhost:" + port1 + contextPath + servletMapping + "?action=check");
request.header("Cookie", "JSESSIONID=1234");
request.header("Cookie", "JSESSIONID=1234");
ContentResponse response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//ensure request has finished processing so session will be completed
assertTrue(latch.await(5, TimeUnit.SECONDS));
//check that all valid sessions have their request counts decremented correctly after the request, back to 0
assertEquals(0, s1234.getRequests());
}