BAEL-672 test for weakHashMap
This commit is contained in:
parent
7664601b7d
commit
c791bdf1dc
|
@ -154,6 +154,12 @@
|
|||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
|
@ -371,6 +377,7 @@
|
|||
<mockito.version>1.10.19</mockito.version>
|
||||
<testng.version>6.10</testng.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.weakhashmap;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.jayway.awaitility.Awaitility.await;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class WeakHashMapTest {
|
||||
|
||||
@Test
|
||||
public void givenWeakHashMap_whenCacheValueThatHasNoReferenceToIt_GCShouldReclaimThatObject() {
|
||||
//given
|
||||
WeakHashMap<UniqueImageName, BigImage> map = new WeakHashMap<>();
|
||||
BigImage bigImage = new BigImage("foo");
|
||||
UniqueImageName imageName = new UniqueImageName("name_of_big_image");
|
||||
|
||||
map.put(imageName, bigImage);
|
||||
assertTrue(map.containsKey(imageName));
|
||||
|
||||
//when big image is not in use anymore
|
||||
imageName = null;
|
||||
System.gc();
|
||||
|
||||
//then GC will finally reclaim that object
|
||||
await().atMost(10, TimeUnit.SECONDS).until(map::isEmpty);
|
||||
}
|
||||
|
||||
|
||||
class BigImage {
|
||||
public final String imageId;
|
||||
|
||||
BigImage(String imageId) {
|
||||
this.imageId = imageId;
|
||||
}
|
||||
}
|
||||
|
||||
class UniqueImageName {
|
||||
public final String imageName;
|
||||
|
||||
UniqueImageName(String imageName) {
|
||||
this.imageName = imageName;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue