Skip to content
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

api: update Status equals&hashCode #11953

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions api/src/main/java/io/grpc/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,14 @@ private static String parseAsciiStringSlow(byte[] value) {
* additional fields may be added to Status in the future.
*/
@Override
public boolean equals(Object obj) {
return super.equals(obj);
public boolean equals(Object o) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment suggests to just do comparison on the status code as the description and cause are unlikely to be stable. It may be better to do any handling of the comparison logic at the caller site, i.e., StatusOr.equals in this case. Also whether that comparison should just consider the status code or all 3 fields is another question. I will schedule this for discussion in our next week's API review meeting.

if (!(o instanceof Status)) {
return false;
}
Status status = (Status) o;
return code == status.code
&& Objects.equal(description, status.description)
&& Objects.equal(cause, status.cause);
}

/**
Expand All @@ -667,6 +673,6 @@ public boolean equals(Object obj) {
*/
@Override
public int hashCode() {
return super.hashCode();
return Objects.hashCode(code, description, cause);
}
}
2 changes: 2 additions & 0 deletions api/src/test/java/io/grpc/StatusOrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void equals_differentStatuses() {
@Test
public void equals_sameStatuses() {
assertThat(StatusOr.fromStatus(Status.ABORTED)).isEqualTo(StatusOr.fromStatus(Status.ABORTED));
assertThat(StatusOr.fromStatus(Status.ABORTED.withDescription("aborted")))
.isEqualTo(StatusOr.fromStatus(Status.ABORTED.withDescription("aborted")));
}

@Test
Expand Down
15 changes: 13 additions & 2 deletions api/src/test/java/io/grpc/StatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;

import io.grpc.Status.Code;
Expand Down Expand Up @@ -51,15 +52,25 @@ public void sameCauseReturnsSelf() {
assertSame(Status.CANCELLED, Status.CANCELLED.withCause(null));
}

@Test
public void equalsStatus() {
IllegalStateException ex = new IllegalStateException("The operation was aborted");
assertEquals(Status.ABORTED.withDescription("The operation was aborted")
.withCause(ex),
Status.ABORTED.withDescription("The operation was aborted")
.withCause(ex));
assertNotEquals(Status.ABORTED.withDescription("The operation was aborted"), null);
}

@Test
public void sameDescriptionReturnsSelf() {
assertSame(Status.CANCELLED, Status.CANCELLED.withDescription(null));
assertSame(Status.CANCELLED, Status.CANCELLED.augmentDescription(null));
}

@Test
public void useObjectHashCode() {
assertEquals(Status.CANCELLED.hashCode(), System.identityHashCode(Status.CANCELLED));
public void notUseObjectHashCode() {
assertNotEquals(Status.CANCELLED.hashCode(), System.identityHashCode(Status.CANCELLED));
}

@Test
Expand Down