diff options
author | Leif Åstrand <leif@vaadin.com> | 2014-12-09 20:57:16 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-02-05 09:11:14 +0000 |
commit | cb2f385cd0082e6571c0cba1e6fe2969f855daab (patch) | |
tree | 9f21f9854be5f3a881a55f9a051bd28846663e23 | |
parent | 323e8bb4588fcb4eefa6e988cceb8444f963d733 (diff) | |
download | vaadin-framework-cb2f385cd0082e6571c0cba1e6fe2969f855daab.tar.gz vaadin-framework-cb2f385cd0082e6571c0cba1e6fe2969f855daab.zip |
Prevent MockVaadinSession from becomming GCed
This fixes the issue discussed in #14595 for the framework's internal
tests (if they use MockVaadinSession).
Change-Id: I1956680ac065428be41b2ad43fbb80503351b366
-rw-r--r-- | server/tests/src/com/vaadin/server/MockVaadinSession.java | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/server/tests/src/com/vaadin/server/MockVaadinSession.java b/server/tests/src/com/vaadin/server/MockVaadinSession.java index 660767e0d4..e1def5bcee 100644 --- a/server/tests/src/com/vaadin/server/MockVaadinSession.java +++ b/server/tests/src/com/vaadin/server/MockVaadinSession.java @@ -23,6 +23,15 @@ import java.util.concurrent.locks.ReentrantLock; * @author Vaadin Ltd */ public class MockVaadinSession extends VaadinSession { + /* + * Used to make sure there's at least one reference to the mock session + * while it's locked. This is used to prevent the session from being eaten + * by GC in tests where @Before creates a session and sets it as the current + * instance without keeping any direct reference to it. This pattern has a + * chance of leaking memory if the session is not unlocked in the right way, + * but it should be acceptable for testing use. + */ + private static final ThreadLocal<MockVaadinSession> referenceKeeper = new ThreadLocal<MockVaadinSession>(); public MockVaadinSession(VaadinService service) { super(service); @@ -43,6 +52,18 @@ public class MockVaadinSession extends VaadinSession { return lock; } + @Override + public void lock() { + super.lock(); + referenceKeeper.set(this); + } + + @Override + public void unlock() { + super.unlock(); + referenceKeeper.remove(); + } + private int closeCount; private ReentrantLock lock = new ReentrantLock(); |