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.okhttp; 18 19 import io.grpc.ServerStreamTracer; 20 import io.grpc.internal.AccessProtectedHack; 21 import io.grpc.internal.ClientTransportFactory; 22 import io.grpc.internal.FakeClock; 23 import io.grpc.internal.InternalServer; 24 import io.grpc.internal.ManagedClientTransport; 25 import io.grpc.internal.testing.AbstractTransportTest; 26 import io.grpc.netty.NettyServerBuilder; 27 import java.net.InetSocketAddress; 28 import java.util.List; 29 import java.util.concurrent.TimeUnit; 30 import org.junit.After; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 /** Unit tests for OkHttp transport. */ 35 @RunWith(JUnit4.class) 36 public class OkHttpTransportTest extends AbstractTransportTest { 37 private final FakeClock fakeClock = new FakeClock(); 38 private ClientTransportFactory clientFactory = 39 OkHttpChannelBuilder 40 // Although specified here, address is ignored because we never call build. 41 .forAddress("localhost", 0) 42 .usePlaintext() 43 .setTransportTracerFactory(fakeClockTransportTracer) 44 .buildTransportFactory(); 45 46 @After releaseClientFactory()47 public void releaseClientFactory() { 48 clientFactory.close(); 49 } 50 51 @Override newServer(List<ServerStreamTracer.Factory> streamTracerFactories)52 protected InternalServer newServer(List<ServerStreamTracer.Factory> streamTracerFactories) { 53 return AccessProtectedHack.serverBuilderBuildTransportServer( 54 NettyServerBuilder 55 .forPort(0) 56 .flowControlWindow(65 * 1024), 57 streamTracerFactories, 58 fakeClockTransportTracer); 59 } 60 61 @Override newServer( InternalServer server, List<ServerStreamTracer.Factory> streamTracerFactories)62 protected InternalServer newServer( 63 InternalServer server, List<ServerStreamTracer.Factory> streamTracerFactories) { 64 int port = server.getPort(); 65 return AccessProtectedHack.serverBuilderBuildTransportServer( 66 NettyServerBuilder 67 .forPort(port) 68 .flowControlWindow(65 * 1024), 69 streamTracerFactories, 70 fakeClockTransportTracer); 71 } 72 73 @Override testAuthority(InternalServer server)74 protected String testAuthority(InternalServer server) { 75 return "thebestauthority:" + server.getPort(); 76 } 77 78 @Override newClientTransport(InternalServer server)79 protected ManagedClientTransport newClientTransport(InternalServer server) { 80 int port = server.getPort(); 81 return clientFactory.newClientTransport( 82 new InetSocketAddress("localhost", port), 83 new ClientTransportFactory.ClientTransportOptions() 84 .setAuthority(testAuthority(server))); 85 } 86 87 @Override advanceClock(long offset, TimeUnit unit)88 protected void advanceClock(long offset, TimeUnit unit) { 89 fakeClock.forwardNanos(unit.toNanos(offset)); 90 } 91 92 @Override fakeCurrentTimeNanos()93 protected long fakeCurrentTimeNanos() { 94 return fakeClock.getTicker().read(); 95 } 96 97 @Override haveTransportTracer()98 protected boolean haveTransportTracer() { 99 return true; 100 } 101 } 102