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.testing.integration; 18 19 import static com.google.common.collect.Sets.newHashSet; 20 import static java.util.Collections.singletonList; 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertTrue; 23 24 import com.google.common.collect.ImmutableList; 25 import io.grpc.Grpc; 26 import io.grpc.InsecureChannelCredentials; 27 import io.grpc.ManagedChannel; 28 import io.grpc.testing.integration.Metrics.EmptyMessage; 29 import io.grpc.testing.integration.Metrics.GaugeResponse; 30 import io.grpc.testing.integration.StressTestClient.TestCaseWeightPair; 31 import java.net.InetSocketAddress; 32 import java.util.Arrays; 33 import java.util.List; 34 import java.util.Set; 35 import java.util.concurrent.TimeUnit; 36 import java.util.concurrent.locks.LockSupport; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.rules.Timeout; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 43 /** Unit tests for {@link StressTestClient}. */ 44 @RunWith(JUnit4.class) 45 public class StressTestClientTest { 46 47 @Rule 48 public final Timeout globalTimeout = Timeout.seconds(10); 49 50 @Test ipv6AddressesShouldBeSupported()51 public void ipv6AddressesShouldBeSupported() { 52 StressTestClient client = new StressTestClient(); 53 client.parseArgs(new String[] {"--server_addresses=[0:0:0:0:0:0:0:1]:8080," 54 + "[1:2:3:4:f:e:a:b]:8083"}); 55 56 assertEquals(2, client.addresses().size()); 57 assertEquals(new InetSocketAddress("0:0:0:0:0:0:0:1", 8080), client.addresses().get(0)); 58 assertEquals(new InetSocketAddress("1:2:3:4:f:e:a:b", 8083), client.addresses().get(1)); 59 } 60 61 @Test defaults()62 public void defaults() { 63 StressTestClient client = new StressTestClient(); 64 assertEquals(singletonList(new InetSocketAddress("localhost", 8080)), client.addresses()); 65 assertTrue(client.testCaseWeightPairs().isEmpty()); 66 assertEquals(-1, client.durationSecs()); 67 assertEquals(1, client.channelsPerServer()); 68 assertEquals(1, client.stubsPerChannel()); 69 assertEquals(8081, client.metricsPort()); 70 } 71 72 @Test allCommandlineSwitchesAreSupported()73 public void allCommandlineSwitchesAreSupported() { 74 StressTestClient client = new StressTestClient(); 75 client.parseArgs(new String[] { 76 "--server_addresses=localhost:8080,localhost:8081,localhost:8082", 77 "--test_cases=empty_unary:20,large_unary:50,server_streaming:30", 78 "--test_duration_secs=20", 79 "--num_channels_per_server=10", 80 "--num_stubs_per_channel=5", 81 "--metrics_port=9090", 82 "--server_host_override=foo.test.google.fr", 83 "--use_tls=true", 84 "--use_test_ca=true" 85 }); 86 87 List<InetSocketAddress> addresses = Arrays.asList(new InetSocketAddress("localhost", 8080), 88 new InetSocketAddress("localhost", 8081), new InetSocketAddress("localhost", 8082)); 89 assertEquals(addresses, client.addresses()); 90 91 List<TestCaseWeightPair> testCases = Arrays.asList( 92 new TestCaseWeightPair(TestCases.EMPTY_UNARY, 20), 93 new TestCaseWeightPair(TestCases.LARGE_UNARY, 50), 94 new TestCaseWeightPair(TestCases.SERVER_STREAMING, 30)); 95 assertEquals(testCases, client.testCaseWeightPairs()); 96 97 assertEquals("foo.test.google.fr", client.serverHostOverride()); 98 assertTrue(client.useTls()); 99 assertTrue(client.useTestCa()); 100 assertEquals(20, client.durationSecs()); 101 assertEquals(10, client.channelsPerServer()); 102 assertEquals(5, client.stubsPerChannel()); 103 assertEquals(9090, client.metricsPort()); 104 } 105 106 @Test serverHostOverrideShouldBeApplied()107 public void serverHostOverrideShouldBeApplied() { 108 StressTestClient client = new StressTestClient(); 109 client.parseArgs(new String[] { 110 "--server_addresses=localhost:8080", 111 "--server_host_override=foo.test.google.fr", 112 }); 113 114 assertEquals("foo.test.google.fr", client.addresses().get(0).getHostName()); 115 } 116 117 @Test gaugesShouldBeExported()118 public void gaugesShouldBeExported() throws Exception { 119 120 TestServiceServer server = new TestServiceServer(); 121 server.parseArgs(new String[]{"--port=" + 0, "--use_tls=false"}); 122 server.start(); 123 124 StressTestClient client = new StressTestClient(); 125 client.parseArgs(new String[] {"--test_cases=empty_unary:1", 126 "--server_addresses=localhost:" + server.getPort(), "--metrics_port=" + 0, 127 "--num_stubs_per_channel=2"}); 128 client.startMetricsService(); 129 client.runStressTest(); 130 131 // Connect to the metrics service 132 ManagedChannel ch = Grpc.newChannelBuilder( 133 "localhost:" + client.getMetricServerPort(), InsecureChannelCredentials.create()) 134 .build(); 135 136 MetricsServiceGrpc.MetricsServiceBlockingStub stub = MetricsServiceGrpc.newBlockingStub(ch); 137 138 // Wait until gauges have been exported 139 Set<String> gaugeNames = newHashSet("/stress_test/server_0/channel_0/stub_0/qps", 140 "/stress_test/server_0/channel_0/stub_1/qps"); 141 142 List<GaugeResponse> allGauges = 143 ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance())); 144 while (allGauges.size() < gaugeNames.size()) { 145 LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); 146 allGauges = ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance())); 147 } 148 149 for (GaugeResponse gauge : allGauges) { 150 String gaugeName = gauge.getName(); 151 152 assertTrue("gaugeName: " + gaugeName, gaugeNames.contains(gaugeName)); 153 assertTrue("qps: " + gauge.getLongValue(), gauge.getLongValue() > 0); 154 gaugeNames.remove(gauge.getName()); 155 156 GaugeResponse gauge1 = 157 stub.getGauge(Metrics.GaugeRequest.newBuilder().setName(gaugeName).build()); 158 assertEquals(gaugeName, gauge1.getName()); 159 assertTrue("qps: " + gauge1.getLongValue(), gauge1.getLongValue() > 0); 160 } 161 162 assertTrue("gauges: " + gaugeNames, gaugeNames.isEmpty()); 163 164 client.shutdown(); 165 server.stop(); 166 } 167 168 } 169