• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #18793

17 Aug 2023 01:33AM UTC coverage: 88.278% (-0.03%) from 88.304%
#18793

push

github-actions

web-flow
testing: Stabilize GrpcCleanupRule, GrpcServerRule (#10494)

Closes #2488.

30342 of 34371 relevant lines covered (88.28%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/../testing/src/main/java/io/grpc/testing/GrpcServerRule.java
1
/*
2
 * Copyright 2016 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.testing;
18

19
import static com.google.common.base.Preconditions.checkState;
20

21
import io.grpc.BindableService;
22
import io.grpc.ManagedChannel;
23
import io.grpc.Server;
24
import io.grpc.ServerServiceDefinition;
25
import io.grpc.inprocess.InProcessChannelBuilder;
26
import io.grpc.inprocess.InProcessServerBuilder;
27
import io.grpc.stub.AbstractStub;
28
import io.grpc.util.MutableHandlerRegistry;
29
import java.util.UUID;
30
import java.util.concurrent.TimeUnit;
31
import org.junit.rules.ExternalResource;
32
import org.junit.rules.TestRule;
33

34
/**
35
 * {@code GrpcServerRule} is a JUnit {@link TestRule} that starts an in-process gRPC service with
36
 * a {@link MutableHandlerRegistry} for adding services. Prefer {@link GrpcCleanupRule} in new code.
37
 *
38
 * <p>{@code GrpcServerRule} is useful for testing gRPC-based clients and services. However,
39
 * because {@code GrpcServerRule} does not support useful features such as transport
40
 * types other than in-process, multiple channels per server, custom channel or server builder
41
 * options, and configuration inside individual test methods, users would end up to a difficult
42
 * situation when later they want to make extensions to their tests that were using {@code
43
 * GrpcServerRule}. Little benefit comes from proactively migrating existing code from {@code
44
 * GrpcServerRule}, but new code is better served by explicit channel and server creation with
45
 * {@code GrpcCleanupRule} managing resource lifetimes.
46
 *
47
 * <p>An {@link AbstractStub} can be created against this service by using the
48
 * {@link ManagedChannel} provided by {@link GrpcServerRule#getChannel()}.
49
 */
50
public final class GrpcServerRule extends ExternalResource {
1✔
51

52
  private ManagedChannel channel;
53
  private Server server;
54
  private String serverName;
55
  private MutableHandlerRegistry serviceRegistry;
56
  private boolean useDirectExecutor;
57

58
  /**
59
   * Returns {@code this} configured to use a direct executor for the {@link ManagedChannel} and
60
   * {@link Server}. This can only be called at the rule instantiation.
61
   */
62
  public final GrpcServerRule directExecutor() {
63
    checkState(serverName == null, "directExecutor() can only be called at the rule instantiation");
1✔
64
    useDirectExecutor = true;
1✔
65
    return this;
1✔
66
  }
67

68
  /**
69
   * Returns a {@link ManagedChannel} connected to this service.
70
   */
71
  public final ManagedChannel getChannel() {
72
    return channel;
1✔
73
  }
74

75
  /**
76
   * Returns the underlying gRPC {@link Server} for this service.
77
   */
78
  public final Server getServer() {
79
    return server;
1✔
80
  }
81

82
  /**
83
   * Returns the randomly generated server name for this service.
84
   */
85
  public final String getServerName() {
86
    return serverName;
1✔
87
  }
88

89
  /**
90
   * Returns the service registry for this service. The registry is used to add service instances
91
   * (e.g. {@link BindableService} or {@link ServerServiceDefinition} to the server.
92
   */
93
  public final MutableHandlerRegistry getServiceRegistry() {
94
    return serviceRegistry;
1✔
95
  }
96

97
  /**
98
   * After the test has completed, clean up the channel and server.
99
   */
100
  @Override
101
  protected void after() {
102
    serverName = null;
1✔
103
    serviceRegistry = null;
1✔
104

105
    channel.shutdown();
1✔
106
    server.shutdown();
1✔
107

108
    try {
109
      channel.awaitTermination(1, TimeUnit.MINUTES);
1✔
110
      server.awaitTermination(1, TimeUnit.MINUTES);
1✔
111
    } catch (InterruptedException e) {
×
112
      Thread.currentThread().interrupt();
×
113
      throw new RuntimeException(e);
×
114
    } finally {
115
      channel.shutdownNow();
1✔
116
      channel = null;
1✔
117

118
      server.shutdownNow();
1✔
119
      server = null;
1✔
120
    }
121
  }
1✔
122

123
  /**
124
   * Before the test has started, create the server and channel.
125
   */
126
  @Override
127
  protected void before() throws Throwable {
128
    serverName = UUID.randomUUID().toString();
1✔
129

130
    serviceRegistry = new MutableHandlerRegistry();
1✔
131

132
    InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(serverName)
1✔
133
        .fallbackHandlerRegistry(serviceRegistry);
1✔
134

135
    if (useDirectExecutor) {
1✔
136
      serverBuilder.directExecutor();
1✔
137
    }
138

139
    server = serverBuilder.build().start();
1✔
140

141
    InProcessChannelBuilder channelBuilder = InProcessChannelBuilder.forName(serverName);
1✔
142

143
    if (useDirectExecutor) {
1✔
144
      channelBuilder.directExecutor();
1✔
145
    }
146

147
    channel = channelBuilder.build();
1✔
148
  }
1✔
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc