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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. public MockVaadinSession() throws ServiceException {
  18. super(new MockVaadinServletService());
  19. }
  20. @Override
  21. public void close() {
  22. super.close();
  23. closeCount++;
  24. }
  25. public int getCloseCount() {
  26. return closeCount;
  27. }
  28. @Override
  29. public Lock getLockInstance() {
  30. return lock;
  31. }
  32. @Override
  33. public void lock() {
  34. super.lock();
  35. referenceKeeper.set(this);
  36. }
  37. @Override
  38. public void unlock() {
  39. super.unlock();
  40. referenceKeeper.remove();
  41. }
  42. @Override
  43. public String createConnectorId(ClientConnector connector) {
  44. // Don't delegate to service which may be null or a broken mock
  45. return getNextConnectorId();
  46. }
  47. private int closeCount;
  48. private final ReentrantLock lock = new ReentrantLock();
  49. }