• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.stub;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.mock;
24 
25 import io.grpc.CallOptions;
26 import io.grpc.Channel;
27 import java.util.concurrent.Executor;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 
35 @RunWith(JUnit4.class)
36 public class AbstractStubTest {
37 
38   @Mock
39   Channel channel;
40 
41   @Before
setup()42   public void setup() {
43     MockitoAnnotations.initMocks(this);
44   }
45 
46   @Test(expected = NullPointerException.class)
channelMustNotBeNull()47   public void channelMustNotBeNull() {
48     new NoopStub(null);
49   }
50 
51   @Test(expected = NullPointerException.class)
callOptionsMustNotBeNull()52   public void callOptionsMustNotBeNull() {
53     new NoopStub(channel, null);
54   }
55 
56   @Test(expected = NullPointerException.class)
channelMustNotBeNull2()57   public void channelMustNotBeNull2() {
58     new NoopStub(null, CallOptions.DEFAULT);
59   }
60 
61   @Test()
withWaitForReady()62   public void withWaitForReady() {
63     NoopStub stub = new NoopStub(channel);
64     CallOptions callOptions = stub.getCallOptions();
65     assertFalse(callOptions.isWaitForReady());
66 
67     stub = stub.withWaitForReady();
68     callOptions = stub.getCallOptions();
69     assertTrue(callOptions.isWaitForReady());
70   }
71 
72   class NoopStub extends AbstractStub<NoopStub> {
73 
NoopStub(Channel channel)74     NoopStub(Channel channel) {
75       super(channel);
76     }
77 
NoopStub(Channel channel, CallOptions options)78     NoopStub(Channel channel, CallOptions options) {
79       super(channel, options);
80     }
81 
82     @Override
build(Channel channel, CallOptions callOptions)83     protected NoopStub build(Channel channel, CallOptions callOptions) {
84       return new NoopStub(channel, callOptions);
85     }
86   }
87 
88   @Test
withExecutor()89   public void withExecutor() {
90     NoopStub stub = new NoopStub(channel);
91     CallOptions callOptions = stub.getCallOptions();
92 
93     assertNull(callOptions.getExecutor());
94 
95     Executor executor = mock(Executor.class);
96     stub = stub.withExecutor(executor);
97     callOptions = stub.getCallOptions();
98 
99     assertEquals(callOptions.getExecutor(), executor);
100   }
101 }
102