From 121a8fce5f6003c556d042702ed1b19c1ca06233 Mon Sep 17 00:00:00 2001 From: rickardoberg Date: Fri, 7 Aug 2026 14:53:32 +0800 Subject: [PATCH 1/2] Fixed race in test --- .../servlet/DeferredAuthenticationTest.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java index bce343df8375..0dae93a6dda4 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java @@ -14,6 +14,8 @@ package org.eclipse.jetty.ee10.servlet; import java.security.Principal; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -38,12 +40,13 @@ import org.eclipse.jetty.util.security.Credential; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.RepeatedTest; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class DeferredAuthenticationTest { @@ -89,13 +92,14 @@ public void stopServer() throws Exception _server.stop(); } - @Test + @RepeatedTest(100) public void testWriteOnDeferredAuthentication() throws Exception { AtomicInteger authenticatorCount = new AtomicInteger(); AtomicReference authenticatedRef = new AtomicReference<>(); AtomicReference userPrincipalRef = new AtomicReference<>(); AtomicReference servletErrorRef = new AtomicReference<>(); + CountDownLatch latch = new CountDownLatch(1); startServer(new LoginAuthenticator() { @Override @@ -145,21 +149,29 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) { servletErrorRef.set(t); } + finally + { + latch.countDown(); + } } }); // Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails, // the second time for authenticate() which returns false and sends a 401 response. String response = _connector.getResponse("GET /public/foo HTTP/1.0\r\n\r\n"); + assertTrue(latch.await(5, TimeUnit.SECONDS)); assertThat(response, containsString("401 Unauthorized")); assertThat(response, containsString("this is a challenge")); assertThat(authenticatorCount.get(), equalTo(2)); + System.out.println(response); + System.out.println(authenticatedRef.get()); assertThat(authenticatedRef.get(), equalTo(false)); assertThat(userPrincipalRef.get(), equalTo(null)); assertThat(servletErrorRef.get(), equalTo(null)); // Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails, // the second time for authenticate() which returns true and sends a 200 response. +/* response = _connector.getResponse("GET /public/foo?authenticate HTTP/1.0\r\n\r\n"); assertThat(response, containsString("200 OK")); assertThat(response, containsString("success")); @@ -167,5 +179,6 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) assertThat(authenticatedRef.get(), equalTo(true)); assertThat(userPrincipalRef.get().getName(), equalTo("test-user")); assertThat(servletErrorRef.get(), equalTo(null)); +*/ } } \ No newline at end of file From 5f959bde4c088a33238dd5f4790752d4f4543f72 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 21 Aug 2026 17:02:29 +1000 Subject: [PATCH 2/2] Cleanups for DeferredAuthenticationTest Signed-off-by: Lachlan Roberts --- .../servlet/DeferredAuthenticationTest.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java index 0dae93a6dda4..0bbbd75ed82f 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/DeferredAuthenticationTest.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.ee10.servlet; import java.security.Principal; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -40,13 +39,13 @@ import org.eclipse.jetty.util.security.Credential; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; +import static org.awaitility.Awaitility.await; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; public class DeferredAuthenticationTest { @@ -92,14 +91,14 @@ public void stopServer() throws Exception _server.stop(); } - @RepeatedTest(100) + @Test public void testWriteOnDeferredAuthentication() throws Exception { AtomicInteger authenticatorCount = new AtomicInteger(); AtomicReference authenticatedRef = new AtomicReference<>(); AtomicReference userPrincipalRef = new AtomicReference<>(); AtomicReference servletErrorRef = new AtomicReference<>(); - CountDownLatch latch = new CountDownLatch(1); + AtomicInteger serviceCount = new AtomicInteger(); startServer(new LoginAuthenticator() { @Override @@ -151,7 +150,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) } finally { - latch.countDown(); + serviceCount.incrementAndGet(); } } }); @@ -159,26 +158,23 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) // Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails, // the second time for authenticate() which returns false and sends a 401 response. String response = _connector.getResponse("GET /public/foo HTTP/1.0\r\n\r\n"); - assertTrue(latch.await(5, TimeUnit.SECONDS)); + await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCount.get() == 1); assertThat(response, containsString("401 Unauthorized")); assertThat(response, containsString("this is a challenge")); assertThat(authenticatorCount.get(), equalTo(2)); - System.out.println(response); - System.out.println(authenticatedRef.get()); assertThat(authenticatedRef.get(), equalTo(false)); assertThat(userPrincipalRef.get(), equalTo(null)); assertThat(servletErrorRef.get(), equalTo(null)); // Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails, // the second time for authenticate() which returns true and sends a 200 response. -/* response = _connector.getResponse("GET /public/foo?authenticate HTTP/1.0\r\n\r\n"); + await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCount.get() == 2); assertThat(response, containsString("200 OK")); assertThat(response, containsString("success")); assertThat(authenticatorCount.get(), equalTo(4)); assertThat(authenticatedRef.get(), equalTo(true)); assertThat(userPrincipalRef.get().getName(), equalTo("test-user")); assertThat(servletErrorRef.get(), equalTo(null)); -*/ } } \ No newline at end of file