-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from bratkartoffel/bugfix/issue-10
Iterating of the map should not throw a ConcurrentModificationException
- Loading branch information
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()); | ||
} | ||
} | ||
} |