Skip to content

Commit 9b55aed

Browse files
committed
testing: Make more obvious that GrpcServerRule has been replaced
The previous text did explain to use GrpcCleanupRule, but a user just skimming might not read the long paragraph. Now we have a TL;DR.
1 parent 629748d commit 9b55aed

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

testing/src/main/java/io/grpc/testing/GrpcCleanupRule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@
4141
* the end of the test. If any of the resources registered to the rule can not be successfully
4242
* released, the test will fail.
4343
*
44+
* <p>Example usage:
45+
* <pre>{@code @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
46+
* ...
47+
* // The Channel and Server can be created in any order
48+
* grpcCleanup.register(
49+
* InProcessServerBuilder.forName("my-test-case")
50+
* .directExecutor()
51+
* .addService(serviceImpl)
52+
* .build()
53+
* .start());
54+
* ManagedChannel channel = grpcCleanup.register(
55+
* InProcessChannelBuilder.forName("my-test-case")
56+
* .directExecutor()
57+
* .build());
58+
* }</pre>
59+
*
60+
* <p>To use as a replacement for {@link GrpcServerRule}:
61+
* <pre>{@code String serverName = InProcessServerBuilder.generateName();
62+
* MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
63+
* Server server = grpcCleanup.register(
64+
* InProcessServerBuilder.forName(serverName)
65+
* .fallbackHandlerRegistry(serviceRegistry)
66+
* .build()
67+
* .start());
68+
* ManagedChannel channel = grpcCleanup.register(
69+
* InProcessChannelBuilder.forName(serverName).build());
70+
* }</pre>
71+
*
4472
* @since 1.13.0
4573
*/
4674
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2488")

testing/src/main/java/io/grpc/testing/GrpcServerRule.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@
3434

3535
/**
3636
* {@code GrpcServerRule} is a JUnit {@link TestRule} that starts an in-process gRPC service with
37-
* a {@link MutableHandlerRegistry} for adding services. It is particularly useful for mocking out
38-
* external gRPC-based services and asserting that the expected requests were made. However, due to
39-
* it's limitation that {@code GrpcServerRule} does not support useful features such as transport
37+
* a {@link MutableHandlerRegistry} for adding services. Prefer {@link GrpcCleanupRule} in new code.
38+
*
39+
* <p>{@code GrpcServerRule} is useful for testing gRPC-based clients and services. However,
40+
* because {@code GrpcServerRule} does not support useful features such as transport
4041
* types other than in-process, multiple channels per server, custom channel or server builder
4142
* options, and configuration inside individual test methods, users would end up to a difficult
4243
* situation when later they want to make extensions to their tests that were using {@code
43-
* GrpcServerRule}. So in general it is more favorable to use the more flexible {@code TestRule}
44-
* {@link GrpcCleanupRule} than {@code GrpcServerRule} in unit tests.
44+
* GrpcServerRule}. Little benefit comes from proactively migrating existing code from {@code
45+
* GrpcServerRule}, but new code is better served by explicit channel and server creation with
46+
* {@code GrpcCleanupRule} managing resource lifetimes.
4547
*
4648
* <p>An {@link AbstractStub} can be created against this service by using the
4749
* {@link ManagedChannel} provided by {@link GrpcServerRule#getChannel()}.

0 commit comments

Comments
 (0)