You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MockVaadinSession.java 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.vaadin.server;
  2. import java.util.concurrent.locks.Lock;
  3. import java.util.concurrent.locks.ReentrantLock;
  4. public class MockVaadinSession extends VaadinSession {
  5. /*
  6. * Used to make sure there's at least one reference to the mock session
  7. * while it's locked. This is used to prevent the session from being eaten
  8. * by GC in tests where @Before creates a session and sets it as the current
  9. * instance without keeping any direct reference to it. This pattern has a
  10. * chance of leaking memory if the session is not unlocked in the right way,
  11. * but it should be acceptable for testing use.
  12. */
  13. private static final ThreadLocal<MockVaadinSession> referenceKeeper = new ThreadLocal<>();
  14. public MockVaadinSession(VaadinService service) {
  15. super(service);
  16. }
  17. @Override
  18. public void close() {
  19. super.close();
  20. closeCount++;
  21. }
  22. public int getCloseCount() {
  23. return closeCount;
  24. }
  25. @Override
  26. public Lock getLockInstance() {
  27. return lock;
  28. }
  29. @Override
  30. public void lock() {
  31. super.lock();
  32. referenceKeeper.set(this);
  33. }
  34. @Override
  35. public void unlock() {
  36. super.unlock();
  37. referenceKeeper.remove();
  38. }
  39. @Override
  40. public String createConnectorId(ClientConnector connector) {
  41. // Don't delegate to service which may be null or a broken mock
  42. return getNextConnectorId();
  43. }
  44. private int closeCount;
  45. private final ReentrantLock lock = new ReentrantLock();
  46. }