MCPcopy Create free account
hub / github.com/grpc/grpc-java / GrpcCleanupRule

Class GrpcCleanupRule

testing/src/main/java/io/grpc/testing/GrpcCleanupRule.java:72–280  ·  view source on GitHub ↗

A JUnit ExternalResource 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: {@code @Rule public final GrpcCleanupRule grpcClean

Source from the content-addressed store, hash-verified

70 * @since 1.13.0
71 */
72@NotThreadSafe
73public final class GrpcCleanupRule extends ExternalResource {
74
75 private final List<Resource> resources = new ArrayList<>();
76 private long timeoutNanos = TimeUnit.SECONDS.toNanos(10L);
77 private Stopwatch stopwatch = Stopwatch.createUnstarted();
78
79 private boolean abruptShutdown;
80
81 /**
82 * Sets a positive total time limit for the automatic resource cleanup. If any of the resources
83 * registered to the rule fails to be released in time, the test will fail.
84 *
85 * <p>Note that the resource cleanup duration may or may not be counted as part of the JUnit
86 * {@link org.junit.rules.Timeout Timeout} rule's test duration, depending on which rule is
87 * applied first.
88 *
89 * @return this
90 */
91 public GrpcCleanupRule setTimeout(long timeout, TimeUnit timeUnit) {
92 checkArgument(timeout > 0, "timeout should be positive");
93 timeoutNanos = timeUnit.toNanos(timeout);
94 return this;
95 }
96
97 /**
98 * Sets a specified time source for monitoring cleanup timeout.
99 *
100 * @return this
101 */
102 @SuppressWarnings("BetaApi") // Test only.
103 @VisibleForTesting
104 GrpcCleanupRule setTicker(Ticker ticker) {
105 this.stopwatch = Stopwatch.createUnstarted(ticker);
106 return this;
107 }
108
109 /**
110 * Registers the given channel to the rule. Once registered, the channel will be automatically
111 * shutdown at the end of the test.
112 *
113 * <p>This method need be properly synchronized if used in multiple threads. This method must
114 * not be used during the test teardown.
115 *
116 * @return the input channel
117 */
118 public <T extends ManagedChannel> T register(@Nonnull T channel) {
119 checkNotNull(channel, "channel");
120 register(new ManagedChannelResource(channel));
121 return channel;
122 }
123
124 /**
125 * Registers the given server to the rule. Once registered, the server will be automatically
126 * shutdown at the end of the test.
127 *
128 * <p>This method need be properly synchronized if used in multiple threads. This method must
129 * not be used during the test teardown.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected