From 138a273705115e7661d44e4270b1a1a168d9760a Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:11:01 +0000 Subject: [PATCH 1/7] fix: send push notifications as SSE events on the SSE transport Atmosphere frames only writes to a suspended resource, so session expired, UI not found and refresh notifications reached the client as a bare JSON line that EventSource ignores. Fixes #27189 --- .../server/communication/PushHandler.java | 43 ++++++++++++++++-- .../server/communication/PushHandlerTest.java | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java index 7658f51d80c..9614f6c5547 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java @@ -15,7 +15,10 @@ */ package com.vaadin.flow.server.communication; +import jakarta.servlet.ServletResponse; + import java.io.IOException; +import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -34,6 +37,7 @@ import org.atmosphere.cpr.AtmosphereResource.TRANSPORT; import org.atmosphere.cpr.AtmosphereResourceEvent; import org.atmosphere.cpr.AtmosphereResourceImpl; +import org.atmosphere.cpr.HeaderConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tools.jackson.core.JacksonException; @@ -62,6 +66,7 @@ import com.vaadin.flow.server.startup.ApplicationConfiguration; import com.vaadin.flow.shared.ApplicationConstants; import com.vaadin.flow.shared.JsonConstants; +import com.vaadin.flow.shared.communication.PushConstants; import com.vaadin.flow.shared.communication.PushMode; /** @@ -75,6 +80,8 @@ */ public class PushHandler { + private static final String SSE_CONTENT_TYPE = "text/event-stream"; + private int longPollingSuspendTimeout = -1; /** @@ -657,9 +664,13 @@ private static void sendNotificationAndDisconnect( "sendNotificationAndDisconnect called for resource no longer in scope"); return; } - resource.getResponse() - .setContentType(JsonConstants.JSON_CONTENT_TYPE); - resource.getResponse().getWriter().write(notificationJson); + if (resource.transport() == TRANSPORT.SSE) { + writeServerSentEvent(resource, notificationJson); + } else { + resource.getResponse() + .setContentType(JsonConstants.JSON_CONTENT_TYPE); + resource.getResponse().getWriter().write(notificationJson); + } resource.resume(); } catch (Exception e) { getLogger().trace("Failed to send critical notification to client", @@ -667,6 +678,32 @@ private static void sendNotificationAndDisconnect( } } + /** + * Writes the message as a server-sent event, so that the client's + * {@code EventSource} receives it. + *

