Skip to content

Commit b2cf34c

Browse files
committed
update Status equals&hashCode
1 parent f3f054a commit b2cf34c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

api/src/main/java/io/grpc/Status.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,14 @@ private static String parseAsciiStringSlow(byte[] value) {
656656
* additional fields may be added to Status in the future.
657657
*/
658658
@Override
659-
public boolean equals(Object obj) {
660-
return super.equals(obj);
659+
public boolean equals(Object o) {
660+
if (!(o instanceof Status)) {
661+
return false;
662+
}
663+
Status status = (Status) o;
664+
return code == status.code
665+
&& Objects.equal(description, status.description)
666+
&& Objects.equal(cause, status.cause);
661667
}
662668

663669
/**
@@ -667,6 +673,6 @@ public boolean equals(Object obj) {
667673
*/
668674
@Override
669675
public int hashCode() {
670-
return super.hashCode();
676+
return Objects.hashCode(code, description, cause);
671677
}
672678
}

api/src/test/java/io/grpc/StatusOrTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public void equals_differentStatuses() {
6161
@Test
6262
public void equals_sameStatuses() {
6363
assertThat(StatusOr.fromStatus(Status.ABORTED)).isEqualTo(StatusOr.fromStatus(Status.ABORTED));
64+
assertThat(StatusOr.fromStatus(Status.ABORTED.withDescription("aborted")))
65+
.isEqualTo(StatusOr.fromStatus(Status.ABORTED.withDescription("aborted")));
6466
}
6567

6668
@Test

api/src/test/java/io/grpc/StatusTest.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
2021
import static org.junit.Assert.assertSame;
2122

2223
import io.grpc.Status.Code;
@@ -51,15 +52,25 @@ public void sameCauseReturnsSelf() {
5152
assertSame(Status.CANCELLED, Status.CANCELLED.withCause(null));
5253
}
5354

55+
@Test
56+
public void equalsStatus() {
57+
IllegalStateException ex = new IllegalStateException("The operation was aborted");
58+
assertEquals(Status.ABORTED.withDescription("The operation was aborted")
59+
.withCause(ex),
60+
Status.ABORTED.withDescription("The operation was aborted")
61+
.withCause(ex));
62+
assertNotEquals(Status.ABORTED.withDescription("The operation was aborted"), null);
63+
}
64+
5465
@Test
5566
public void sameDescriptionReturnsSelf() {
5667
assertSame(Status.CANCELLED, Status.CANCELLED.withDescription(null));
5768
assertSame(Status.CANCELLED, Status.CANCELLED.augmentDescription(null));
5869
}
5970

6071
@Test
61-
public void useObjectHashCode() {
62-
assertEquals(Status.CANCELLED.hashCode(), System.identityHashCode(Status.CANCELLED));
72+
public void notUseObjectHashCode() {
73+
assertNotEquals(Status.CANCELLED.hashCode(), System.identityHashCode(Status.CANCELLED));
6374
}
6475

6576
@Test

0 commit comments

Comments
 (0)