diff options
author | Teamcity <build@vaadin.com> | 2015-04-24 10:47:03 +0300 |
---|---|---|
committer | Teamcity <build@vaadin.com> | 2015-04-24 10:47:03 +0300 |
commit | ac7a9ecf8112b10e86367d5e83a0fc23cab2c0c2 (patch) | |
tree | 0cb0ca432bed82adde7b04b01bbf59b3146f1764 | |
parent | 782984ff388c41a4582f70285fd973d131a1e117 (diff) | |
parent | 8be56ef580e4fab732060d7d99f560c9917ca502 (diff) | |
download | vaadin-framework-ac7a9ecf8112b10e86367d5e83a0fc23cab2c0c2.tar.gz vaadin-framework-ac7a9ecf8112b10e86367d5e83a0fc23cab2c0c2.zip |
Merge branch 'refs/heads/master' into 'snapshot/7.5'
-rw-r--r-- | server/src/com/vaadin/util/CurrentInstance.java | 5 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/util/CurrentInstanceTest.java | 45 |
2 files changed, 47 insertions, 3 deletions
diff --git a/server/src/com/vaadin/util/CurrentInstance.java b/server/src/com/vaadin/util/CurrentInstance.java index d11fa175ac..e6b58ec65c 100644 --- a/server/src/com/vaadin/util/CurrentInstance.java +++ b/server/src/com/vaadin/util/CurrentInstance.java @@ -260,10 +260,9 @@ public class CurrentInstance implements Serializable { * unless it respects null values, will just leave the wrong UI * instance registered. */ - set(c, null, ci.inheritable); - } else { - set(c, v, ci.inheritable); + v = null; } + set(c, v, ci.inheritable); } if (removeStale) { diff --git a/server/tests/src/com/vaadin/util/CurrentInstanceTest.java b/server/tests/src/com/vaadin/util/CurrentInstanceTest.java index 5f9c9638e6..458e8a2f6c 100644 --- a/server/tests/src/com/vaadin/util/CurrentInstanceTest.java +++ b/server/tests/src/com/vaadin/util/CurrentInstanceTest.java @@ -17,6 +17,7 @@ package com.vaadin.util; import static org.junit.Assert.assertNull; +import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -200,4 +201,48 @@ public class CurrentInstanceTest { assertNull(CurrentInstance.get(VaadinSession.class)); assertNull(CurrentInstance.get(VaadinService.class)); } + + @Test + public void testRestoreWithGarbageCollectedValue() + throws InterruptedException { + VaadinSession session1 = new VaadinSession(null) { + @Override + public String toString() { + return "First session"; + } + }; + VaadinSession session2 = new VaadinSession(null) { + @Override + public String toString() { + return "Second session"; + } + }; + + VaadinSession.setCurrent(session1); + Map<Class<?>, CurrentInstance> previous = CurrentInstance + .setCurrent(session2); + + // Use weak ref to verify object is collected + WeakReference<VaadinSession> ref = new WeakReference<VaadinSession>( + session1); + + session1 = null; + waitUntilGarbageCollected(ref); + + CurrentInstance.restoreInstances(previous); + + Assert.assertNull(VaadinSession.getCurrent()); + } + + private static void waitUntilGarbageCollected(WeakReference<?> ref) + throws InterruptedException { + for (int i = 0; i < 50; i++) { + System.gc(); + if (ref.get() == null) { + return; + } + Thread.sleep(100); + } + Assert.fail("Value was not garbage collected."); + } } |