Skip to content

Commit 44840fe

Browse files
authored
api: fix bugs of missing to copy customOptions when converting to a new builder, also trash hashCode/equals (#5647)
* api: fix bugs of missing out customOptions in CreateSubchannelArgs toBuider, hashCode, equals * trash equals/hashCode for CreateSubchannelArgs as they are problematic
1 parent 405d8c3 commit 44840fe

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,8 @@ public SubchannelStateListener getStateListener() {
723723
* Returns a builder with the same initial values as this object.
724724
*/
725725
public Builder toBuilder() {
726-
return newBuilder().setAddresses(addrs).setAttributes(attrs).setStateListener(stateListener);
726+
return newBuilder().setAddresses(addrs).setAttributes(attrs).setStateListener(stateListener)
727+
.copyCustomOptions(customOptions);
727728
}
728729

729730
/**
@@ -743,25 +744,6 @@ public String toString() {
743744
.toString();
744745
}
745746

746-
@Override
747-
public int hashCode() {
748-
return Objects.hashCode(addrs, attrs, stateListener);
749-
}
750-
751-
/**
752-
* Returns true if the {@link Subchannel}, {@link Status}, and
753-
* {@link ClientStreamTracer.Factory} all match.
754-
*/
755-
@Override
756-
public boolean equals(Object other) {
757-
if (!(other instanceof CreateSubchannelArgs)) {
758-
return false;
759-
}
760-
CreateSubchannelArgs that = (CreateSubchannelArgs) other;
761-
return Objects.equal(addrs, that.addrs) && Objects.equal(attrs, that.attrs)
762-
&& Objects.equal(stateListener, that.stateListener);
763-
}
764-
765747
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771")
766748
public static final class Builder {
767749

@@ -773,6 +755,12 @@ public static final class Builder {
773755
Builder() {
774756
}
775757

758+
private <T> Builder copyCustomOptions(Object[][] options) {
759+
customOptions = new Object[options.length][2];
760+
System.arraycopy(options, 0, customOptions, 0, options.length);
761+
return this;
762+
}
763+
776764
/**
777765
* Add a custom option. Any existing value for the key is overwritten.
778766
*

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ public void createSubchannelArgs_option_lastOneWins() {
268268
assertThat(args.getOption(testKey)).isEqualTo(testValue2);
269269
}
270270

271+
@Test
272+
public void createSubchannelArgs_build() {
273+
CreateSubchannelArgs.Key<Object> testKey = CreateSubchannelArgs.Key.create("test-key");
274+
Object testValue = new Object();
275+
CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder()
276+
.setAddresses(eag)
277+
.setAttributes(attrs)
278+
.setStateListener(subchannelStateListener)
279+
.addOption(testKey, testValue)
280+
.build();
281+
CreateSubchannelArgs rebuildedArgs = args.toBuilder().build();
282+
assertThat(rebuildedArgs.getAddresses()).containsExactly(eag);
283+
assertThat(rebuildedArgs.getAttributes()).isSameInstanceAs(attrs);
284+
assertThat(rebuildedArgs.getStateListener()).isSameInstanceAs(subchannelStateListener);
285+
assertThat(rebuildedArgs.getOption(testKey)).isSameInstanceAs(testValue);
286+
}
287+
271288
@Test
272289
public void createSubchannelArgs_toString() {
273290
CreateSubchannelArgs.Key<String> testKey = CreateSubchannelArgs.Key.create("test-key");

0 commit comments

Comments
 (0)