• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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.stub;
18 
19 import io.grpc.CallOptions;
20 import io.grpc.Channel;
21 import io.grpc.stub.ClientCalls.StubType;
22 import javax.annotation.CheckReturnValue;
23 import javax.annotation.concurrent.ThreadSafe;
24 
25 /**
26  * Stub implementations for blocking stubs.
27  *
28  * <p>DO NOT MOCK: Customizing options doesn't work properly in mocks. Use InProcessChannelBuilder
29  * to create a real channel suitable for testing. It is also possible to mock Channel instead.
30  *
31  * @since 1.26.0
32  */
33 @ThreadSafe
34 @CheckReturnValue
35 public abstract class AbstractBlockingStub<S extends AbstractBlockingStub<S>>
36     extends AbstractStub<S> {
37 
AbstractBlockingStub(Channel channel, CallOptions callOptions)38   protected AbstractBlockingStub(Channel channel, CallOptions callOptions) {
39     super(channel, callOptions);
40   }
41 
42   /**
43    * Returns a new blocking stub with the given channel for the provided method configurations.
44    *
45    * @since 1.26.0
46    * @param factory the factory to create a blocking stub
47    * @param channel the channel that this stub will use to do communications
48    */
newStub( StubFactory<T> factory, Channel channel)49   public static <T extends AbstractStub<T>> T newStub(
50       StubFactory<T> factory, Channel channel) {
51     return newStub(factory, channel, CallOptions.DEFAULT);
52   }
53 
54   /**
55    * Returns a new blocking stub with the given channel for the provided method configurations.
56    *
57    * @since 1.26.0
58    * @param factory the factory to create a blocking stub
59    * @param channel the channel that this stub will use to do communications
60    * @param callOptions the runtime call options to be applied to every call on this stub
61    */
newStub( StubFactory<T> factory, Channel channel, CallOptions callOptions)62   public static <T extends AbstractStub<T>> T newStub(
63       StubFactory<T> factory, Channel channel, CallOptions callOptions) {
64     T stub = factory.newStub(
65         channel, callOptions.withOption(ClientCalls.STUB_TYPE_OPTION, StubType.BLOCKING));
66     assert stub instanceof AbstractBlockingStub
67         : String.format("Expected AbstractBlockingStub, but got %s.", stub.getClass());
68     return stub;
69   }
70 }
71