+ * Atmosphere's SSE interceptor only adds the event framing to writes on a + * suspended resource, and a notification is usually sent before the + * resource is suspended. The event is therefore written to the container + * response, past Atmosphere's interceptors, with the length prefix that + * Atmosphere's message size tracking would otherwise add. + */ + private static void writeServerSentEvent(AtmosphereResource resource, + String message) throws IOException { + String data = message; + if (Boolean.parseBoolean(resource.getRequest() + .getHeader(HeaderConfig.X_ATMOSPHERE_TRACKMESSAGESIZE))) { + data = message.length() + + String.valueOf(PushConstants.MESSAGE_DELIMITER) + message; + } + ServletResponse response = resource.getResponse().getResponse(); + response.setContentType(SSE_CONTENT_TYPE); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + PrintWriter writer = response.getWriter(); + writer.write("data:" + data + "\n\n"); + writer.flush(); + } + private static Logger getLogger() { return LoggerFactory.getLogger(PushHandler.class.getName()); } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java index 352edebe081..4fcbb9d5ab1 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java @@ -15,10 +15,13 @@ */ package com.vaadin.flow.server.communication; +import jakarta.servlet.ServletResponse; + import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; +import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -33,7 +36,10 @@ import org.atmosphere.cpr.AtmosphereResourceEvent; import org.atmosphere.cpr.AtmosphereResourceImpl; import org.atmosphere.cpr.AtmosphereResponse; +import org.atmosphere.cpr.HeaderConfig; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import com.vaadin.flow.component.UI; import com.vaadin.flow.di.Instantiator; @@ -692,6 +698,44 @@ public VaadinSession findVaadinSession(VaadinRequest request) { .write(VaadinService.createSessionExpiredJSON(true)); } + @ParameterizedTest + @ValueSource(booleans = { true, false }) + void onConnect_noSessionForRequest_sse_sessionExpiredNotificationSentAsEvent( + boolean trackMessageSize) throws Exception { + MockVaadinServletService service = new MockVaadinServletService() { + @Override + public VaadinSession findVaadinSession(VaadinRequest request) { + return null; + } + }; + setProductionMode(service, false); + AtomicReference res = new AtomicReference<>(); + ServletResponse containerResponse = mock(ServletResponse.class); + StringWriter written = new StringWriter(); + when(containerResponse.getWriter()) + .thenReturn(new PrintWriter(written)); + + runTest(service, (handler, resource) -> { + when(resource.transport()).thenReturn(TRANSPORT.SSE); + when(resource.getRequest() + .getHeader(HeaderConfig.X_ATMOSPHERE_TRACKMESSAGESIZE)) + .thenReturn(String.valueOf(trackMessageSize)); + when(resource.getResponse().getResponse()) + .thenReturn(containerResponse); + handler.onConnect(resource); + res.set(resource); + }); + + // EventSource ignores anything that is not framed as an event, and + // Atmosphere frames only writes to a suspended resource + String json = VaadinService.createSessionExpiredJSON(true); + String data = trackMessageSize ? json.length() + "|" + json : json; + assertEquals("data:" + data + "\n\n", written.toString()); + verify(containerResponse).setContentType("text/event-stream"); + verify(res.get().getResponse(), never()).getWriter(); + verify(res.get()).resume(); + } + @Test void connectionLost_noSessionForRequest_noFailure() { MockVaadinServletService service = new MockVaadinServletService() { From f620638383c4097d3d6e2df1c4bbf2034978ae76 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:12:45 +0000 Subject: [PATCH 2/7] fix: keep Atmosphere framing for open SSE connections --- .../server/communication/PushHandler.java | 15 ++++++----- .../server/communication/PushHandlerTest.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java index 9614f6c5547..4a034f8f010 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java @@ -664,7 +664,8 @@ private static void sendNotificationAndDisconnect( "sendNotificationAndDisconnect called for resource no longer in scope"); return; } - if (resource.transport() == TRANSPORT.SSE) { + if (resource.transport() == TRANSPORT.SSE + && !resource.isSuspended()) { writeServerSentEvent(resource, notificationJson); } else { resource.getResponse() @@ -682,11 +683,13 @@ private static void sendNotificationAndDisconnect( * Writes the message as a server-sent event, so that the client's * {@code EventSource} receives it. *

