aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-05-16 15:14:11 +0300
committerPekka Hyvönen <pekka@vaadin.com>2017-05-16 15:14:11 +0300
commitaa3ad5db0f3c5e1c95dd0b84c229851460da6449 (patch)
tree0fd7bf987c7503b745921b2866316a9ed170514f /server/src/test/java
parent76a0e04cb056fe2c6e2ad62f6d0972b2347a0fcf (diff)
downloadvaadin-framework-aa3ad5db0f3c5e1c95dd0b84c229851460da6449.tar.gz
vaadin-framework-aa3ad5db0f3c5e1c95dd0b84c229851460da6449.zip
Clean connector tracker after each access block to stop memory leaks (#9305)
Immediately clean connectors which the client side does not know about Fixes #9303
Diffstat (limited to 'server/src/test/java')
-rw-r--r--server/src/test/java/com/vaadin/ui/UITest.java56
-rw-r--r--server/src/test/java/com/vaadin/util/CurrentInstanceTest.java2
2 files changed, 57 insertions, 1 deletions
diff --git a/server/src/test/java/com/vaadin/ui/UITest.java b/server/src/test/java/com/vaadin/ui/UITest.java
index 322bc81a04..8bc3c98470 100644
--- a/server/src/test/java/com/vaadin/ui/UITest.java
+++ b/server/src/test/java/com/vaadin/ui/UITest.java
@@ -1,5 +1,6 @@
package com.vaadin.ui;
+import java.lang.ref.WeakReference;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
@@ -10,6 +11,7 @@ import javax.servlet.ServletConfig;
import org.junit.Assert;
import org.junit.Test;
+import org.mockito.Mockito;
import com.vaadin.server.DefaultDeploymentConfiguration;
import com.vaadin.server.MockServletConfig;
@@ -20,6 +22,7 @@ import com.vaadin.server.VaadinServletService;
import com.vaadin.server.VaadinSession;
import com.vaadin.server.communication.PushConnection;
import com.vaadin.shared.communication.PushMode;
+import com.vaadin.util.CurrentInstanceTest;
public class UITest {
@@ -152,4 +155,57 @@ public class UITest {
Assert.assertNull(ui.getPushConnection());
}
+
+ @Test
+ public void connectorTrackerMemoryLeak() throws Exception {
+ final UI ui = new UI() {
+
+ @Override
+ protected void init(VaadinRequest request) {
+ }
+
+ };
+ ServletConfig servletConfig = new MockServletConfig();
+ VaadinServlet servlet = new VaadinServlet();
+ servlet.init(servletConfig);
+
+ DefaultDeploymentConfiguration deploymentConfiguration = new DefaultDeploymentConfiguration(
+ UI.class, new Properties());
+
+ VaadinServletService service = new VaadinServletService(servlet,
+ deploymentConfiguration);
+ MockVaadinSession session = new MockVaadinSession(service);
+ session.lock();
+ ui.setSession(session);
+ ui.doInit(Mockito.mock(VaadinRequest.class), 1, "foo");
+ session.addUI(ui);
+ ui.setContent(createContent());
+ WeakReference<Component> contentSentToClient = new WeakReference<>(
+ ui.getContent());
+ ui.getConnectorTracker()
+ .markClientSideInitialized(contentSentToClient.get());
+ session.unlock();
+
+ session.lock();
+ ui.setContent(createContent());
+ WeakReference<Component> contentOnlyOnServer = new WeakReference<>(
+ ui.getContent());
+ ui.setContent(createContent());
+
+ CurrentInstanceTest.waitUntilGarbageCollected(contentOnlyOnServer);
+ // Should not clean references for connectors available in the browser
+ // until the session is unlocked and we know if it has been moved
+ Assert.assertNotNull(contentSentToClient.get());
+ session.unlock();
+ CurrentInstanceTest.waitUntilGarbageCollected(contentSentToClient);
+ }
+
+ private Component createContent() {
+ VerticalLayout vl = new VerticalLayout();
+ vl.addComponent(new Button("foo"));
+ vl.addComponent(new Button("bar"));
+ vl.addComponent(new Button("baz"));
+ return vl;
+ }
+
}
diff --git a/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java b/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java
index e2da8e0278..dd4cc3f543 100644
--- a/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java
+++ b/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java
@@ -159,7 +159,7 @@ public class CurrentInstanceTest {
Assert.assertNull(VaadinSession.getCurrent());
}
- private static void waitUntilGarbageCollected(WeakReference<?> ref)
+ public static void waitUntilGarbageCollected(WeakReference<?> ref)
throws InterruptedException {
for (int i = 0; i < 50; i++) {
System.gc();