Add tests to CopyOnWriteMultisetTest for modification during iteration
This is a key use-case for copy-on-write collections. PiperOrigin-RevId: 358807866
This commit is contained in:
parent
623597addb
commit
c64a1a0c3c
@ -117,4 +117,34 @@ public final class CopyOnWriteMultisetTest {
|
|||||||
assertThat(multiset.count("a string")).isEqualTo(2);
|
assertThat(multiset.count("a string")).isEqualTo(2);
|
||||||
assertThat(multiset.count("another string")).isEqualTo(0);
|
assertThat(multiset.count("another string")).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void modifyingWhileIteratingElements_succeeds() {
|
||||||
|
CopyOnWriteMultiset<String> multiset = new CopyOnWriteMultiset<>();
|
||||||
|
multiset.add("a string");
|
||||||
|
multiset.add("a string");
|
||||||
|
multiset.add("another string");
|
||||||
|
|
||||||
|
// A traditional collection would throw a ConcurrentModificationException here.
|
||||||
|
for (String element : multiset) {
|
||||||
|
multiset.remove(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(multiset).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void modifyingWhileIteratingElementSet_succeeds() {
|
||||||
|
CopyOnWriteMultiset<String> multiset = new CopyOnWriteMultiset<>();
|
||||||
|
multiset.add("a string");
|
||||||
|
multiset.add("a string");
|
||||||
|
multiset.add("another string");
|
||||||
|
|
||||||
|
// A traditional collection would throw a ConcurrentModificationException here.
|
||||||
|
for (String element : multiset.elementSet()) {
|
||||||
|
multiset.remove(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(multiset).containsExactly("a string");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user