- * Atmosphere's SSE interceptor only adds the event framing to writes on a - * suspended resource, and a notification is usually sent before the - * resource is suspended. The event is therefore written to the container - * response, past Atmosphere's interceptors, with the length prefix that - * Atmosphere's message size tracking would otherwise add. + * Only for a resource that is not suspended yet, for example when the + * connection is rejected right away. Atmosphere's SSE interceptor adds the + * event framing only to writes on a suspended resource, so the event is + * written to the container response, past Atmosphere's interceptors, with + * the length prefix that Atmosphere's message size tracking would otherwise + * add. Writes to an open connection go through Atmosphere, which frames + * them. */ private static void writeServerSentEvent(AtmosphereResource resource, String message) throws IOException { diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java index 4fcbb9d5ab1..f7140c5ed96 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java @@ -732,10 +732,36 @@ public VaadinSession findVaadinSession(VaadinRequest request) { String data = trackMessageSize ? json.length() + "|" + json : json; assertEquals("data:" + data + "\n\n", written.toString()); verify(containerResponse).setContentType("text/event-stream"); + verify(containerResponse).setCharacterEncoding("UTF-8"); verify(res.get().getResponse(), never()).getWriter(); verify(res.get()).resume(); } + @Test + void onConnect_noSessionForRequest_suspendedSse_notificationWrittenThroughAtmosphere() + throws Exception { + MockVaadinServletService service = new MockVaadinServletService() { + @Override + public VaadinSession findVaadinSession(VaadinRequest request) { + return null; + } + }; + setProductionMode(service, false); + AtomicReference res = new AtomicReference<>(); + + runTest(service, (handler, resource) -> { + when(resource.transport()).thenReturn(TRANSPORT.SSE); + when(resource.isSuspended()).thenReturn(true); + handler.onConnect(resource); + res.set(resource); + }); + + // Atmosphere frames writes to an open connection itself + verify(res.get().getResponse().getWriter()) + .write(VaadinService.createSessionExpiredJSON(true)); + verify(res.get().getResponse(), never()).getResponse(); + } + @Test void connectionLost_noSessionForRequest_noFailure() { MockVaadinServletService service = new MockVaadinServletService() { From 2166e93ec30c858438b5451af16931fdbc756ac6 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:21:36 +0000 Subject: [PATCH 3/7] refactor: let Atmosphere frame SSE notifications Instead of writing the event to the container response, mark the request the way Atmosphere marks a connection handshake, so its SSE interceptor adds the framing, padding and length prefix itself. --- .../server/communication/PushHandler.java | 51 +++----------- .../server/communication/PushHandlerTest.java | 68 ++++++------------- 2 files changed, 31 insertions(+), 88 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java index 4a034f8f010..0f9613bd21e 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/PushHandler.java @@ -15,10 +15,7 @@ */ package com.vaadin.flow.server.communication; -import jakarta.servlet.ServletResponse; - import java.io.IOException; -import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.nio.charset.StandardCharsets; @@ -37,7 +34,7 @@ import org.atmosphere.cpr.AtmosphereResource.TRANSPORT; import org.atmosphere.cpr.AtmosphereResourceEvent; import org.atmosphere.cpr.AtmosphereResourceImpl; -import org.atmosphere.cpr.HeaderConfig; +import org.atmosphere.cpr.FrameworkConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tools.jackson.core.JacksonException; @@ -66,7 +63,6 @@ import com.vaadin.flow.server.startup.ApplicationConfiguration; import com.vaadin.flow.shared.ApplicationConstants; import com.vaadin.flow.shared.JsonConstants; -import com.vaadin.flow.shared.communication.PushConstants; import com.vaadin.flow.shared.communication.PushMode; /** @@ -80,8 +76,6 @@ */ public class PushHandler { - private static final String SSE_CONTENT_TYPE = "text/event-stream"; - private int longPollingSuspendTimeout = -1; /** @@ -666,12 +660,17 @@ private static void sendNotificationAndDisconnect( } if (resource.transport() == TRANSPORT.SSE && !resource.isSuspended()) { - writeServerSentEvent(resource, notificationJson); - } else { - resource.getResponse() - .setContentType(JsonConstants.JSON_CONTENT_TYPE); - resource.getResponse().getWriter().write(notificationJson); + // Atmosphere's SSE interceptor frames writes as events only on + // a suspended resource, or when this attribute is set, as it + // is for the handshake of a new connection. EventSource ignores + // anything that is not framed. + resource.getRequest().setAttribute( + FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL, + Boolean.TRUE); } + resource.getResponse() + .setContentType(JsonConstants.JSON_CONTENT_TYPE); + resource.getResponse().getWriter().write(notificationJson); resource.resume(); } catch (Exception e) { getLogger().trace("Failed to send critical notification to client", @@ -679,34 +678,6 @@ private static void sendNotificationAndDisconnect( } } - /** - * Writes the message as a server-sent event, so that the client's - * {@code EventSource} receives it. - *

- * Only for a resource that is not suspended yet, for example when the - * connection is rejected right away. Atmosphere's SSE interceptor adds the - * event framing only to writes on a suspended resource, so the event is - * written to the container response, past Atmosphere's interceptors, with - * the length prefix that Atmosphere's message size tracking would otherwise - * add. Writes to an open connection go through Atmosphere, which frames - * them. - */ - private static void writeServerSentEvent(AtmosphereResource resource, - String message) throws IOException { - String data = message; - if (Boolean.parseBoolean(resource.getRequest() - .getHeader(HeaderConfig.X_ATMOSPHERE_TRACKMESSAGESIZE))) { - data = message.length() - + String.valueOf(PushConstants.MESSAGE_DELIMITER) + message; - } - ServletResponse response = resource.getResponse().getResponse(); - response.setContentType(SSE_CONTENT_TYPE); - response.setCharacterEncoding(StandardCharsets.UTF_8.name()); - PrintWriter writer = response.getWriter(); - writer.write("data:" + data + "\n\n"); - writer.flush(); - } - private static Logger getLogger() { return LoggerFactory.getLogger(PushHandler.class.getName()); } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java index f7140c5ed96..ab87346719d 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java @@ -15,13 +15,10 @@ */ package com.vaadin.flow.server.communication; -import jakarta.servlet.ServletResponse; - import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; -import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -36,10 +33,11 @@ import org.atmosphere.cpr.AtmosphereResourceEvent; import org.atmosphere.cpr.AtmosphereResourceImpl; import org.atmosphere.cpr.AtmosphereResponse; -import org.atmosphere.cpr.HeaderConfig; +import org.atmosphere.cpr.FrameworkConfig; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InOrder; import com.vaadin.flow.component.UI; import com.vaadin.flow.di.Instantiator; @@ -73,11 +71,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.after; import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -700,8 +700,8 @@ public VaadinSession findVaadinSession(VaadinRequest request) { @ParameterizedTest @ValueSource(booleans = { true, false }) - void onConnect_noSessionForRequest_sse_sessionExpiredNotificationSentAsEvent( - boolean trackMessageSize) throws Exception { + void onConnect_noSessionForRequest_sse_notificationFramedAsEventUntilSuspended( + boolean suspended) throws Exception { MockVaadinServletService service = new MockVaadinServletService() { @Override public VaadinSession findVaadinSession(VaadinRequest request) { @@ -710,56 +710,28 @@ public VaadinSession findVaadinSession(VaadinRequest request) { }; setProductionMode(service, false); AtomicReference res = new AtomicReference<>(); - ServletResponse containerResponse = mock(ServletResponse.class); - StringWriter written = new StringWriter(); - when(containerResponse.getWriter()) - .thenReturn(new PrintWriter(written)); runTest(service, (handler, resource) -> { when(resource.transport()).thenReturn(TRANSPORT.SSE); - when(resource.getRequest() - .getHeader(HeaderConfig.X_ATMOSPHERE_TRACKMESSAGESIZE)) - .thenReturn(String.valueOf(trackMessageSize)); - when(resource.getResponse().getResponse()) - .thenReturn(containerResponse); + when(resource.isSuspended()).thenReturn(suspended); handler.onConnect(resource); res.set(resource); }); - // EventSource ignores anything that is not framed as an event, and - // Atmosphere frames only writes to a suspended resource - String json = VaadinService.createSessionExpiredJSON(true); - String data = trackMessageSize ? json.length() + "|" + json : json; - assertEquals("data:" + data + "\n\n", written.toString()); - verify(containerResponse).setContentType("text/event-stream"); - verify(containerResponse).setCharacterEncoding("UTF-8"); - verify(res.get().getResponse(), never()).getWriter(); - verify(res.get()).resume(); - } - - @Test - void onConnect_noSessionForRequest_suspendedSse_notificationWrittenThroughAtmosphere() - throws Exception { - MockVaadinServletService service = new MockVaadinServletService() { - @Override - public VaadinSession findVaadinSession(VaadinRequest request) { - return null; - } - }; - setProductionMode(service, false); - AtomicReference res = new AtomicReference<>(); - - runTest(service, (handler, resource) -> { - when(resource.transport()).thenReturn(TRANSPORT.SSE); - when(resource.isSuspended()).thenReturn(true); - handler.onConnect(resource); - res.set(resource); - }); - - // Atmosphere frames writes to an open connection itself - verify(res.get().getResponse().getWriter()) + // Atmosphere frames writes to a suspended resource as SSE events by + // itself, and to one that is not suspended yet only with the attribute + AtmosphereRequest request = res.get().getRequest(); + PrintWriter writer = res.get().getResponse().getWriter(); + InOrder inOrder = inOrder(request, writer); + if (suspended) { + verify(request, never()).setAttribute( + eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any()); + } else { + inOrder.verify(request).setAttribute( + FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL, Boolean.TRUE); + } + inOrder.verify(writer) .write(VaadinService.createSessionExpiredJSON(true)); - verify(res.get().getResponse(), never()).getResponse(); } @Test From c3a04cf9826864df44e4ec3b932b608e1fb2f58b Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:22:47 +0000 Subject: [PATCH 4/7] test: check websocket notifications are not marked as SSE handshake --- .../vaadin/flow/server/communication/PushHandlerTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java index ab87346719d..7257b883606 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java @@ -696,6 +696,8 @@ public VaadinSession findVaadinSession(VaadinRequest request) { verify(res.get().getResponse().getWriter()) .write(VaadinService.createSessionExpiredJSON(true)); + verify(res.get().getRequest(), never()).setAttribute( + eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any()); } @ParameterizedTest @@ -718,8 +720,9 @@ public VaadinSession findVaadinSession(VaadinRequest request) { res.set(resource); }); - // Atmosphere frames writes to a suspended resource as SSE events by - // itself, and to one that is not suspended yet only with the attribute + // Atmosphere's SSE interceptor frames writes to a suspended resource as + // events by itself, and to one that is not suspended yet only when this + // attribute is set, as Atmosphere does for a connection handshake AtmosphereRequest request = res.get().getRequest(); PrintWriter writer = res.get().getResponse().getWriter(); InOrder inOrder = inOrder(request, writer); From 271e911049040d243d296d1e89ed1c96540ec8e9 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:42:22 +0000 Subject: [PATCH 5/7] test: check SSE session expired notification in a browser Replace the unit test that only checked a request attribute on a mocked resource with an integration test: the server closes the SSE push connection after the session cookie is gone, and the page must reload from the session expired notification sent on the new connection. The test fails without the fix, as the client then keeps reconnecting. The websocket assertion added earlier in this PR is removed with it, as it also checked the attribute rather than the output. --- .../server/communication/PushHandlerTest.java | 45 ------------- .../uitest/ui/push/SsePushReconnectView.java | 60 +++++++++++++++++ .../ui/push/SsePushSessionExpiredIT.java | 64 +++++++++++++++++++ 3 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java create mode 100644 flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java index 7257b883606..352edebe081 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/PushHandlerTest.java @@ -33,11 +33,7 @@ import org.atmosphere.cpr.AtmosphereResourceEvent; import org.atmosphere.cpr.AtmosphereResourceImpl; import org.atmosphere.cpr.AtmosphereResponse; -import org.atmosphere.cpr.FrameworkConfig; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.InOrder; import com.vaadin.flow.component.UI; import com.vaadin.flow.di.Instantiator; @@ -71,13 +67,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.after; import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -696,45 +690,6 @@ public VaadinSession findVaadinSession(VaadinRequest request) { verify(res.get().getResponse().getWriter()) .write(VaadinService.createSessionExpiredJSON(true)); - verify(res.get().getRequest(), never()).setAttribute( - eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any()); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void onConnect_noSessionForRequest_sse_notificationFramedAsEventUntilSuspended( - boolean suspended) throws Exception { - MockVaadinServletService service = new MockVaadinServletService() { - @Override - public VaadinSession findVaadinSession(VaadinRequest request) { - return null; - } - }; - setProductionMode(service, false); - AtomicReference res = new AtomicReference<>(); - - runTest(service, (handler, resource) -> { - when(resource.transport()).thenReturn(TRANSPORT.SSE); - when(resource.isSuspended()).thenReturn(suspended); - handler.onConnect(resource); - res.set(resource); - }); - - // Atmosphere's SSE interceptor frames writes to a suspended resource as - // events by itself, and to one that is not suspended yet only when this - // attribute is set, as Atmosphere does for a connection handshake - AtmosphereRequest request = res.get().getRequest(); - PrintWriter writer = res.get().getResponse().getWriter(); - InOrder inOrder = inOrder(request, writer); - if (suspended) { - verify(request, never()).setAttribute( - eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any()); - } else { - inOrder.verify(request).setAttribute( - FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL, Boolean.TRUE); - } - inOrder.verify(writer) - .write(VaadinService.createSessionExpiredJSON(true)); } @Test diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java new file mode 100644 index 00000000000..38ec34c9c36 --- /dev/null +++ b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2026 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui.push; + +import com.vaadin.flow.component.AttachEvent; +import com.vaadin.flow.component.UI; +import com.vaadin.flow.component.html.NativeButton; +import com.vaadin.flow.router.Route; +import com.vaadin.flow.shared.ui.Transport; +import com.vaadin.flow.uitest.servlet.ViewTestLayout; + +/** + * Lets a test close the push connection from the server, so that the client + * opens a new one. + */ +@CustomPush(transport = Transport.SERVER_SENT_EVENTS) +@Route(value = "com.vaadin.flow.uitest.ui.push.SsePushReconnectView", layout = ViewTestLayout.class) +public class SsePushReconnectView extends BasicPushSseView { + + public static final String CLOSE_PUSH_LATER_ID = "close-push-later"; + + /** + * Delay before the push connection is closed, giving the test time to + * prepare the reconnect. + */ + public static final int CLOSE_DELAY_MS = 2000; + + @Override + protected void onAttach(AttachEvent attachEvent) { + super.onAttach(attachEvent); + UI ui = attachEvent.getUI(); + NativeButton closePushLater = new NativeButton( + "Close push connection in " + CLOSE_DELAY_MS + " ms", + event -> new Thread(() -> { + try { + Thread.sleep(CLOSE_DELAY_MS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + ui.access(() -> ui.getInternals().getPushConnection() + .disconnect()); + }).start()); + closePushLater.setId(CLOSE_PUSH_LATER_ID); + add(closePushLater); + } +} diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java new file mode 100644 index 00000000000..190aceaead9 --- /dev/null +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2026 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui.push; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.openqa.selenium.By; +import org.openqa.selenium.TimeoutException; + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.testcategory.IgnoreOSGi; +import com.vaadin.flow.testcategory.PushTests; +import com.vaadin.flow.testutil.ChromeBrowserTest; + +@Category({ PushTests.class, IgnoreOSGi.class }) +public class SsePushSessionExpiredIT extends ChromeBrowserTest { + + @Override + protected Class getViewClass() { + return SsePushReconnectView.class; + } + + @Test + public void sessionGone_pushReconnects_pageReloads() { + open(); + AbstractClientServerCounterIT.getIncrementButton(this).click(); + waitUntil(driver -> AbstractClientServerCounterIT + .getClientCounter(this) == 1); + // Survives push reconnects, but not a reload + executeScript("window.beforeSessionGone = true"); + + // As after a server restart: the server closes the push connection, + // and the session is gone when the client opens a new one + findElement(By.id(SsePushReconnectView.CLOSE_PUSH_LATER_ID)).click(); + getDriver().manage().deleteCookieNamed("JSESSIONID"); + + // The new push connection is answered with the session expired + // notification, which reloads the page by default. If the client + // misses the notification, it keeps opening new push connections and + // the page is not reloaded. + try { + waitUntil( + driver -> Boolean.TRUE.equals(executeScript( + "return window.beforeSessionGone === undefined")), + 20); + } catch (TimeoutException e) { + Assert.fail("The page was not reloaded after the session was gone"); + } + } +} From 2dede2079c8deaeda4ae0f6d0f4cc31b0f9ab303 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:56:15 +0000 Subject: [PATCH 6/7] test: run the push session expired IT for every transport Follow the IdlePushChannelIT pattern: an abstract test with one subclass per transport, so long polling and websocket are covered too. The button that closes the push connection from the server moves into BasicPushView, which all transport views extend, replacing the SSE-only SsePushReconnectView. --- .../flow/uitest/ui/push/BasicPushView.java | 26 ++++++++ .../uitest/ui/push/SsePushReconnectView.java | 60 ------------------- ...piredIT.java => PushSessionExpiredIT.java} | 13 +--- .../push/PushSessionExpiredLongPollingIT.java | 31 ++++++++++ .../ui/push/PushSessionExpiredSseIT.java | 31 ++++++++++ .../push/PushSessionExpiredWebsocketIT.java | 31 ++++++++++ 6 files changed, 122 insertions(+), 70 deletions(-) delete mode 100644 flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java rename flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/{SsePushSessionExpiredIT.java => PushSessionExpiredIT.java} (83%) create mode 100644 flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredLongPollingIT.java create mode 100644 flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredSseIT.java create mode 100644 flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredWebsocketIT.java diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/BasicPushView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/BasicPushView.java index 596ef4d7fab..cb4c643df47 100644 --- a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/BasicPushView.java +++ b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/BasicPushView.java @@ -17,6 +17,7 @@ import com.vaadin.flow.component.AttachEvent; import com.vaadin.flow.component.UI; +import com.vaadin.flow.component.html.NativeButton; import com.vaadin.flow.router.Route; import com.vaadin.flow.uitest.servlet.ViewTestLayout; import com.vaadin.flow.uitest.ui.push.components.ClientServerCounter; @@ -25,6 +26,14 @@ @Route(value = "com.vaadin.flow.uitest.ui.push.BasicPushView", layout = ViewTestLayout.class) public class BasicPushView extends ClientServerCounter { + public static final String CLOSE_PUSH_LATER_ID = "close-push-later"; + + /** + * Delay before the push connection is closed, giving the test time to + * prepare the reconnect. + */ + public static final int CLOSE_PUSH_DELAY_MS = 2000; + @Override protected void onAttach(AttachEvent attachEvent) { UI ui = attachEvent.getUI(); @@ -36,5 +45,22 @@ protected void onAttach(AttachEvent attachEvent) { ui.getPushConfiguration().setPushMode(push.value()); ui.getPushConfiguration().setTransport(push.transport()); + + // Lets a test close the push connection from the server, so that the + // client opens a new one + NativeButton closePushLater = new NativeButton( + "Close push connection in " + CLOSE_PUSH_DELAY_MS + " ms", + event -> new Thread(() -> { + try { + Thread.sleep(CLOSE_PUSH_DELAY_MS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + ui.access(() -> ui.getInternals().getPushConnection() + .disconnect()); + }).start()); + closePushLater.setId(CLOSE_PUSH_LATER_ID); + add(closePushLater); } } diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java deleted file mode 100644 index 38ec34c9c36..00000000000 --- a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/push/SsePushReconnectView.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2000-2026 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.flow.uitest.ui.push; - -import com.vaadin.flow.component.AttachEvent; -import com.vaadin.flow.component.UI; -import com.vaadin.flow.component.html.NativeButton; -import com.vaadin.flow.router.Route; -import com.vaadin.flow.shared.ui.Transport; -import com.vaadin.flow.uitest.servlet.ViewTestLayout; - -/** - * Lets a test close the push connection from the server, so that the client - * opens a new one. - */ -@CustomPush(transport = Transport.SERVER_SENT_EVENTS) -@Route(value = "com.vaadin.flow.uitest.ui.push.SsePushReconnectView", layout = ViewTestLayout.class) -public class SsePushReconnectView extends BasicPushSseView { - - public static final String CLOSE_PUSH_LATER_ID = "close-push-later"; - - /** - * Delay before the push connection is closed, giving the test time to - * prepare the reconnect. - */ - public static final int CLOSE_DELAY_MS = 2000; - - @Override - protected void onAttach(AttachEvent attachEvent) { - super.onAttach(attachEvent); - UI ui = attachEvent.getUI(); - NativeButton closePushLater = new NativeButton( - "Close push connection in " + CLOSE_DELAY_MS + " ms", - event -> new Thread(() -> { - try { - Thread.sleep(CLOSE_DELAY_MS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return; - } - ui.access(() -> ui.getInternals().getPushConnection() - .disconnect()); - }).start()); - closePushLater.setId(CLOSE_PUSH_LATER_ID); - add(closePushLater); - } -} diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredIT.java similarity index 83% rename from flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java rename to flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredIT.java index 190aceaead9..3040e51019e 100644 --- a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/SsePushSessionExpiredIT.java +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredIT.java @@ -21,18 +21,11 @@ import org.openqa.selenium.By; import org.openqa.selenium.TimeoutException; -import com.vaadin.flow.component.Component; -import com.vaadin.flow.testcategory.IgnoreOSGi; import com.vaadin.flow.testcategory.PushTests; import com.vaadin.flow.testutil.ChromeBrowserTest; -@Category({ PushTests.class, IgnoreOSGi.class }) -public class SsePushSessionExpiredIT extends ChromeBrowserTest { - - @Override - protected Class getViewClass() { - return SsePushReconnectView.class; - } +@Category(PushTests.class) +public abstract class PushSessionExpiredIT extends ChromeBrowserTest { @Test public void sessionGone_pushReconnects_pageReloads() { @@ -45,7 +38,7 @@ public void sessionGone_pushReconnects_pageReloads() { // As after a server restart: the server closes the push connection, // and the session is gone when the client opens a new one - findElement(By.id(SsePushReconnectView.CLOSE_PUSH_LATER_ID)).click(); + findElement(By.id(BasicPushView.CLOSE_PUSH_LATER_ID)).click(); getDriver().manage().deleteCookieNamed("JSESSIONID"); // The new push connection is answered with the session expired diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredLongPollingIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredLongPollingIT.java new file mode 100644 index 00000000000..496f15064ef --- /dev/null +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredLongPollingIT.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2026 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui.push; + +import org.junit.experimental.categories.Category; + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.testcategory.IgnoreOSGi; +import com.vaadin.flow.testcategory.PushTests; + +@Category({ PushTests.class, IgnoreOSGi.class }) +public class PushSessionExpiredLongPollingIT extends PushSessionExpiredIT { + + @Override + protected Class getViewClass() { + return BasicPushLongPollingView.class; + } +} diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredSseIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredSseIT.java new file mode 100644 index 00000000000..52f4f145431 --- /dev/null +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredSseIT.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2026 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui.push; + +import org.junit.experimental.categories.Category; + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.testcategory.IgnoreOSGi; +import com.vaadin.flow.testcategory.PushTests; + +@Category({ PushTests.class, IgnoreOSGi.class }) +public class PushSessionExpiredSseIT extends PushSessionExpiredIT { + + @Override + protected Class getViewClass() { + return BasicPushSseView.class; + } +} diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredWebsocketIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredWebsocketIT.java new file mode 100644 index 00000000000..60b3559caa9 --- /dev/null +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/push/PushSessionExpiredWebsocketIT.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2026 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui.push; + +import org.junit.experimental.categories.Category; + +import com.vaadin.flow.component.Component; +import com.vaadin.flow.testcategory.IgnoreOSGi; +import com.vaadin.flow.testcategory.PushTests; + +@Category({ PushTests.class, IgnoreOSGi.class }) +public class PushSessionExpiredWebsocketIT extends PushSessionExpiredIT { + + @Override + protected Class getViewClass() { + return BasicPushWebsocketView.class; + } +} From 8850e914ef9fc958302cf67c39ec36ad612556cf Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 07:37:54 +0000 Subject: [PATCH 7/7] test: list the push session-expired ITs in the flow-tests migration list The new PushSessionExpired*IT classes live in test-root-context next to the other push ITs, so they move to test-default with the push package. --- flow-tests/MIGRATION_LIST.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/flow-tests/MIGRATION_LIST.md b/flow-tests/MIGRATION_LIST.md index d63efcc0f29..a3893b4a835 100644 --- a/flow-tests/MIGRATION_LIST.md +++ b/flow-tests/MIGRATION_LIST.md @@ -16,7 +16,7 @@ procedure in [README.md](README.md) and the permutation policy in | Target | ITs | |---|---| -| test-default | 368 | +| test-default | 372 | | ?UNMAPPED? | 33 | | test-embedding | 27 | | test-production | 23 | @@ -46,7 +46,7 @@ procedure in [README.md](README.md) and the permutation policy in | test-plain-spring | 1 | | test-push-startup(keep) | 1 | | test-tailwind | 1 | -| **TOTAL** | **631** | +| **TOTAL** | **635** | ## Mapping (grouped by target module, then feature) @@ -496,6 +496,10 @@ procedure in [README.md](README.md) and the permutation policy in | PushLargeDataWebsocketIT | test-root-context | move | | PushLongPollingUpdateDivIT | test-root-context | move | | PushLongPollingWithPreserveOnRefreshIT | test-root-context | move | +| PushSessionExpiredIT | test-root-context | move | +| PushSessionExpiredLongPollingIT | test-root-context | move | +| PushSessionExpiredSseIT | test-root-context | move | +| PushSessionExpiredWebsocketIT | test-root-context | move | | PushSettingsIT | test-root-context | move | | PushToggleComponentVisibilityIT | test-root-context | move | | PushUnloadPolicyIT | test-root-context | move |