Skip to content

Introduce custom NameResolver.Args #11669

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

Merged
merged 28 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
707e1da
Introduce NameResolver.Args extensions.
jdcormie Nov 5, 2024
54e089b
Introduce NameResolver.Args extensions.
jdcormie Nov 5, 2024
f2a7b81
Merge remote-tracking branch 'origin/multi-user-nrp' into multi-user-nrp
jdcormie Nov 5, 2024
ce2930f
Introduce NameResolver.Args extensions.
jdcormie Nov 5, 2024
2f30771
fixes
jdcormie Nov 5, 2024
1a21575
understand
jdcormie Nov 5, 2024
07cb7c2
appropriate
jdcormie Nov 5, 2024
169e993
meaning
jdcormie Nov 5, 2024
a5c714a
javadoc
jdcormie Nov 5, 2024
1874264
polish
jdcormie Nov 6, 2024
2fbfc89
value/key
jdcormie Nov 6, 2024
a4b6f07
rename
jdcormie Nov 6, 2024
7f48a33
Address code review comments.
jdcormie Nov 20, 2024
76d1742
javadoc
jdcormie Nov 20, 2024
a216397
Copy forward AttributesTest.java
jdcormie Nov 20, 2024
29d01c7
Beef up unit tests
jdcormie Nov 20, 2024
9fbbc6e
Merge branch 'master' into multi-user-nrp
jdcormie Nov 26, 2024
1a79734
Merge branch 'grpc:master' into multi-user-nrp
jdcormie Dec 10, 2024
9ff19f7
Rename get/setExtension to be like CallOptions#get/setOption
jdcormie Dec 10, 2024
3124a91
Delete Extensions nested class completely.
jdcormie Dec 17, 2024
7653270
A few more extended -> custom renames
jdcormie Dec 17, 2024
5d4a24d
one more extension rename
jdcormie Dec 17, 2024
38946a4
Disallow null arg keys/values just like CallOptions does.
jdcormie Dec 17, 2024
2575fb1
returns
jdcormie Dec 17, 2024
e7fbb5c
extension
jdcormie Dec 18, 2024
2a97cb7
Merge branch `grpc:master` into multi-user-nrp
jdcormie Dec 18, 2024
0857a68
checkNotNull
jdcormie Dec 20, 2024
079a548
Merge branch 'multi-user-nrp' of https://github.com/jdcormie/grpc-jav…
jdcormie Dec 20, 2024
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
Prev Previous commit
Next Next commit
Copy forward AttributesTest.java
  • Loading branch information
jdcormie committed Nov 20, 2024
commit a216397a03c2c1513d66ca3b07f26385f04bc0ea
25 changes: 12 additions & 13 deletions api/src/main/java/io/grpc/NameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -374,10 +375,10 @@
* Gets the value of an "extension" arg by key, or {@code null} if it's not set.
*
* <p>While ordinary {@link Args} should be universally useful and meaningful, extension
* arguments can apply just to resolvers of a certain URI scheme, just to resolvers producing
* a particular type of {@link java.net.SocketAddress}, or even an individual {@link
* NameResolver} subclass. Extension args are identified by an instance of {@link Args.Key}
* which should be defined in a java package and class appropriate to the argument's scope.
* arguments can apply just to resolvers of a certain URI scheme, just to resolvers producing a
* particular type of {@link java.net.SocketAddress}, or even an individual {@link NameResolver}
* subclass. Extension args are identified by an instance of {@link Args.Key} which should be
* defined in a java package and class appropriate to the argument's scope.
*
* <p>{@link Args} are normally reserved for information in *support* of name resolution, not
* the name to be resolved itself. However, there are rare cases where all or part of the target
Expand Down Expand Up @@ -438,7 +439,7 @@
.add("proxyDetector", proxyDetector)
.add("syncContext", syncContext)
.add("serviceConfigParser", serviceConfigParser)
.add("extensions", extensions)

Check warning on line 442 in api/src/main/java/io/grpc/NameResolver.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/NameResolver.java#L442

