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.Grpc; 21 import io.grpc.ManagedChannel; 22 import io.grpc.TlsChannelCredentials; 23 import io.grpc.testing.integration.Messages.Payload; 24 import io.grpc.testing.integration.Messages.SimpleRequest; 25 import io.grpc.testing.integration.Messages.SimpleResponse; 26 import java.io.IOException; 27 import java.util.concurrent.TimeUnit; 28 import javax.servlet.http.HttpServlet; 29 import javax.servlet.http.HttpServletRequest; 30 import javax.servlet.http.HttpServletResponse; 31 32 /** 33 * A test class that checks we can reuse a channel across requests. 34 * 35 * <p>This servlet communicates with {@code grpc-test.sandbox.googleapis.com}, which is a server 36 * managed by the gRPC team. For more information, see 37 * <a href="https://github.com/grpc/grpc/blob/master/doc/interop-test-descriptions.md"> 38 * Interoperability Test Case Descriptions</a>. 39 */ 40 @SuppressWarnings("serial") 41 public final class LongLivedChannel extends HttpServlet { 42 private static final String INTEROP_TEST_ADDRESS = "grpc-test.sandbox.googleapis.com:443"; 43 private final ManagedChannel channel = 44 Grpc.newChannelBuilder(INTEROP_TEST_ADDRESS, TlsChannelCredentials.create()).build(); 45 46 @Override destroy()47 public void destroy() { 48 try { 49 channel.shutdownNow().awaitTermination(1, TimeUnit.SECONDS); 50 } catch (Exception e) { 51 throw new RuntimeException(e); 52 } 53 } 54 55 @Override doGet(HttpServletRequest req, HttpServletResponse resp)56 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 57 int requestSize = 1234; 58 int responseSize = 5678; 59 SimpleRequest request = SimpleRequest.newBuilder() 60 .setResponseSize(responseSize) 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