Skip to content

Commit 975e1c4

Browse files
authored
fix: Explicitly check the lock when touching pendingJsInvocations (#22783)
Explicitly check the lock when touching pendingJsInvocations Alos uses proper UI in test: this ensures that the session is available also from the inside and not only through a Mockito mock. Related to #22742
1 parent 3288949 commit 975e1c4

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ public void addJavaScriptInvocation(
620620
* @return a list of pending JavaScript invocations
621621
*/
622622
public List<PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations() {
623+
session.checkHasLock();
623624
pendingTitleUpdateCanceler = null;
624625

625626
if (pendingJsInvocations.isEmpty()) {
@@ -677,6 +678,7 @@ public void execute() {
677678

678679
private void removePendingInvocation(
679680
PendingJavaScriptInvocation invocation) {
681+
session.checkHasLock();
680682
UIInternals.this.pendingJsInvocations.remove(invocation);
681683
if (invocationList.isEmpty() && registration != null) {
682684
registration.remove();
@@ -699,6 +701,7 @@ void onInvocationCompleted(PendingJavaScriptInvocation invocation) {
699701
*/
700702
// Non-private for testing purposes
701703
Stream<PendingJavaScriptInvocation> getPendingJavaScriptInvocations() {
704+
session.checkHasLock();
702705
return pendingJsInvocations.stream()
703706
.filter(invocation -> !invocation.isCanceled());
704707
}

flow-server/src/test/java/com/vaadin/flow/server/communication/AtmospherePushConnectionTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.vaadin.flow.component.UI;
4343
import com.vaadin.flow.server.MockVaadinSession;
4444
import com.vaadin.flow.server.communication.AtmospherePushConnection.State;
45+
import com.vaadin.tests.util.MockUI;
4546

4647
/**
4748
* @author Vaadin Ltd
@@ -67,9 +68,10 @@ public static void stopExecutor() {
6768

6869
@Before
6970
public void setup() throws Exception {
70-
UI ui = Mockito.spy(new UI());
7171
vaadinSession = new MockVaadinSession();
72-
Mockito.when(ui.getSession()).thenReturn(vaadinSession);
72+
vaadinSession.lock();
73+
UI ui = new MockUI(vaadinSession);
74+
vaadinSession.unlock();
7375
broadcaster = Mockito.mock(Broadcaster.class);
7476
resource = Mockito.mock(AtmosphereResource.class);
7577
Mockito.when(resource.getBroadcaster()).thenReturn(broadcaster);

0 commit comments

Comments
 (0)