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.

CurrentInstanceTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.util;
  17. import static org.junit.Assert.assertNull;
  18. import java.lang.ref.WeakReference;
  19. import java.lang.reflect.Field;
  20. import java.util.Map;
  21. import java.util.concurrent.atomic.AtomicBoolean;
  22. import org.easymock.EasyMock;
  23. import org.junit.Assert;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import com.vaadin.server.VaadinRequest;
  27. import com.vaadin.server.VaadinService;
  28. import com.vaadin.server.VaadinSession;
  29. import com.vaadin.ui.UI;
  30. public class CurrentInstanceTest {
  31. @Before
  32. public void clearExistingThreadLocals() {
  33. // Ensure no previous test left some thread locals hanging
  34. CurrentInstance.clearAll();
  35. }
  36. @Test
  37. public void testInitiallyCleared() throws Exception {
  38. assertCleared();
  39. }
  40. @Test
  41. public void testClearedAfterRemove() throws Exception {
  42. CurrentInstance.set(CurrentInstanceTest.class, this);
  43. Assert.assertEquals(this,
  44. CurrentInstance.get(CurrentInstanceTest.class));
  45. CurrentInstance.set(CurrentInstanceTest.class, null);
  46. assertCleared();
  47. }
  48. @Test
  49. public void testClearedAfterRemoveInheritable() throws Exception {
  50. CurrentInstance.clearAll();
  51. CurrentInstance.setInheritable(CurrentInstanceTest.class, this);
  52. Assert.assertEquals(this,
  53. CurrentInstance.get(CurrentInstanceTest.class));
  54. CurrentInstance.setInheritable(CurrentInstanceTest.class, null);
  55. assertCleared();
  56. }
  57. @Test
  58. public void testInheritableThreadLocal() throws Exception {
  59. final AtomicBoolean threadFailed = new AtomicBoolean(true);
  60. CurrentInstance.setInheritable(CurrentInstanceTest.class, this);
  61. Assert.assertEquals(this,
  62. CurrentInstance.get(CurrentInstanceTest.class));
  63. Thread t = new Thread() {
  64. @Override
  65. public void run() {
  66. Assert.assertEquals(CurrentInstanceTest.this,
  67. CurrentInstance.get(CurrentInstanceTest.class));
  68. threadFailed.set(false);
  69. }
  70. };
  71. t.start();
  72. CurrentInstance.set(CurrentInstanceTest.class, null);
  73. assertCleared();
  74. while (t.isAlive()) {
  75. Thread.sleep(1000);
  76. }
  77. Assert.assertFalse("Thread failed", threadFailed.get());
  78. }
  79. @Test
  80. public void testClearedAfterRemoveInSeparateThread() throws Exception {
  81. final AtomicBoolean threadFailed = new AtomicBoolean(true);
  82. CurrentInstance.setInheritable(CurrentInstanceTest.class, this);
  83. Assert.assertEquals(this,
  84. CurrentInstance.get(CurrentInstanceTest.class));
  85. Thread t = new Thread() {
  86. @Override
  87. public void run() {
  88. try {
  89. Assert.assertEquals(CurrentInstanceTest.this,
  90. CurrentInstance.get(CurrentInstanceTest.class));
  91. CurrentInstance.set(CurrentInstanceTest.class, null);
  92. assertCleared();
  93. threadFailed.set(false);
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. };
  99. t.start();
  100. while (t.isAlive()) {
  101. Thread.sleep(1000);
  102. }
  103. Assert.assertFalse("Thread failed", threadFailed.get());
  104. // Clearing the threadlocal in the thread should not have cleared it
  105. // here
  106. Assert.assertEquals(this,
  107. CurrentInstance.get(CurrentInstanceTest.class));
  108. // Clearing the only remaining threadlocal should free all memory
  109. CurrentInstance.set(CurrentInstanceTest.class, null);
  110. assertCleared();
  111. }
  112. @Test
  113. public void testClearedWithClearAll() throws Exception {
  114. CurrentInstance.set(CurrentInstanceTest.class, this);
  115. Assert.assertEquals(this,
  116. CurrentInstance.get(CurrentInstanceTest.class));
  117. CurrentInstance.clearAll();
  118. assertCleared();
  119. }
  120. private void assertCleared() throws SecurityException,
  121. NoSuchFieldException, IllegalAccessException {
  122. Assert.assertNull(getInternalCurrentInstanceVariable().get());
  123. }
  124. private InheritableThreadLocal<Map<Class<?>, CurrentInstance>> getInternalCurrentInstanceVariable()
  125. throws SecurityException, NoSuchFieldException,
  126. IllegalAccessException {
  127. Field f = CurrentInstance.class.getDeclaredField("instances");
  128. f.setAccessible(true);
  129. return (InheritableThreadLocal<Map<Class<?>, CurrentInstance>>) f
  130. .get(null);
  131. }
  132. public void testInheritedClearedAfterRemove() {
  133. }
  134. private static class UIStoredInCurrentInstance extends UI {
  135. @Override
  136. protected void init(VaadinRequest request) {
  137. }
  138. }
  139. private static class SessionStoredInCurrentInstance extends VaadinSession {
  140. public SessionStoredInCurrentInstance(VaadinService service) {
  141. super(service);
  142. }
  143. }
  144. @Test
  145. public void testRestoringNullUIWorks() throws Exception {
  146. // First make sure current instance is empty
  147. CurrentInstance.clearAll();
  148. // Then store a new UI in there
  149. Map<Class<?>, CurrentInstance> old = CurrentInstance
  150. .setCurrent(new UIStoredInCurrentInstance());
  151. // Restore the old values and assert that the UI is null again
  152. CurrentInstance.restoreInstances(old);
  153. assertNull(CurrentInstance.get(UI.class));
  154. }
  155. @Test
  156. public void testRestoringNullSessionWorks() throws Exception {
  157. // First make sure current instance is empty
  158. CurrentInstance.clearAll();
  159. // Then store a new session in there
  160. Map<Class<?>, CurrentInstance> old = CurrentInstance
  161. .setCurrent(new SessionStoredInCurrentInstance(EasyMock
  162. .createNiceMock(VaadinService.class)));
  163. // Restore the old values and assert that the session is null again
  164. CurrentInstance.restoreInstances(old);
  165. assertNull(CurrentInstance.get(VaadinSession.class));
  166. assertNull(CurrentInstance.get(VaadinService.class));
  167. }
  168. @Test
  169. public void testRestoreWithGarbageCollectedValue()
  170. throws InterruptedException {
  171. VaadinSession session1 = new VaadinSession(null) {
  172. @Override
  173. public String toString() {
  174. return "First session";
  175. }
  176. };
  177. VaadinSession session2 = new VaadinSession(null) {
  178. @Override
  179. public String toString() {
  180. return "Second session";
  181. }
  182. };
  183. VaadinSession.setCurrent(session1);
  184. Map<Class<?>, CurrentInstance> previous = CurrentInstance
  185. .setCurrent(session2);
  186. // Use weak ref to verify object is collected
  187. WeakReference<VaadinSession> ref = new WeakReference<VaadinSession>(
  188. session1);
  189. session1 = null;
  190. waitUntilGarbageCollected(ref);
  191. CurrentInstance.restoreInstances(previous);
  192. Assert.assertNull(VaadinSession.getCurrent());
  193. }
  194. private static void waitUntilGarbageCollected(WeakReference<?> ref)
  195. throws InterruptedException {
  196. for (int i = 0; i < 50; i++) {
  197. System.gc();
  198. if (ref.get() == null) {
  199. return;
  200. }
  201. Thread.sleep(100);
  202. }
  203. Assert.fail("Value was not garbage collected.");
  204. }
  205. }