1 /* 2 * Copyright 2015 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 static org.junit.Assert.assertTrue; 20 21 import io.grpc.ManagedChannel; 22 import io.grpc.StatusRuntimeException; 23 import io.grpc.netty.NegotiationType; 24 import io.grpc.netty.NettyChannelBuilder; 25 import io.grpc.okhttp.OkHttpChannelBuilder; 26 import io.grpc.testing.integration.EmptyProtos.Empty; 27 import io.grpc.testing.integration.Messages.ReconnectInfo; 28 import io.grpc.testing.integration.Messages.ReconnectParams; 29 30 /** 31 * Verifies the client is reconnecting the server with correct backoffs 32 * 33 * <p>See the <a href="https://github.com/grpc/grpc/blob/master/doc/connection-backoff-interop-test-description.md">Test Spec</a>. 34 */ 35 public class ReconnectTestClient { 36 private static final int TEST_TIME_MS = 540 * 1000; 37 38 private int serverControlPort = 8080; 39 private int serverRetryPort = 8081; 40 private boolean useOkhttp = false; 41 private ManagedChannel controlChannel; 42 private ManagedChannel retryChannel; 43 private ReconnectServiceGrpc.ReconnectServiceBlockingStub controlStub; 44 private ReconnectServiceGrpc.ReconnectServiceBlockingStub retryStub; 45 parseArgs(String[] args)46 private void parseArgs(String[] args) { 47 for (String arg : args) { 48 if (!arg.startsWith("--")) { 49 System.err.println("All arguments must start with '--': " + arg); 50 System.exit(1); 51 } 52 String[] parts = arg.substring(2).split("=", 2); 53 String key = parts[0]; 54 String value = parts[1]; 55 if ("server_control_port".equals(key)) { 56 serverControlPort = Integer.parseInt(value); 57 } else if ("server_retry_port".equals(key)) { 58 serverRetryPort = Integer.parseInt(value); 59 } else if ("use_okhttp".equals(key)) { 60 useOkhttp = Boolean.parseBoolean(value); 61 } else { 62 System.err.println("Unknown argument: " + key); 63 System.exit(1); 64 } 65 } 66 } 67 runTest()68 private void runTest() throws Exception { 69 try { 70 controlChannel = NettyChannelBuilder.forAddress("127.0.0.1", serverControlPort) 71 .negotiationType(NegotiationType.PLAINTEXT).build(); 72 controlStub = ReconnectServiceGrpc.newBlockingStub(controlChannel); 73 if (useOkhttp) { 74 retryChannel = 75 OkHttpChannelBuilder.forAddress("127.0.0.1", serverRetryPort) 76 .useTransportSecurity() 77 .build(); 78 } else { 79 retryChannel = NettyChannelBuilder.forAddress("127.0.0.1", serverRetryPort) 80 .negotiationType(NegotiationType.TLS).build(); 81 } 82 retryStub = ReconnectServiceGrpc.newBlockingStub(retryChannel); 83 controlStub.start(ReconnectParams.getDefaultInstance()); 84 85 long startTimeStamp = System.currentTimeMillis(); 86 while ((System.currentTimeMillis() - startTimeStamp) < TEST_TIME_MS) { 87 try { 88 retryStub.start(ReconnectParams.getDefaultInstance()); 89 } catch (StatusRuntimeException expected) { 90 // Make CheckStyle happy. 91 } 92 Thread.sleep(50); 93 } 94 ReconnectInfo info = controlStub.stop(Empty.getDefaultInstance()); 95 assertTrue(info.getPassed()); 96 } finally { 97 controlChannel.shutdownNow(); 98 retryChannel.shutdownNow(); 99 } 100 } 101 102 /** 103 * The main application allowing this client to be launched from the command line. 104 */ main(String[] args)105 public static void main(String[] args) { 106 ReconnectTestClient client = new ReconnectTestClient(); 107 client.parseArgs(args); 108 System.out.println("Starting test:"); 109 try { 110 client.runTest(); 111 System.out.println("Finished successfully"); 112 System.exit(0); 113 } catch (Throwable e) { 114 e.printStackTrace(); 115 System.err.println("Test failed!"); 116 System.exit(1); 117 } 118 } 119 } 120