Skip to content

Add iterator fail-fast tests for LinkedTreeMap.clear()#2992

Merged
eamonnmcmanus merged 6 commits into
google:mainfrom
lmj798:codex/linkedtreemap-clear-tests
Mar 10, 2026
Merged

Add iterator fail-fast tests for LinkedTreeMap.clear()#2992
eamonnmcmanus merged 6 commits into
google:mainfrom
lmj798:codex/linkedtreemap-clear-tests

Conversation

@lmj798

@lmj798 lmj798 commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Add iterator fail-fast validation tests for LinkedTreeMap.clear() method.

Why

The clear() method increments modCount to ensure existing iterators become invalid after the map is cleared. This is a critical fail-fast contract that prevents silent data corruption during iteration.
The added tests verify that:

  1. EntrySet iterator throws ConcurrentModificationException after clear()
  2. KeySet iterator throws ConcurrentModificationException after clear()
    These tests ensure the fail-fast mechanism remains intact, which is essential for preventing concurrent modification bugs in production code.

Verification

Ran mvn -pl gson -Dtest=LinkedTreeMapTest
Result: all tests passed

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this makes sense!

@eamonnmcmanus eamonnmcmanus merged commit b7d5954 into google:main Mar 10, 2026
16 checks passed
utsavatwork pushed a commit to utsavatwork/gson that referenced this pull request Mar 12, 2026
* Add boundary tests for JsonTreeWriter.endArray() error handling

* Add comprehensive boundary tests for LinkedTreeMap.clear() method

* Update JsonTreeWriterTest.java

* Remove redundant clear map tests from LinkedTreeMapTest

Removed multiple test cases for clearing LinkedTreeMap, including tests for single and multiple entries, null values, and various scenarios.

* Remove blank line in LinkedTreeMapTest

Removed unnecessary blank line in LinkedTreeMapTest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants