Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2018 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.internal;
     18 
     19 import static org.junit.Assert.assertSame;
     20 import static org.mockito.Matchers.eq;
     21 import static org.mockito.Matchers.same;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import io.grpc.CallOptions;
     27 import io.grpc.ConnectivityState;
     28 import io.grpc.ForwardingTestUtil;
     29 import io.grpc.ManagedChannel;
     30 import io.grpc.MethodDescriptor;
     31 import io.grpc.testing.TestMethodDescriptors;
     32 import java.lang.reflect.Method;
     33 import java.util.Collections;
     34 import java.util.concurrent.TimeUnit;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.junit.runners.JUnit4;
     38 
     39 @RunWith(JUnit4.class)
     40 public final class ForwardingManagedChannelTest {
     41   private final ManagedChannel mock = mock(ManagedChannel.class);
     42   private final ForwardingManagedChannel forward = new ForwardingManagedChannel(mock) {};
     43 
     44   @Test
     45   public void allMethodsForwarded() throws Exception {
     46     ForwardingTestUtil.testMethodsForwarded(
     47         ManagedChannel.class,
     48         mock,
     49         forward,
     50         Collections.<Method>emptyList());
     51   }
     52 
     53   @Test
     54   public void shutdown() {
     55     ManagedChannel ret = mock(ManagedChannel.class);
     56     when(mock.shutdown()).thenReturn(ret);
     57     assertSame(ret, forward.shutdown());
     58   }
     59 
     60   @Test
     61   public void isShutdown() {
     62     when(mock.isShutdown()).thenReturn(true);
     63     assertSame(true, forward.isShutdown());
     64   }
     65 
     66   @Test
     67   public void isTerminated() {
     68     when(mock.isTerminated()).thenReturn(true);
     69     assertSame(true, forward.isTerminated());
     70   }
     71 
     72   @Test
     73   public void shutdownNow() {
     74     ManagedChannel ret = mock(ManagedChannel.class);
     75     when(mock.shutdownNow()).thenReturn(ret);
     76     assertSame(ret, forward.shutdownNow());
     77   }
     78 
     79   @Test
     80   public void awaitTermination() throws Exception {
     81     long timeout = 1234;
     82     TimeUnit unit = TimeUnit.MILLISECONDS;
     83     forward.awaitTermination(timeout, unit);
     84     verify(mock).awaitTermination(eq(timeout), eq(unit));
     85   }
     86 
     87   @Test
     88   public void newCall() {
     89     NoopClientCall<Void, Void> clientCall = new NoopClientCall<Void, Void>();
     90     CallOptions callOptions = CallOptions.DEFAULT.withoutWaitForReady();
     91     MethodDescriptor<Void, Void> method = TestMethodDescriptors.voidMethod();
     92     when(mock.newCall(same(method), same(callOptions))).thenReturn(clientCall);
     93     assertSame(clientCall, forward.newCall(method, callOptions));
     94   }
     95 
     96   @Test
     97   public void authority() {
     98     String authority = "authority5678";
     99     when(mock.authority()).thenReturn(authority);
    100     assertSame(authority, forward.authority());
    101   }
    102 
    103   @Test
    104   public void getState() {
    105     when(mock.getState(true)).thenReturn(ConnectivityState.READY);
    106     assertSame(ConnectivityState.READY, forward.getState(true));
    107   }
    108 
    109   @Test
    110   public void notifyWhenStateChanged() {
    111     Runnable callback = new Runnable() {
    112       @Override
    113       public void run() { }
    114     };
    115     forward.notifyWhenStateChanged(ConnectivityState.READY, callback);
    116     verify(mock).notifyWhenStateChanged(same(ConnectivityState.READY), same(callback));
    117   }
    118 }
    119