|
| 1 | +/* |
| 2 | + * Copyright 2020 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.inprocess; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import io.grpc.InternalChannelz.SocketStats; |
| 21 | +import io.grpc.InternalInstrumented; |
| 22 | +import io.grpc.ServerStreamTracer; |
| 23 | +import io.grpc.internal.AbstractTransportTest; |
| 24 | +import io.grpc.internal.GrpcUtil; |
| 25 | +import io.grpc.internal.InternalServer; |
| 26 | +import io.grpc.internal.ManagedClientTransport; |
| 27 | +import io.grpc.internal.ObjectPool; |
| 28 | +import io.grpc.internal.ServerListener; |
| 29 | +import io.grpc.internal.ServerTransport; |
| 30 | +import io.grpc.internal.ServerTransportListener; |
| 31 | +import io.grpc.internal.SharedResourcePool; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.SocketAddress; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.ScheduledExecutorService; |
| 36 | +import javax.annotation.Nullable; |
| 37 | +import org.junit.Ignore; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.JUnit4; |
| 41 | + |
| 42 | +/** Unit tests for {@link InProcessTransport} when used with a separate {@link InternalServer}. */ |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public final class StandaloneInProcessTransportTest extends AbstractTransportTest { |
| 45 | + private static final String TRANSPORT_NAME = "perfect-for-testing"; |
| 46 | + private static final String AUTHORITY = "a-testing-authority"; |
| 47 | + private static final String USER_AGENT = "a-testing-user-agent"; |
| 48 | + |
| 49 | + private final ObjectPool<ScheduledExecutorService> schedulerPool = |
| 50 | + SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE); |
| 51 | + |
| 52 | + private TestServer currentServer; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected List<? extends InternalServer> newServer( |
| 56 | + List<ServerStreamTracer.Factory> streamTracerFactories) { |
| 57 | + return ImmutableList.of(new TestServer(streamTracerFactories)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected List<? extends InternalServer> newServer( |
| 62 | + int port, List<ServerStreamTracer.Factory> streamTracerFactories) { |
| 63 | + return newServer(streamTracerFactories); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected String testAuthority(InternalServer server) { |
| 68 | + return AUTHORITY; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected ManagedClientTransport newClientTransport(InternalServer server) { |
| 73 | + TestServer testServer = (TestServer) server; |
| 74 | + return InternalInProcess.createInProcessTransport( |
| 75 | + TRANSPORT_NAME, |
| 76 | + GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, |
| 77 | + testAuthority(server), |
| 78 | + USER_AGENT, |
| 79 | + eagAttrs(), |
| 80 | + schedulerPool, |
| 81 | + testServer.streamTracerFactories, |
| 82 | + testServer.serverListener); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected boolean sizesReported() { |
| 87 | + // TODO(zhangkun83): InProcessTransport doesn't record metrics for now |
| 88 | + // (https://github.com/grpc/grpc-java/issues/2284) |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @Ignore |
| 94 | + @Override |
| 95 | + public void socketStats() throws Exception { |
| 96 | + // test does not apply to in-process |
| 97 | + } |
| 98 | + |
| 99 | + /** An internalserver just for this test. */ |
| 100 | + private final class TestServer implements InternalServer { |
| 101 | + |
| 102 | + final List<ServerStreamTracer.Factory> streamTracerFactories; |
| 103 | + ServerListener serverListener; |
| 104 | + |
| 105 | + TestServer(List<ServerStreamTracer.Factory> streamTracerFactories) { |
| 106 | + this.streamTracerFactories = streamTracerFactories; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void start(ServerListener serverListener) throws IOException { |
| 111 | + if (currentServer != null) { |
| 112 | + throw new IOException("Server already present"); |
| 113 | + } |
| 114 | + currentServer = this; |
| 115 | + this.serverListener = new ServerListenerWrapper(serverListener); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void shutdown() { |
| 120 | + currentServer = null; |
| 121 | + serverListener.serverShutdown(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public SocketAddress getListenSocketAddress() { |
| 126 | + return new SocketAddress() {}; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + @Nullable |
| 131 | + public InternalInstrumented<SocketStats> getListenSocketStats() { |
| 132 | + return null; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** Wraps the server listener to ensure we don't accept new transports after shutdown. */ |
| 137 | + private static final class ServerListenerWrapper implements ServerListener { |
| 138 | + private final ServerListener delegateListener; |
| 139 | + private boolean shutdown; |
| 140 | + |
| 141 | + ServerListenerWrapper(ServerListener delegateListener) { |
| 142 | + this.delegateListener = delegateListener; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public ServerTransportListener transportCreated(ServerTransport transport) { |
| 147 | + if (shutdown) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + return delegateListener.transportCreated(transport); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void serverShutdown() { |
| 155 | + shutdown = true; |
| 156 | + delegateListener.serverShutdown(); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments