From: Leif Åstrand Date: Thu, 5 Jan 2017 08:27:22 +0000 (+0200) Subject: Restore all current instance values after running access tasks (#8131) X-Git-Tag: 8.0.0.beta2~110 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=948ce0333a353d49f484500dc932da21c61946ce;p=vaadin-framework.git Restore all current instance values after running access tasks (#8131) * Restore all current instance values after running access tasks Fixes vaadin/framework8-issues#593 --- diff --git a/server/src/main/java/com/vaadin/server/VaadinService.java b/server/src/main/java/com/vaadin/server/VaadinService.java index c5a5aeec18..6ae9df2408 100644 --- a/server/src/main/java/com/vaadin/server/VaadinService.java +++ b/server/src/main/java/com/vaadin/server/VaadinService.java @@ -1860,8 +1860,11 @@ public abstract class VaadinService implements Serializable { } FutureAccess pendingAccess; + + // Dump all current instances, not only the ones dumped by setCurrent Map, CurrentInstance> oldInstances = CurrentInstance - .setCurrent(session); + .getInstances(); + CurrentInstance.setCurrent(session); try { while ((pendingAccess = session.getPendingAccessQueue() .poll()) != null) { diff --git a/server/src/test/java/com/vaadin/server/VaadinServiceTest.java b/server/src/test/java/com/vaadin/server/VaadinServiceTest.java index d0e0c591b3..59ddaf3e67 100644 --- a/server/src/test/java/com/vaadin/server/VaadinServiceTest.java +++ b/server/src/test/java/com/vaadin/server/VaadinServiceTest.java @@ -26,6 +26,8 @@ import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Test; +import com.vaadin.util.CurrentInstance; + /** * * @author Vaadin Ltd @@ -50,10 +52,7 @@ public class VaadinServiceTest { @Test public void testFireSessionDestroy() throws ServletException { - ServletConfig servletConfig = new MockServletConfig(); - VaadinServlet servlet = new VaadinServlet(); - servlet.init(servletConfig); - VaadinService service = servlet.getService(); + VaadinService service = createService(); TestSessionDestroyListener listener = new TestSessionDestroyListener(); @@ -140,4 +139,34 @@ public class VaadinServiceTest { assertThat(notification, containsString("\"url\":null")); } + + @Test + public void currentInstancesAfterPendingAccessTasks() { + VaadinService service = createService(); + + MockVaadinSession session = new MockVaadinSession(service); + session.lock(); + service.accessSession(session, () -> { + CurrentInstance.set(String.class, "Set in task"); + }); + + CurrentInstance.set(String.class, "Original value"); + service.runPendingAccessTasks(session); + Assert.assertEquals( + "Original CurrentInstance should be set after the task has been run", + "Original value", CurrentInstance.get(String.class)); + } + + private static VaadinService createService() { + ServletConfig servletConfig = new MockServletConfig(); + VaadinServlet servlet = new VaadinServlet(); + try { + servlet.init(servletConfig); + } catch (ServletException e) { + throw new RuntimeException(e); + } + VaadinService service = servlet.getService(); + return service; + } + }