/
ConcurrencyTest.java
224 lines (194 loc) · 8.09 KB
/
ConcurrencyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2016 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.testing.integration;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.Server;
import io.grpc.ServerCredentials;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettySslContextChannelCredentials;
import io.grpc.netty.NettySslContextServerCredentials;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.integration.Messages.ResponseParameters;
import io.grpc.testing.integration.Messages.StreamingOutputCallRequest;
import io.grpc.testing.integration.Messages.StreamingOutputCallResponse;
import io.netty.handler.ssl.ClientAuth;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Tests that gRPC clients and servers can handle concurrent RPCs.
*
* <p>These tests use TLS to make them more realistic, and because we'd like to test the thread
* safety of the TLS-related code paths as well.
*/
// TODO: Consider augmenting this class to perform non-streaming, client streaming, and
// bidirectional streaming requests also.
@RunWith(JUnit4.class)
public class ConcurrencyTest {
@Rule public final Timeout globalTimeout = Timeout.seconds(60);
/**
* A response observer that signals a {@code CountDownLatch} when the proper number of responses
* arrives and the server signals that the RPC is complete.
*/
private static class SignalingResponseObserver
implements StreamObserver<StreamingOutputCallResponse> {
public SignalingResponseObserver(CountDownLatch responsesDoneSignal) {
this.responsesDoneSignal = responsesDoneSignal;
}
@Override
public void onCompleted() {
Preconditions.checkState(numResponsesReceived == NUM_RESPONSES_PER_REQUEST);
responsesDoneSignal.countDown();
}
@Override
public void onError(Throwable error) {
// This should never happen. If it does happen, ensure that the error is visible.
error.printStackTrace();
}
@Override
public void onNext(StreamingOutputCallResponse response) {
numResponsesReceived++;
}
private final CountDownLatch responsesDoneSignal;
private int numResponsesReceived = 0;
}
/**
* A client worker task that waits until all client workers are ready, then sends a request for a
* server-streaming RPC and arranges for a {@code CountDownLatch} to be signaled when the RPC is
* complete.
*/
private class ClientWorker implements Runnable {
public ClientWorker(CyclicBarrier startBarrier, CountDownLatch responsesDoneSignal) {
this.startBarrier = startBarrier;
this.responsesDoneSignal = responsesDoneSignal;
}
@Override
public void run() {
try {
// Prepare the request.
StreamingOutputCallRequest.Builder requestBuilder = StreamingOutputCallRequest.newBuilder();
for (int i = 0; i < NUM_RESPONSES_PER_REQUEST; i++) {
requestBuilder.addResponseParameters(ResponseParameters.newBuilder()
.setSize(1000)
.setIntervalUs(0)); // No delay between responses, for maximum concurrency.
}
StreamingOutputCallRequest request = requestBuilder.build();
// Wait until all client worker threads are poised & ready, then send the request. This way
// all clients send their requests at approximately the same time.
startBarrier.await();
clientStub.streamingOutputCall(request, new SignalingResponseObserver(responsesDoneSignal));
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
private final CyclicBarrier startBarrier;
private final CountDownLatch responsesDoneSignal;
}
private static final int NUM_SERVER_THREADS = 10;
private static final int NUM_CONCURRENT_REQUESTS = 100;
private static final int NUM_RESPONSES_PER_REQUEST = 100;
private Server server;
private ManagedChannel clientChannel;
private TestServiceGrpc.TestServiceStub clientStub;
private ScheduledExecutorService serverExecutor;
private ExecutorService clientExecutor;
@Before
public void setUp() throws Exception {
serverExecutor = Executors.newScheduledThreadPool(NUM_SERVER_THREADS);
clientExecutor = Executors.newFixedThreadPool(NUM_CONCURRENT_REQUESTS);
server = newServer();
// Create the client. Keep a reference to its channel so we can shut it down during tearDown().
clientChannel = newClientChannel();
clientStub = TestServiceGrpc.newStub(clientChannel);
}
@After
public void tearDown() {
if (server != null) {
server.shutdown();
}
if (clientChannel != null) {
clientChannel.shutdown();
}
MoreExecutors.shutdownAndAwaitTermination(serverExecutor, 5, TimeUnit.SECONDS);
MoreExecutors.shutdownAndAwaitTermination(clientExecutor, 5, TimeUnit.SECONDS);
}
/**
* Tests that gRPC can handle concurrent server-streaming RPCs.
*/
@Test
public void serverStreamingTest() throws Exception {
CyclicBarrier startBarrier = new CyclicBarrier(NUM_CONCURRENT_REQUESTS);
CountDownLatch responsesDoneSignal = new CountDownLatch(NUM_CONCURRENT_REQUESTS);
for (int i = 0; i < NUM_CONCURRENT_REQUESTS; i++) {
clientExecutor.execute(new ClientWorker(startBarrier, responsesDoneSignal));
}
// Wait until the clients all receive their complete RPC response streams.
responsesDoneSignal.await();
}
/**
* Creates and starts a new {@link TestServiceImpl} server.
*/
private Server newServer() throws CertificateException, IOException {
File serverCertChainFile = TestUtils.loadCert("server1.pem");
File serverPrivateKeyFile = TestUtils.loadCert("server1.key");
X509Certificate[] serverTrustedCaCerts = {
TestUtils.loadX509Cert("ca.pem")
};
ServerCredentials serverCreds = NettySslContextServerCredentials.create(
GrpcSslContexts.forServer(serverCertChainFile, serverPrivateKeyFile)
.trustManager(serverTrustedCaCerts)
.clientAuth(ClientAuth.REQUIRE)
.build());
return Grpc.newServerBuilderForPort(0, serverCreds)
.addService(new TestServiceImpl(serverExecutor))
.build()
.start();
}
private ManagedChannel newClientChannel() throws CertificateException, IOException {
File clientCertChainFile = TestUtils.loadCert("client.pem");
File clientPrivateKeyFile = TestUtils.loadCert("client.key");
X509Certificate[] clientTrustedCaCerts = {
TestUtils.loadX509Cert("ca.pem")
};
ChannelCredentials channelCreds = NettySslContextChannelCredentials.create(
GrpcSslContexts.forClient()
.keyManager(clientCertChainFile, clientPrivateKeyFile)
.trustManager(clientTrustedCaCerts)
.build());
return Grpc.newChannelBuilder("localhost:" + server.getPort(), channelCreds)
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
.build();
}
}