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.Matchers.any; 24 import static org.mockito.Matchers.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.Test; 39 import org.junit.runner.RunWith; 40 import org.junit.runners.JUnit4; 41 import org.mockito.Mock; 42 import org.mockito.Mockito; 43 import org.mockito.MockitoAnnotations; 44 45 /** 46 * Tests for stub reconfiguration. 47 */ 48 @RunWith(JUnit4.class) 49 public class StubConfigTest { 50 51 @Mock 52 private Channel channel; 53 54 @Mock 55 private StreamObserver<SimpleResponse> responseObserver; 56 57 /** 58 * Sets up mocks. 59 */ setUp()60 @Before public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 ClientCall<SimpleRequest, SimpleResponse> call = 63 new NoopClientCall<SimpleRequest, SimpleResponse>(); 64 when(channel.newCall( 65 Mockito.<MethodDescriptor<SimpleRequest, SimpleResponse>>any(), any(CallOptions.class))) 66 .thenReturn(call); 67 } 68 69 @Test testConfigureDeadline()70 public void testConfigureDeadline() { 71 Deadline deadline = Deadline.after(2, NANOSECONDS); 72 // Create a default stub 73 TestServiceGrpc.TestServiceBlockingStub stub = TestServiceGrpc.newBlockingStub(channel); 74 assertNull(stub.getCallOptions().getDeadline()); 75 // Reconfigure it 76 TestServiceGrpc.TestServiceBlockingStub reconfiguredStub = stub.withDeadline(deadline); 77 // New altered config 78 assertEquals(deadline, reconfiguredStub.getCallOptions().getDeadline()); 79 // Default config unchanged 80 assertNull(stub.getCallOptions().getDeadline()); 81 } 82 83 @Test testStubCallOptionsPopulatedToNewCall()84 public void testStubCallOptionsPopulatedToNewCall() { 85 TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel); 86 CallOptions options1 = stub.getCallOptions(); 87 SimpleRequest request = SimpleRequest.getDefaultInstance(); 88 stub.unaryCall(request, responseObserver); 89 verify(channel).newCall(same(TestServiceGrpc.getUnaryCallMethod()), same(options1)); 90 stub = stub.withDeadlineAfter(2, NANOSECONDS); 91 CallOptions options2 = stub.getCallOptions(); 92 assertNotSame(options1, options2); 93 stub.unaryCall(request, responseObserver); 94 verify(channel).newCall(same(TestServiceGrpc.getUnaryCallMethod()), same(options2)); 95 } 96 } 97