Package io.grpc.testing
Class GrpcCleanupRule
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- io.grpc.testing.GrpcCleanupRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
@NotThreadSafe public final class GrpcCleanupRule extends org.junit.rules.ExternalResource
A JUnitExternalResource
that can register gRPC resources and manages its automatic release at the end of the test. If any of the resources registered to the rule can not be successfully released, the test will fail.Example usage:
@Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); ... // The Channel and Server can be created in any order grpcCleanup.register( InProcessServerBuilder.forName("my-test-case") .directExecutor() .addService(serviceImpl) .build() .start()); ManagedChannel channel = grpcCleanup.register( InProcessChannelBuilder.forName("my-test-case") .directExecutor() .build());
To use as a replacement for
GrpcServerRule
:String serverName = InProcessServerBuilder.generateName(); MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); Server server = grpcCleanup.register( InProcessServerBuilder.forName(serverName) .fallbackHandlerRegistry(serviceRegistry) .build() .start()); ManagedChannel channel = grpcCleanup.register( InProcessChannelBuilder.forName(serverName).build());
- Since:
- 1.13.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GrpcCleanupRule.ManagedChannelResource
(package private) static interface
GrpcCleanupRule.Resource
private static class
GrpcCleanupRule.ServerResource
-
Field Summary
Fields Modifier and Type Field Description private boolean
abruptShutdown
private java.util.List<GrpcCleanupRule.Resource>
resources
private com.google.common.base.Stopwatch
stopwatch
private long
timeoutNanos
-
Constructor Summary
Constructors Constructor Description GrpcCleanupRule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
after()
Releases all the registered resources.org.junit.runners.model.Statement
apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
(package private) void
register(GrpcCleanupRule.Resource resource)
<T extends io.grpc.ManagedChannel>
Tregister(T channel)
Registers the given channel to the rule.(package private) GrpcCleanupRule
setTicker(com.google.common.base.Ticker ticker)
Sets a specified time source for monitoring cleanup timeout.GrpcCleanupRule
setTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
Sets a positive total time limit for the automatic resource cleanup.
-
-
-
Field Detail
-
resources
private final java.util.List<GrpcCleanupRule.Resource> resources
-
timeoutNanos
private long timeoutNanos
-
stopwatch
private com.google.common.base.Stopwatch stopwatch
-
abruptShutdown
private boolean abruptShutdown
-
-
Method Detail
-
setTimeout
public GrpcCleanupRule setTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
Sets a positive total time limit for the automatic resource cleanup. If any of the resources registered to the rule fails to be released in time, the test will fail.Note that the resource cleanup duration may or may not be counted as part of the JUnit
Timeout
rule's test duration, depending on which rule is applied first.- Returns:
- this
-
setTicker
GrpcCleanupRule setTicker(com.google.common.base.Ticker ticker)
Sets a specified time source for monitoring cleanup timeout.- Returns:
- this
-
register
public <T extends io.grpc.ManagedChannel> T register(@Nonnull T channel)
Registers the given channel to the rule. Once registered, the channel will be automatically shutdown at the end of the test.This method need be properly synchronized if used in multiple threads. This method must not be used during the test teardown.
- Returns:
- the input channel
-
register
public <T extends io.grpc.Server> T register(@Nonnull T server)
Registers the given server to the rule. Once registered, the server will be automatically shutdown at the end of the test.This method need be properly synchronized if used in multiple threads. This method must not be used during the test teardown.
- Returns:
- the input server
-
register
void register(GrpcCleanupRule.Resource resource)
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
- Specified by:
apply
in interfaceorg.junit.rules.TestRule
- Overrides:
apply
in classorg.junit.rules.ExternalResource
-
after
protected void after()
Releases all the registered resources.- Overrides:
after
in classorg.junit.rules.ExternalResource
-
-