Browse Source

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
tags/7.4.0.rc1
Leif Åstrand 9 years ago
parent
commit
fec191879e
1 changed files with 21 additions and 0 deletions
  1. 21
    0
      server/tests/src/com/vaadin/server/MockVaadinSession.java

+ 21
- 0
server/tests/src/com/vaadin/server/MockVaadinSession.java View File

@@ -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();

Loading…
Cancel
Save