From 5b18c5469e40529fad0d3d014ab4237a56616d81 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 25 May 2016 21:37:56 +0300 Subject: Send an ack message after push has removed connectors (#19822) The server side needs to know the client has removed the connectors to be able to do cleanup Change-Id: Ic3d41cc5cbab035a53bf5c99496d74858c376e73 --- .../vaadin/tests/push/PushRemoveConnectors.java | 92 ++++++++++++++++++++++ .../tests/push/PushRemoveConnectorsTest.java | 36 +++++++++ 2 files changed, 128 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/push/PushRemoveConnectors.java create mode 100644 uitest/src/test/java/com/vaadin/tests/push/PushRemoveConnectorsTest.java (limited to 'uitest/src') diff --git a/uitest/src/main/java/com/vaadin/tests/push/PushRemoveConnectors.java b/uitest/src/main/java/com/vaadin/tests/push/PushRemoveConnectors.java new file mode 100644 index 0000000000..528492c33a --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/push/PushRemoveConnectors.java @@ -0,0 +1,92 @@ +package com.vaadin.tests.push; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang.SerializationUtils; + +import com.vaadin.annotations.Push; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.AbstractOrderedLayout; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; + +@Push +public class PushRemoveConnectors extends AbstractTestUIWithLog { + + private transient final ScheduledExecutorService threadPool = Executors + .newScheduledThreadPool(5); + static final String START = "start"; + static final String STOP = "stop"; + private AbstractOrderedLayout verticalLayout; + private transient ScheduledFuture task = null; + + @Override + protected void setup(VaadinRequest request) { + final CheckBox pollingEnabled = new CheckBox("Polling enabled"); + pollingEnabled.addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + setPollInterval(pollingEnabled.getValue() ? 1000 : -1); + } + }); + + Button start = new Button("start"); + start.setId(START); + start.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + task = threadPool.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + access(new Runnable() { + public void run() { + populate(); + log("Serialized session size: " + + getSessionSize()); + } + }); + } + }, 1, 1, TimeUnit.SECONDS); + } + }); + Button stop = new Button("stop"); + stop.setId(STOP); + stop.addClickListener(new ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + if (task != null) { + task.cancel(true); + task = null; + } + + } + }); + verticalLayout = new HorizontalLayout(); + populate(); + addComponents(pollingEnabled, start, stop, verticalLayout); + } + + private void populate() { + verticalLayout.removeAllComponents(); + for (int i = 0; i < 500; i++) { + Label l = new Label("."); + l.setSizeUndefined(); + verticalLayout.addComponent(l); + } + } + + private int getSessionSize() { + return SerializationUtils.serialize(getSession()).length; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/push/PushRemoveConnectorsTest.java b/uitest/src/test/java/com/vaadin/tests/push/PushRemoveConnectorsTest.java new file mode 100644 index 0000000000..bcf93b0aa9 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/push/PushRemoveConnectorsTest.java @@ -0,0 +1,36 @@ +package com.vaadin.tests.push; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class PushRemoveConnectorsTest extends SingleBrowserTest { + + @Test + public void testNoMemoryLeak() throws InterruptedException { + openTestURL(); + $(ButtonElement.class).id(PushRemoveConnectors.START).click(); + Thread.sleep(5000); + int last = getMemoryUsage(); + int i = 0; + while (i++ < 10) { + Thread.sleep(5000); + int now = getMemoryUsage(); + System.out.println("Memory usage: "+now); + if (last == now) + break; + + last = now; + } + $(ButtonElement.class).id(PushRemoveConnectors.STOP).click(); + + Assert.assertNotEquals(10, i); + } + + private int getMemoryUsage() { + return Integer.parseInt(getLogRow(0).replaceFirst( + ".*Serialized session size: ", "")); + } +} -- cgit v1.2.3