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 static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import io.grpc.CallOptions; 23 import io.grpc.Channel; 24 import io.grpc.stub.AbstractAsyncStubTest.NoopAsyncStub; 25 import io.grpc.stub.AbstractBlockingStubTest.NoopBlockingStub; 26 import io.grpc.stub.AbstractFutureStubTest.NoopFutureStub; 27 import io.grpc.stub.AbstractStub.StubFactory; 28 import io.grpc.stub.ClientCalls.StubType; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 @RunWith(JUnit4.class) 34 public class AbstractAsyncStubTest extends BaseAbstractStubTest<NoopAsyncStub> { 35 36 @Override create(Channel channel, CallOptions callOptions)37 NoopAsyncStub create(Channel channel, CallOptions callOptions) { 38 return new NoopAsyncStub(channel, callOptions); 39 } 40 41 @Test defaultCallOptions()42 public void defaultCallOptions() { 43 NoopAsyncStub stub = NoopAsyncStub.newStub(new StubFactory<NoopAsyncStub>() { 44 @Override 45 public NoopAsyncStub newStub(Channel channel, CallOptions callOptions) { 46 return create(channel, callOptions); 47 } 48 }, channel, CallOptions.DEFAULT); 49 50 assertThat(stub.getCallOptions().getOption(ClientCalls.STUB_TYPE_OPTION)) 51 .isEqualTo(StubType.ASYNC); 52 } 53 54 @Test 55 @SuppressWarnings("AssertionFailureIgnored") newStub_futureStub_throwsException()56 public void newStub_futureStub_throwsException() { 57 try { 58 NoopFutureStub unused = NoopAsyncStub.newStub(new StubFactory<NoopFutureStub>() { 59 @Override 60 public NoopFutureStub newStub(Channel channel, CallOptions callOptions) { 61 return new NoopFutureStub(channel, callOptions); 62 } 63 }, channel, CallOptions.DEFAULT); 64 fail("should not reach here"); 65 } catch (AssertionError e) { 66 assertThat(e).hasMessageThat().startsWith("Expected AbstractAsyncStub"); 67 } 68 } 69 70 @Test 71 @SuppressWarnings("AssertionFailureIgnored") newStub_blockingStub_throwsException()72 public void newStub_blockingStub_throwsException() { 73 try { 74 NoopBlockingStub unused = NoopAsyncStub.newStub(new StubFactory<NoopBlockingStub>() { 75 @Override 76 public NoopBlockingStub newStub(Channel channel, CallOptions callOptions) { 77 return new NoopBlockingStub(channel, callOptions); 78 } 79 }, channel, CallOptions.DEFAULT); 80 fail("should not reach here"); 81 } catch (AssertionError e) { 82 assertThat(e).hasMessageThat().startsWith("Expected AbstractAsyncStub"); 83 } 84 } 85 86 static class NoopAsyncStub extends AbstractAsyncStub<NoopAsyncStub> { 87 NoopAsyncStub(Channel channel, CallOptions options)88 NoopAsyncStub(Channel channel, CallOptions options) { 89 super(channel, options); 90 } 91 92 @Override build(Channel channel, CallOptions callOptions)93 protected NoopAsyncStub build(Channel channel, CallOptions callOptions) { 94 return new NoopAsyncStub(channel, callOptions); 95 } 96 } 97 } 98