|
|
@@ -17,6 +17,7 @@ package com.vaadin.util; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertNull; |
|
|
|
|
|
|
|
import java.lang.ref.WeakReference; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
@@ -200,4 +201,48 @@ public class CurrentInstanceTest { |
|
|
|
assertNull(CurrentInstance.get(VaadinSession.class)); |
|
|
|
assertNull(CurrentInstance.get(VaadinService.class)); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testRestoreWithGarbageCollectedValue() |
|
|
|
throws InterruptedException { |
|
|
|
VaadinSession session1 = new VaadinSession(null) { |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
return "First session"; |
|
|
|
} |
|
|
|
}; |
|
|
|
VaadinSession session2 = new VaadinSession(null) { |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
return "Second session"; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
VaadinSession.setCurrent(session1); |
|
|
|
Map<Class<?>, CurrentInstance> previous = CurrentInstance |
|
|
|
.setCurrent(session2); |
|
|
|
|
|
|
|
// Use weak ref to verify object is collected |
|
|
|
WeakReference<VaadinSession> ref = new WeakReference<VaadinSession>( |
|
|
|
session1); |
|
|
|
|
|
|
|
session1 = null; |
|
|
|
waitUntilGarbageCollected(ref); |
|
|
|
|
|
|
|
CurrentInstance.restoreInstances(previous); |
|
|
|
|
|
|
|
Assert.assertNull(VaadinSession.getCurrent()); |
|
|
|
} |
|
|
|
|
|
|
|
private static void waitUntilGarbageCollected(WeakReference<?> ref) |
|
|
|
throws InterruptedException { |
|
|
|
for (int i = 0; i < 50; i++) { |
|
|
|
System.gc(); |
|
|
|
if (ref.get() == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
Thread.sleep(100); |
|
|
|
} |
|
|
|
Assert.fail("Value was not garbage collected."); |
|
|
|
} |
|
|
|
} |