Added line #L442 was not covered by tests
.add("scheduledExecutorService", scheduledExecutorService)
.add("channelLogger", channelLogger)
.add("executor", executor)
Expand Down Expand Up @@ -575,17 +576,13 @@
return this;
}

/**
* See {@link Args#getExtension(Key)}.
*/
/** See {@link Args#getExtension(Key)}. */
public <T> Builder setExtension(Key<T> key, T value) {
extensionsBuilder.set(key, value);
return this;
}

Check warning on line 584 in api/src/main/java/io/grpc/NameResolver.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/NameResolver.java#L583-L584

Added lines #L583 - L584 were not covered by tests
/**
* Copies each extension argument from 'extensions' into this Builder.
*/
/** Copies each extension argument from 'extensions' into this Builder. */
@Internal
public Builder setAllExtensions(Extensions extensions) {
extensionsBuilder.setAll(extensions);
Expand Down Expand Up @@ -622,7 +619,7 @@
public String toString() {
return debugString;
}

Check warning on line 622 in api/src/main/java/io/grpc/NameResolver.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/NameResolver.java#L622

Added line #L622 was not covered by tests
/**
* Creates a new instance of {@link Key}.
*
Expand Down Expand Up @@ -662,6 +659,10 @@
return (T) data.get(key);
}

Set<Key<?>> keysForTest() {
return Collections.unmodifiableSet(data.keySet());
}

/** Creates a new builder. */
public static Builder newBuilder() {
return new Builder(EMPTY);
Expand All @@ -680,7 +681,7 @@
public String toString() {
return data.toString();
}

Check warning on line 684 in api/src/main/java/io/grpc/NameResolver.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/NameResolver.java#L684

Added line #L684 was not covered by tests
/** Fluently builds instances of {@link Extensions}. */
public static final class Builder {
private Extensions base;
Expand Down Expand Up @@ -708,9 +709,7 @@
return this;
}

/**
* Copies all entries from 'other' into this Builder.
*/
/** Copies all entries from 'other' into this Builder. */
public Builder setAll(Extensions other) {
data(other.data.size()).putAll(other.data);
return this;
Expand All @@ -723,7 +722,7 @@
if (!newdata.containsKey(entry.getKey())) {
newdata.put(entry.getKey(), entry.getValue());
}
}

Check warning on line 725 in api/src/main/java/io/grpc/NameResolver.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/NameResolver.java#L725

Added line #L725 was not covered by tests
base = new Extensions(newdata);
newdata = null;
}
Expand Down
67 changes: 67 additions & 0 deletions api/src/test/java/io/grpc/NameResolverArgsExtensionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertSame;

import io.grpc.NameResolver.Args;
import io.grpc.NameResolver.Args.Extensions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link NameResolver.Args.Extensions}. */
@RunWith(JUnit4.class)
public class NameResolverArgsExtensionsTest {
private static final Args.Key<String> YOLO_KEY = Args.Key.create("yolo");

@Test
public void buildExtensions() {
Extensions attrs = Extensions.newBuilder().set(YOLO_KEY, "To be, or not to be?").build();
assertSame("To be, or not to be?", attrs.get(YOLO_KEY));
assertThat(attrs.keysForTest()).hasSize(1);
}

@Test
public void duplicates() {
Extensions attrs =
Extensions.newBuilder()
.set(YOLO_KEY, "To be?")
.set(YOLO_KEY, "Or not to be?")
.set(Args.Key.create("yolo"), "I'm not a duplicate")
.build();
assertThat(attrs.get(YOLO_KEY)).isEqualTo("Or not to be?");
assertThat(attrs.keysForTest()).hasSize(2);
}

@Test
public void toBuilder() {
Extensions attrs =
Extensions.newBuilder().set(YOLO_KEY, "To be?").build().toBuilder()
.set(YOLO_KEY, "Or not to be?")
.set(Args.Key.create("yolo"), "I'm not a duplicate")
.build();
assertThat(attrs.get(YOLO_KEY)).isEqualTo("Or not to be?");
assertThat(attrs.keysForTest()).hasSize(2);
}

@Test
public void empty() {
assertThat(Extensions.EMPTY.keysForTest()).isEmpty();
}
}
Loading