@Test public void testEntrySetContainsReflectsMapChanges() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); assertThat(entries.contains(new AbstractMap.SimpleEntry<>("key1", "value1")), is(true)); }
@Test public void testEntrySetIteratorReflectsMapChanges() { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); Iterator<Map.Entry<String, String>> iterator = entries.iterator(); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(true)); assertThat(iterator.next().getKey(), startsWith("key")); assertThat(iterator.hasNext(), is(false)); }
@Test public void testIteration() throws InterruptedException { final ConcurrentWeakIdentityHashMap<Object, Integer> map = new ConcurrentWeakIdentityHashMap<>(); int i = 0; while(i < 10240) { if (i % 1024 == 0) { map.put(i++, i); } else { addToMap(map, i++); } } System.gc(); System.gc(); Thread.sleep(500); System.gc(); int size = 0; // This relies on the entrySet keeping a hard ref to all keys in the map at invocation time for (Map.Entry<Object, Integer> entry : map.entrySet()) { i--; size = map.size(); assertThat(entry.getKey(), notNullValue()); } assertThat(i, not(is(0))); assertThat(size, not(is(0))); }
@Test public void testEntrySetSize() throws Exception { ConcurrentWeakIdentityHashMap<String, String> map = new ConcurrentWeakIdentityHashMap<>(); Set<Map.Entry<String, String>> entries = map.entrySet(); String key1 = "key1"; String key2 = "key2"; String key3 = "key3"; map.put(key1, "value1"); map.put(key2, "value2"); map.put(key3, "value3"); assertThat(entries.size(), is(3)); }