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