-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8285295: Need better testing for IdentityHashMap #8354
8285295: Need better testing for IdentityHashMap #8354
Conversation
👋 Welcome back smarks! A progress list of the required criteria for merging this PR into |
@stuart-marks The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Just curious, will entrySet/keySet/values tests be here or in a separate file? They are identity-based collections as well, as shown by their contains/removeAll/retainAll. |
Probably the same file. It would be nice to be able to re-use some general Set and Collection tests, but the identity semantics make this difficult to do without extensive refactoring. ** Oh wait, |
@SafeVarargs | ||
private <E> void checkContents(Collection<E> c, BiPredicate<E,E> p, E... given) { | ||
@SuppressWarnings("unchecked") | ||
E[] contents = (E[]) c.toArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worthy to note that entry set toArray may return entries different from what the iterator returns, and some predicates may thus fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I could add a note to caution against checking identity of Entry
instances. That doesn't occur anywhere in these tests, does it?
@SuppressWarnings("unchecked") | ||
E[] contents = (E[]) c.toArray(); | ||
|
||
assertEquals(c.size(), given.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe testng has the expected values in front in the assertEquals
methods, as embodied in the exception messages, so this should be assertEquals(given.length, c.size());
. Applies to other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, TestNG is assertEquals(actual, expected)
which is irritatingly the opposite of JUnit.
This will make things quite tedious when/if we convert to JUnit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s a reason why I prefer using AssertJ, where the calls are:
Assertions.assertThat(actual)
.isEqualTo(expected);
Webrevs
|
@Test | ||
public void testKeySetNoRemove() { | ||
Set<Box> keySet = map.keySet(); | ||
keySet.remove(new Box(k1a)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we assert here that this returns false
?
@Test | ||
public void testKeySetRemove() { | ||
Set<Box> keySet = map.keySet(); | ||
keySet.remove(k1a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly should we assert true
here and few other places in this PR which does the remove?
checkElements(keySet, k1b, k2); | ||
checkEntries(map.entrySet(), entry(k1b, v1b), | ||
entry(k2, v2)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would an additional check assertFalse(map.equals(map2));
be useful here (and other similar tests where we do "remove").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if you noticed that previous versions checked the map's contents with map.equals(map2)
and either asserted true or false depending on the situation. I pulled most of those out when I added checkEntries
. The reason is that checkEntries
and checkElements
are supposed to check the exact contents of the map or the collection, and they fail if there is a missing, different, or extra entry or element. If that's true we don't need to test map.equals
. I don't think it calling map.equals
adds any value or does any checking that the checkX
methods don't already do.
Of course this relies on checkEntries
and checkElements
to do their jobs properly, so we should make sure they do!
And also we need to test that the equals
method is doing its job as well. There are a couple tests for it already, and they test at least the basic cases. But it's possible I might have missed something.
In any case, if we believe the checkX
methods are sufficient, and if we believe that the tests for equals
are also sufficient, then I don't think we need to add assertions about equals
in any tests other than for equals
itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Stuart,
Thank you for the explanation.
In any case, if we believe the checkX methods are sufficient, and if we believe that the tests for equals are also sufficient, then I don't think we need to add assertions about equals in any tests other than for equals itself.
That makes sense.
public void testPutNew() { | ||
Box newKey = new Box(k1a); | ||
Box newVal = new Box(v1a); | ||
map.put(newKey, newVal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we capture the return value and assert that it is null
?
@Test | ||
public void testPutOverwrite() { | ||
Box newVal = new Box(v1a); | ||
map.put(k1a, newVal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we capture the return value and assert that it's not null and is identity equal to v1a
?
Perhaps, similarly at a few other places where we do put and putIfAbsent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good point, I'll add checks of the return values in the appropriate places. There are several, including remove
, computeX
, put
, etc. as you suggest here and in other comments.
I've added checking of return values for I think everything that has a significant return value. I've elected to store the return value in a local variable and add a separate assert line. For example, instead of
I've done
The reason is that I think it separates the test setup/action from the test assertions. I tried it the first way, but it felt like the lack of this separation made things messy. I've also added a couple more tests over Please take a look. |
Hello Stuart, I had a look at the updates. This looks good to me, thank you for the changes. Just a couple of comments/questions that I've included at relevant lines. |
// as its logic for testing collections equality is suspect. Use checkContents() to assert | ||
// that a map's entrySet contains exactly the expected mappings. In most cases, keys and | ||
// values should be compared by identity, not equality. However, the identities of Map.Entry | ||
// instances from an IdentityHashSet are not guaranteed; the keys and values they contain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understand what you are saying here, but it took me a few reads to understand it. More so because of IdentityHashSet
here, which I think is a typo.
So what's being stated here is that you cannot do something like:
IdentityHashMap m = new IdentityHashMap();
...
var e1 = m.entrySet();
var e2 = m.entrySet();
assertSame(e1, e2); // this isn't guaranteed to pass
Did I understand this right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that comment was poorly worded, and indeed I meant the IdentityHashMap's entrySet and not IdentityHashSet. I've reworded it. I was trying to make a statement about exactly what needs to be compared if you want to make assertions about two maps being "equal" in the sense of IdentityHashMap behaving correctly. Specifically, given two maps m1
and m2
, clearly we don't want either of
assertSame(m1, m2);
assertSame(m1.entrySet(), m2.entrySet());
Instead, we want the Map.Entry
instances to "match". However, given two entries entry1
and entry2
that are supposed to match, we also do not want
assertSame(entry1, entry2);
Instead, we want
assertSame(entry1.getKey(), entry2.getKey());
assertSame(entry1.getValue(), entry2,getValue());
The checkEntries
method goes to some length to get this right.
boolean[] called = new boolean[1]; | ||
Box newKey = new Box(k1a); | ||
Box newVal = new Box(v1a); | ||
Box r = map.computeIfAbsent(newKey, k -> { called[0] = true; return newVal; }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More of a curiosity than a review comment - I see that various places in this PR use a boolean array with one element instead of just a boolean type. Is that a personal coding preference or is there something more to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just serves as a modifiable boolean like an AtomicBoolean. Remember lambdas can only use final local var references (due to how they work), and it cannot access or modify the local variable in the caller method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's it; you can't mutate a local variable captured by a lambda.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @liach and Stuart. I had overlooked the lambda aspect of this.
I've added a few more assertions to cover |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Stuart, these changes look good to me.
As I said in GH‑6532:
|
Thanks Jaikiran. Could I have a Reviewer take a look at this please? |
Maybe. The intent of the test is fine, which is to ensure that a default method doesn't get added that breaks the invariants of the wrapper. One problem is that it tests only the default methods of |
Would adding |
It should really be a method name and type whitelist. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you have done a nice job on the coverage.
It would be nice for future maintainers if you consider adding comments for all of the tests vs. a subset prior to pushing
@stuart-marks This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 269 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
Thanks @LanceAndersen I've added some comments. |
/integrate |
Going to push as commit 5a1d8f7.
Your commit was automatically rebased without conflicts. |
@stuart-marks Pushed as commit 5a1d8f7. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Basic but fairly comprehensive set of tests for
IdentityHashMap
. The patch in the bug report that breaksIdentityHashMap
now causes several cases in this new test to fail. There's more that could be done, but the new tests cover most of the core functions ofIdentityHashMap
. Unfortunately it seems difficult to merge this with the existing, comprehensive Collections tests (e.g., MOAT.java) because those tests implicity rely onequals()
-based contract instead of the special-purpose==
-based contract used byIdentityHashMap
.Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/8354/head:pull/8354
$ git checkout pull/8354
Update a local copy of the PR:
$ git checkout pull/8354
$ git pull https://git.openjdk.java.net/jdk pull/8354/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 8354
View PR using the GUI difftool:
$ git pr show -t 8354
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/8354.diff