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

grpc / grpc-java / #20230

31 Mar 2026 09:55AM UTC coverage: 88.734% (+0.01%) from 88.72%
#20230

push

github

web-flow
openTelemetry: add tcp metrics (#12652)

Implements [A80](https://github.com/grpc/proposal/pull/519)

35697 of 40229 relevant lines covered (88.73%)

0.89 hits per line

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

86.21
/../inprocess/src/main/java/io/grpc/inprocess/InProcessServerBuilder.java
1
/*
2
 * Copyright 2015 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.inprocess;
18

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

21
import com.google.common.base.Preconditions;
22
import com.google.errorprone.annotations.DoNotCall;
23
import io.grpc.Deadline;
24
import io.grpc.ExperimentalApi;
25
import io.grpc.ForwardingServerBuilder;
26
import io.grpc.Internal;
27
import io.grpc.MetricRecorder;
28
import io.grpc.ServerBuilder;
29
import io.grpc.ServerStreamTracer;
30
import io.grpc.internal.FixedObjectPool;
31
import io.grpc.internal.GrpcUtil;
32
import io.grpc.internal.InternalServer;
33
import io.grpc.internal.ObjectPool;
34
import io.grpc.internal.ServerImplBuilder;
35
import io.grpc.internal.ServerImplBuilder.ClientTransportServersBuilder;
36
import io.grpc.internal.SharedResourcePool;
37
import java.io.File;
38
import java.net.SocketAddress;
39
import java.util.List;
40
import java.util.UUID;
41
import java.util.concurrent.ScheduledExecutorService;
42
import java.util.concurrent.TimeUnit;
43

44
/**
45
 * Builder for a server that services in-process requests. Clients identify the in-process server by
46
 * its name.
47
 *
48
 * <p>The server is intended to be fully-featured, high performance, and useful in testing.
49
 *
50
 * <h3>Using JUnit TestRule</h3>
51
 * The class "GrpcServerRule" (from "grpc-java/testing") is a JUnit TestRule that
52
 * creates a {@link InProcessServer} and a {@link io.grpc.ManagedChannel ManagedChannel}. This
53
 * test rule contains the boilerplate code shown below. The classes "HelloWorldServerTest" and
54
 * "HelloWorldClientTest" (from "grpc-java/examples") demonstrate basic usage.
55
 *
56
 * <h3>Usage example</h3>
57
 * <h4>Server and client channel setup</h4>
58
 * <pre>
59
 *   String uniqueName = InProcessServerBuilder.generateName();
60
 *   Server server = InProcessServerBuilder.forName(uniqueName)
61
 *       .directExecutor() // directExecutor is fine for unit tests
62
 *       .addService(&#47;* your code here *&#47;)
63
 *       .build().start();
64
 *   ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName)
65
 *       .directExecutor()
66
 *       .build();
67
 * </pre>
68
 *
69
 * <h4>Usage in tests</h4>
70
 * The channel can be used normally. A blocking stub example:
71
 * <pre>
72
 *   TestServiceGrpc.TestServiceBlockingStub blockingStub =
73
 *       TestServiceGrpc.newBlockingStub(channel);
74
 * </pre>
75
 */
76
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783")
77
public final class InProcessServerBuilder extends ForwardingServerBuilder<InProcessServerBuilder> {
78
  /**
79
   * Create a server builder that will bind with the given name.
80
   *
81
   * @param name the identity of the server for clients to connect to
82
   * @return a new builder
83
   */
84
  public static InProcessServerBuilder forName(String name) {
85
    return forAddress(new InProcessSocketAddress(checkNotNull(name, "name")));
1✔
86
  }
87

88
  /**
89
   * Create a server builder which listens on the given address.
90
   * @param listenAddress The SocketAddress this server will listen on.
91
   * @return a new builder
92
   */
93
  public static InProcessServerBuilder forAddress(SocketAddress listenAddress) {
94
    return new InProcessServerBuilder(listenAddress);
1✔
95
  }
96

97
  /**
98
   * Always fails.  Call {@link #forName} instead.
99
   */
100
  @DoNotCall("Unsupported. Use forName() instead")
101
  public static InProcessServerBuilder forPort(int port) {
102
    throw new UnsupportedOperationException("call forName() instead");
×
103
  }
104

105
  /**
106
   * Generates a new server name that is unique each time.
107
   */
108
  public static String generateName() {
109
    return UUID.randomUUID().toString();
1✔
110
  }
111

112
  private final ServerImplBuilder serverImplBuilder;
113
  final SocketAddress listenAddress;
114
  int maxInboundMetadataSize = Integer.MAX_VALUE;
1✔
115
  ObjectPool<ScheduledExecutorService> schedulerPool =
1✔
116
      SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE);
1✔
117

118
  private InProcessServerBuilder(SocketAddress listenAddress) {
1✔
119
    this.listenAddress = checkNotNull(listenAddress, "listenAddress");
1✔
120

121
    final class InProcessClientTransportServersBuilder implements ClientTransportServersBuilder {
1✔
122
      @Override
123
      public InternalServer buildClientTransportServers(
124
          List<? extends ServerStreamTracer.Factory> streamTracerFactories,
125
          MetricRecorder metricRecorder) {
126
        return buildTransportServers(streamTracerFactories);
1✔
127
      }
128
    }
129

130
    serverImplBuilder = new ServerImplBuilder(new InProcessClientTransportServersBuilder());
1✔
131

132
    // In-process transport should not record its traffic to the stats module.
133
    // https://github.com/grpc/grpc-java/issues/2284
134
    serverImplBuilder.setStatsRecordStartedRpcs(false);
1✔
135
    serverImplBuilder.setStatsRecordFinishedRpcs(false);
1✔
136
    // Disable handshake timeout because it is unnecessary, and can trigger Thread creation that can
137
    // break some environments (like tests).
138
    handshakeTimeout(Long.MAX_VALUE, TimeUnit.SECONDS);
1✔
139
  }
1✔
140

141
  @Internal
142
  @Override
143
  protected ServerBuilder<?> delegate() {
144
    return serverImplBuilder;
1✔
145
  }
146

147
  /**
148
   * Provides a custom scheduled executor service.
149
   *
150
   * <p>It's an optional parameter. If the user has not provided a scheduled executor service when
151
   * the channel is built, the builder will use a static cached thread pool.
152
   *
153
   * @return this
154
   *
155
   * @since 1.11.0
156
   */
157
  public InProcessServerBuilder scheduledExecutorService(
158
      ScheduledExecutorService scheduledExecutorService) {
159
    schedulerPool = new FixedObjectPool<>(
1✔
160
        checkNotNull(scheduledExecutorService, "scheduledExecutorService"));
1✔
161
    return this;
1✔
162
  }
163

164
  /**
165
   * Provides a custom deadline ticker that this server will use to create incoming {@link
166
   * Deadline}s.
167
   *
168
   * <p>This is intended for unit tests that fake out the clock.  You should also have a fake {@link
169
   * ScheduledExecutorService} whose clock is synchronized with this ticker and set it to {@link
170
   * #scheduledExecutorService}. DO NOT use this in production.
171
   *
172
   * @return this
173
   * @see Deadline#after(long, TimeUnit, Deadline.Ticker)
174
   *
175
   * @since 1.24.0
176
   */
177
  public InProcessServerBuilder deadlineTicker(Deadline.Ticker ticker) {
178
    serverImplBuilder.setDeadlineTicker(ticker);
×
179
    return this;
×
180
  }
181

182
  /**
183
   * Sets the maximum size of metadata allowed to be received. {@code Integer.MAX_VALUE} disables
184
   * the enforcement. Defaults to no limit ({@code Integer.MAX_VALUE}).
185
   *
186
   * <p>There is potential for performance penalty when this setting is enabled, as the Metadata
187
   * must actually be serialized. Since the current implementation of Metadata pre-serializes, it's
188
   * currently negligible. But Metadata is free to change its implementation.
189
   *
190
   * @param bytes the maximum size of received metadata
191
   * @return this
192
   * @throws IllegalArgumentException if bytes is non-positive
193
   * @since 1.17.0
194
   */
195
  @Override
196
  public InProcessServerBuilder maxInboundMetadataSize(int bytes) {
197
    Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
1✔
198
    this.maxInboundMetadataSize = bytes;
1✔
199
    return this;
1✔
200
  }
201

202
  InProcessServer buildTransportServers(
203
      List<? extends ServerStreamTracer.Factory> streamTracerFactories) {
204
    return new InProcessServer(this, streamTracerFactories);
1✔
205
  }
206

207
  @Override
208
  public InProcessServerBuilder useTransportSecurity(File certChain, File privateKey) {
209
    throw new UnsupportedOperationException("TLS not supported in InProcessServer");
×
210
  }
211

212
  void setStatsEnabled(boolean value) {
213
    this.serverImplBuilder.setStatsEnabled(value);
1✔
214
  }
1✔
215
}
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