-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iterating of the map should not throw an ConcurrentModificationException
Create a copy of the entries (guarded by a readLock) and iterate over that copy. This fixes #10
- Loading branch information
1 parent
b6a544d
commit 523fe77
Showing
3 changed files
with
90 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.jodah.expiringmap.issues; | ||
|
||
import net.jodah.expiringmap.ExpirationPolicy; | ||
import net.jodah.expiringmap.ExpiringMap; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Do not throw ConcurrentModificationException when using an Iterator | ||
*/ | ||
@Test | ||
public class Issue10 { | ||
public void testEntrySet() { | ||
Map<Integer, Integer> map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).expirationPolicy(ExpirationPolicy.ACCESSED).build(); | ||
map.put(1, 1); | ||
map.put(2, 2); | ||
Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); | ||
Assert.assertEquals((Integer) 1, iterator.next().getKey()); | ||
map.put(3, 3); | ||
Assert.assertEquals((Integer) 2, iterator.next().getKey()); | ||
// we don't require the iterator to be updated with the new '3' entry and we neither expect a ConcurrentModificationException | ||
if (iterator.hasNext()) { | ||
Assert.assertEquals((Integer) 3, iterator.next().getKey()); | ||
} | ||
} | ||
|
||
public void testValues() { | ||
Map<Integer, Integer> map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).expirationPolicy(ExpirationPolicy.ACCESSED).build(); | ||
map.put(1, 1); | ||
map.put(2, 2); | ||
Iterator<Integer> iterator = map.values().iterator(); | ||
Assert.assertEquals((Integer) 1, iterator.next()); | ||
map.put(3, 3); | ||
Assert.assertEquals((Integer) 2, iterator.next()); | ||
// we don't require the iterator to be updated with the new '3' entry and we neither expect a ConcurrentModificationException | ||
if (iterator.hasNext()) { | ||
Assert.assertEquals((Integer) 3, iterator.next()); | ||
} | ||
} | ||
|
||
public void testKeySet() { | ||
Map<Integer, Integer> map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).expirationPolicy(ExpirationPolicy.ACCESSED).build(); | ||
map.put(1, 1); | ||
map.put(2, 2); | ||
Iterator<Integer> iterator = map.keySet().iterator(); | ||
Assert.assertEquals((Integer) 1, iterator.next()); | ||
map.put(3, 3); | ||
Assert.assertEquals((Integer) 2, iterator.next()); | ||
// we don't require the iterator to be updated with the new '3' entry and we neither expect a ConcurrentModificationException | ||
if (iterator.hasNext()) { | ||
Assert.assertEquals((Integer) 3, iterator.next()); | ||
} | ||
} | ||
} |