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:
Grant Oakley 2018-12-07 16:32:02 -08:00 committed by Carl Mastrangelo
parent 03300cb2de
commit 01f79bb909
2 changed files with 12 additions and 1 deletions

View File

@ -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;

View File

@ -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);