Skip to content

Commit df01d1f

Browse files
authored
feat: add HeartbeatListener to UI (#12409)
1 parent ab14aeb commit df01d1f

6 files changed

Lines changed: 137 additions & 5 deletions

File tree

flow-server/src/main/java/com/vaadin/flow/component/internal/HeartbeatEvent.java renamed to flow-server/src/main/java/com/vaadin/flow/component/HeartbeatEvent.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16-
package com.vaadin.flow.component.internal;
16+
package com.vaadin.flow.component;
1717

1818
import java.util.EventObject;
1919

20-
import com.vaadin.flow.component.UI;
21-
2220
/**
23-
* Event created for a application heartbeat from the client.
21+
* Event created for an application heartbeat from the client.
2422
*
2523
* @since 2.0
2624
*/

flow-server/src/main/java/com/vaadin/flow/component/internal/HeartbeatListener.java renamed to flow-server/src/main/java/com/vaadin/flow/component/HeartbeatListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* License for the specific language governing permissions and limitations under
1414
* the License.
1515
*/
16-
package com.vaadin.flow.component.internal;
16+
package com.vaadin.flow.component;
1717

1818
import java.io.Serializable;
1919

‎flow-server/src/main/java/com/vaadin/flow/component/UI.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,26 @@ public ShortcutRegistration addShortcutListener(
11771177
listener, key).withModifiers(keyModifiers);
11781178
}
11791179

1180+
/**
1181+
* Add a listener that will be informed when this UI received a heartbeat
1182+
* from the client-side.
1183+
* <p>
1184+
* Heartbeat requests are periodically sent by the client-side to inform the
1185+
* server that the UI sending the heartbeat is still alive (the browser
1186+
* window is open, the connection is up) even when there are no UIDL
1187+
* requests for a prolonged period of time. UIs that do not receive either
1188+
* heartbeat or UIDL requests are eventually removed from the session and
1189+
* garbage collected.
1190+
*
1191+
* @param listener
1192+
* the heartbeat listener
1193+
* @return handler to remove the heartbeat listener
1194+
*/
1195+
public Registration addHeartbeatListener(HeartbeatListener listener) {
1196+
Objects.requireNonNull(listener, NULL_LISTENER);
1197+
return internals.addHeartbeatListener(listener);
1198+
}
1199+
11801200
/**
11811201
* Gets the drag source of an active HTML5 drag event.
11821202
* <p>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.vaadin.flow.component.Component;
3636
import com.vaadin.flow.component.ComponentUtil;
3737
import com.vaadin.flow.component.HasElement;
38+
import com.vaadin.flow.component.HeartbeatEvent;
39+
import com.vaadin.flow.component.HeartbeatListener;
3840
import com.vaadin.flow.component.PushConfiguration;
3941
import com.vaadin.flow.component.UI;
4042
import com.vaadin.flow.component.dependency.CssImport;

‎flow-server/src/test/java/com/vaadin/flow/component/UITest.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import com.vaadin.flow.server.VaadinResponse;
8080
import com.vaadin.flow.server.VaadinService;
8181
import com.vaadin.flow.server.VaadinServletRequest;
82+
import com.vaadin.flow.shared.Registration;
8283
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
8384
import com.vaadin.tests.util.MockUI;
8485

@@ -408,6 +409,60 @@ public void removeComponent() {
408409
ComponentTest.assertChildren(ui);
409410
}
410411

412+
@Test
413+
public void setLastHeartbeatTimestamp_heartbeatEventIsFired() {
414+
UI ui = new UI();
415+
initUI(ui, "", null);
416+
417+
long heartbeatTimestamp = System.currentTimeMillis();
418+
List<HeartbeatEvent> events = new ArrayList<>();
419+
ui.addHeartbeatListener(events::add);
420+
421+
ui.getInternals().setLastHeartbeatTimestamp(heartbeatTimestamp);
422+
423+
assertEquals(1, events.size());
424+
assertEquals(ui, events.get(0).getSource());
425+
assertEquals(heartbeatTimestamp, events.get(0).getHeartbeatTime());
426+
}
427+
428+
@Test
429+
public void setLastHeartbeatTimestamp_multipleHeartbeatListenerRegistered_eachHeartbeatListenerIsCalled() {
430+
UI ui = new UI();
431+
initUI(ui, "", null);
432+
433+
List<HeartbeatEvent> events = new ArrayList<>();
434+
ui.addHeartbeatListener(events::add);
435+
ui.addHeartbeatListener(events::add);
436+
437+
ui.getInternals().setLastHeartbeatTimestamp(System.currentTimeMillis());
438+
439+
assertEquals(2, events.size());
440+
}
441+
442+
@Test
443+
public void setLastHeartbeatTimestamp_heartbeatListenerRemoved_listenerNotRun() {
444+
UI ui = new UI();
445+
initUI(ui, "", null);
446+
447+
AtomicReference<Registration> reference = new AtomicReference<>();
448+
AtomicInteger runCount = new AtomicInteger();
449+
450+
Registration registration = ui.addHeartbeatListener(event -> {
451+
runCount.incrementAndGet();
452+
reference.get().remove(); // removes the listener on the first
453+
// invocation
454+
});
455+
reference.set(registration);
456+
457+
ui.getInternals().setLastHeartbeatTimestamp(System.currentTimeMillis());
458+
assertEquals("Listener should have been run once", 1, runCount.get());
459+
460+
ui.getInternals().setLastHeartbeatTimestamp(System.currentTimeMillis());
461+
assertEquals(
462+
"Listener should not have been run again since it was removed",
463+
1, runCount.get());
464+
}
465+
411466
@Test
412467
public void setSession_attachEventIsFired()
413468
throws InvalidRouteConfigurationException {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.server.communication;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Test;
21+
import org.mockito.Mockito;
22+
23+
import com.vaadin.flow.component.UI;
24+
import com.vaadin.flow.component.internal.UIInternals;
25+
import com.vaadin.flow.server.VaadinRequest;
26+
import com.vaadin.flow.server.VaadinResponse;
27+
import com.vaadin.flow.server.VaadinService;
28+
import com.vaadin.flow.server.VaadinSession;
29+
30+
import static org.mockito.ArgumentMatchers.anyLong;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.times;
33+
import static org.mockito.Mockito.when;
34+
35+
public class HeartbeatHandlerTest {
36+
37+
@Test
38+
public void synchronizedHandleRequest_uiPresent_setLastHeartbeatTimestampIsCalledOnce()
39+
throws IOException {
40+
VaadinService service = mock(VaadinService.class);
41+
VaadinSession session = mock(VaadinSession.class);
42+
VaadinRequest request = mock(VaadinRequest.class);
43+
VaadinResponse response = mock(VaadinResponse.class);
44+
UI ui = mock(UI.class);
45+
UIInternals uiInternals = mock(UIInternals.class);
46+
47+
when(ui.getInternals()).thenReturn(uiInternals);
48+
when(session.getService()).thenReturn(service);
49+
when(service.findUI(request)).thenReturn(ui);
50+
51+
HeartbeatHandler handler = new HeartbeatHandler();
52+
handler.synchronizedHandleRequest(session, request, response);
53+
54+
Mockito.verify(ui.getInternals(), times(1))
55+
.setLastHeartbeatTimestamp(anyLong());
56+
}
57+
}

0 commit comments

Comments
 (0)