1 /* 2 * Copyright 2017 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.testing.integration; 18 19 import com.google.protobuf.ByteString; 20 import io.grpc.ManagedChannel; 21 import io.grpc.ManagedChannelBuilder; 22 import io.grpc.testing.integration.Messages.Payload; 23 import io.grpc.testing.integration.Messages.SimpleRequest; 24 import io.grpc.testing.integration.Messages.SimpleResponse; 25 import java.io.IOException; 26 import java.util.concurrent.TimeUnit; 27 import javax.servlet.http.HttpServlet; 28 import javax.servlet.http.HttpServletRequest; 29 import javax.servlet.http.HttpServletResponse; 30 31 /** 32 * A test class that checks we can reuse a channel across requests. 33 * 34 * <p>This servlet communicates with {@code grpc-test.sandbox.googleapis.com}, which is a server 35 * managed by the gRPC team. For more information, see 36 * <a href="https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md"> 37 * Interoperability Test Case Descriptions</a>. 38 */ 39 @SuppressWarnings("serial") 40 public final class LongLivedChannel extends HttpServlet { 41 private static final String INTEROP_TEST_ADDRESS = "grpc-test.sandbox.googleapis.com:443"; 42 private final ManagedChannel channel = 43 ManagedChannelBuilder.forTarget(INTEROP_TEST_ADDRESS).build(); 44 45 @Override destroy()46 public void destroy() { 47 try { 48 channel.shutdownNow().awaitTermination(1, TimeUnit.SECONDS); 49 } catch (Exception e) { 50 throw new RuntimeException(e); 51 } 52 } 53 54 @Override doGet(HttpServletRequest req, HttpServletResponse resp)55 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 56 int requestSize = 1234; 57 int responseSize = 5678; 58 SimpleRequest request = SimpleRequest.newBuilder() 59 .setResponseSize(responseSize) 60 .setResponseType(Messages.PayloadType.COMPRESSABLE) 61 .setPayload(Payload.newBuilder() 62 .setBody(ByteString.copyFrom(new byte[requestSize]))) 63 .build(); 64 TestServiceGrpc.TestServiceBlockingStub blockingStub = 65 TestServiceGrpc.newBlockingStub(channel); 66 SimpleResponse simpleResponse = blockingStub.unaryCall(request); 67 resp.setContentType("text/plain"); 68 if (simpleResponse.getPayload().getBody().size() == responseSize) { 69 resp.setStatus(200); 70 resp.getWriter().println("PASS!"); 71 } else { 72 resp.setStatus(500); 73 resp.getWriter().println("FAILED!"); 74 } 75 } 76 } 77