mirror of https://github.com/grpc/grpc-java.git
core: fix bug in CallOptions#withOption()
Previously, overwriting an existing Key would cause the original CallOptions instance to also be mutated. See #5142 Also adds a regression test for this issue.
This commit is contained in:
parent
03300cb2de
commit
01f79bb909
|
@ -326,7 +326,7 @@ public final class CallOptions {
|
|||
newOptions.customOptions[customOptions.length] = new Object[] {key, value};
|
||||
} else {
|
||||
// Replace an existing option
|
||||
newOptions.customOptions[existingIdx][1] = value;
|
||||
newOptions.customOptions[existingIdx] = new Object[] {key, value};
|
||||
}
|
||||
|
||||
return newOptions;
|
||||
|
|
|
@ -196,6 +196,17 @@ public class CallOptionsTest {
|
|||
assertThat(opts.getOption(OPTION_2)).isEqualTo("v2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withOptionDoesNotMutateOriginal() {
|
||||
CallOptions defaultOpt = CallOptions.DEFAULT;
|
||||
CallOptions opt1 = defaultOpt.withOption(OPTION_1, "v1");
|
||||
CallOptions opt2 = opt1.withOption(OPTION_1, "v2");
|
||||
|
||||
assertThat(defaultOpt.getOption(OPTION_1)).isEqualTo("default");
|
||||
assertThat(opt1.getOption(OPTION_1)).isEqualTo("v1");
|
||||
assertThat(opt2.getOption(OPTION_1)).isEqualTo("v2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withStreamTracerFactory() {
|
||||
CallOptions opts1 = CallOptions.DEFAULT.withStreamTracerFactory(tracerFactory1);
|
||||
|
|
Loading…
Reference in New Issue