1 /* 2 * Copyright 2014 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 java.util.concurrent.TimeUnit.NANOSECONDS; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotSame; 22 import static org.junit.Assert.assertNull; 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.same; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import io.grpc.CallOptions; 29 import io.grpc.Channel; 30 import io.grpc.ClientCall; 31 import io.grpc.Deadline; 32 import io.grpc.MethodDescriptor; 33 import io.grpc.internal.NoopClientCall; 34 import io.grpc.testing.integration.Messages.SimpleRequest; 35 import io.grpc.testing.integration.Messages.SimpleResponse; 36 import io.grpc.testing.integration.TestServiceGrpc; 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 import org.mockito.ArgumentMatchers; 43 import org.mockito.Mock; 44 import org.mockito.junit.MockitoJUnit; 45 import org.mockito.junit.MockitoRule; 46 47 /** 48 * Tests for stub reconfiguration. 49 */ 50 @RunWith(JUnit4.class) 51 public class StubConfigTest { 52 @Rule public final MockitoRule mocks = MockitoJUnit.rule(); 53 54 @Mock 55 private Channel channel; 56 57 @Mock 58 private StreamObserver<SimpleResponse> responseObserver; 59 60 /** 61 * Sets up mocks. 62 */ setUp()63 @Before public void setUp() { 64 ClientCall<SimpleRequest, SimpleResponse> call = 65 new NoopClientCall<>(); 66 when(channel.newCall( 67 ArgumentMatchers.<MethodDescriptor<SimpleRequest, SimpleResponse>>any(), 68 any(CallOptions.class))) 69 .thenReturn(call); 70 } 71 72 @Test testConfigureDeadline()73 public void testConfigureDeadline() { 74 Deadline deadline = Deadline.after(2, NANOSECONDS); 75 // Create a default stub 76 TestServiceGrpc.TestServiceBlockingStub stub = TestServiceGrpc.newBlockingStub(channel); 77 assertNull(stub.getCallOptions().getDeadline()); 78 // Reconfigure it 79 TestServiceGrpc.TestServiceBlockingStub reconfiguredStub = stub.withDeadline(deadline); 80 // New altered config 81 assertEquals(deadline, reconfiguredStub.getCallOptions().getDeadline()); 82 // Default config unchanged 83 assertNull(stub.getCallOptions().getDeadline()); 84 } 85 86 @Test testStubCallOptionsPopulatedToNewCall()87 public void testStubCallOptionsPopulatedToNewCall() { 88 TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel); 89 CallOptions options1 = stub.getCallOptions(); 90 SimpleRequest request = SimpleRequest.getDefaultInstance(); 91 stub.unaryCall(request, responseObserver); 92 verify(channel).newCall(same(TestServiceGrpc.getUnaryCallMethod()), same(options1)); 93 stub = stub.withDeadlineAfter(2, NANOSECONDS); 94 CallOptions options2 = stub.getCallOptions(); 95 assertNotSame(options1, options2); 96 stub.unaryCall(request, responseObserver); 97 verify(channel).newCall(same(TestServiceGrpc.getUnaryCallMethod()), same(options2)); 98 } 99 } 100