Skip to content

Commit 444c4af

Browse files
optimize trpc-proto test (#124)
* feat: optimize trpc-proto test
1 parent 21429e8 commit 444c4af

38 files changed

Lines changed: 603 additions & 597 deletions

File tree

trpc-dependencies/trpc-dependencies-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@
636636
<groupId>org.eclipse.jetty</groupId>
637637
<version>${jetty.version}</version>
638638
</dependency>
639+
<dependency>
640+
<artifactId>jetty-alpn-java-server</artifactId>
641+
<groupId>org.eclipse.jetty</groupId>
642+
<version>${jetty.version}</version>
643+
</dependency>
639644
<dependency>
640645
<artifactId>jetty-alpn-openjdk8-server</artifactId>
641646
<groupId>org.eclipse.jetty</groupId>

trpc-proto/trpc-proto-http/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@
6464
<groupId>jakarta.servlet</groupId>
6565
<artifactId>jakarta.servlet-api</artifactId>
6666
</dependency>
67+
<dependency>
68+
<artifactId>jetty-alpn-java-server</artifactId>
69+
<groupId>org.eclipse.jetty</groupId>
70+
</dependency>
6771
</dependencies>
6872
</project>

trpc-proto/trpc-proto-http/src/main/java/com/tencent/trpc/proto/http/client/Http2RpcClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -22,8 +22,9 @@
2222
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
2323
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
2424
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
25+
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
26+
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
2527
import org.apache.hc.core5.http2.HttpVersionPolicy;
26-
import org.apache.hc.core5.http2.ssl.ConscryptClientTlsStrategy;
2728
import org.apache.hc.core5.ssl.SSLContexts;
2829

2930
/**
@@ -63,7 +64,10 @@ protected void doOpen() {
6364
// 2. Configure connection pool.
6465
final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder
6566
.create().useSystemProperties()
66-
.setTlsStrategy(new ConscryptClientTlsStrategy(sslContext))
67+
.setTlsStrategy(ClientTlsStrategyBuilder.create()
68+
.setSslContext(sslContext)
69+
.setHostnameVerifier(new DefaultHostnameVerifier())
70+
.build())
6771
.build();
6872

6973
// 3. Configure the client to force HTTPS protocol to use HTTP1 communication and H2 protocol

trpc-proto/trpc-proto-http/src/test/java/com/tencent/trpc/proto/http/Http2RpcClientTest.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import java.util.HashMap;
3131
import java.util.Map;
3232
import org.apache.commons.lang3.StringUtils;
33-
import org.junit.AfterClass;
34-
import org.junit.Assert;
35-
import org.junit.BeforeClass;
36-
import org.junit.Test;
33+
import org.junit.jupiter.api.AfterAll;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
3737
import tests.service.GreeterJsonService;
3838
import tests.service.GreeterService;
3939
import tests.service.HelloRequestProtocol.HelloRequest;
@@ -53,7 +53,7 @@ public class Http2RpcClientTest {
5353

5454
private static Map<String, Object> extMap = new HashMap<>();
5555

56-
@BeforeClass
56+
@BeforeAll
5757
public static void startHttpServer() {
5858

5959
String path = Http2RpcServerTest.class.getClassLoader().getResource("keystore.jks")
@@ -130,7 +130,7 @@ private static ServiceConfig getServiceConfig(ProviderConfig gspc, String name,
130130
return serviceConfig;
131131
}
132132

133-
@AfterClass
133+
@AfterAll
134134
public static void stopHttpServer() {
135135
ConfigManager.stopTest();
136136
if (serverConfig != null) {
@@ -154,7 +154,7 @@ public void testHttpRpcClient() {
154154
backendConfig.setRequestTimeout(10000);
155155
consumerConfig.setServiceInterface(GreeterService.class);
156156
consumerConfig.setBackendConfig(backendConfig);
157-
backendConfig.setNamingUrl("ip://127.0.0.1:18084");
157+
backendConfig.setNamingUrl("ip://localhost:18084");
158158
backendConfig.setKeepAlive(false);
159159
backendConfig.setConnsPerAddr(4);
160160
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -166,10 +166,10 @@ public void testHttpRpcClient() {
166166
for (int i = 0; i < 20; i++) {
167167
RpcClientContext context = new RpcClientContext();
168168
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
169-
Assert.assertNotNull(helloResponse);
169+
Assertions.assertNotNull(helloResponse);
170170
String rspMessage = helloResponse.getMessage();
171171
logger.info("http rpc client request result: {}", rspMessage);
172-
Assert.assertTrue(rspMessage.contains(TEST_MESSAGE));
172+
Assertions.assertTrue(rspMessage.contains(TEST_MESSAGE));
173173
}
174174
} finally {
175175
backendConfig.stop();
@@ -185,7 +185,7 @@ public void testHttpRpcClientWithOneWay() {
185185
backendConfig.setRequestTimeout(10000);
186186
consumerConfig.setServiceInterface(GreeterService.class);
187187
consumerConfig.setBackendConfig(backendConfig);
188-
backendConfig.setNamingUrl("ip://127.0.0.1:18084");
188+
backendConfig.setNamingUrl("ip://localhost:18084");
189189
backendConfig.setKeepAlive(false);
190190
backendConfig.setConnsPerAddr(4);
191191
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -198,7 +198,7 @@ public void testHttpRpcClientWithOneWay() {
198198
RpcClientContext context = new RpcClientContext();
199199
context.setOneWay(true);
200200
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
201-
Assert.assertNull(helloResponse);
201+
Assertions.assertNull(helloResponse);
202202
}
203203
} finally {
204204
backendConfig.stop();
@@ -214,7 +214,7 @@ public void testHttpRpcClientBasePath1() {
214214
backendConfig.setRequestTimeout(10000);
215215
consumerConfig.setServiceInterface(GreeterService.class);
216216
consumerConfig.setBackendConfig(backendConfig);
217-
backendConfig.setNamingUrl("ip://127.0.0.1:18085");
217+
backendConfig.setNamingUrl("ip://localhost:18085");
218218
backendConfig.setKeepAlive(false);
219219
backendConfig.setConnsPerAddr(4);
220220
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -227,10 +227,10 @@ public void testHttpRpcClientBasePath1() {
227227
for (int i = 0; i < 20; i++) {
228228
RpcClientContext context = new RpcClientContext();
229229
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
230-
Assert.assertNotNull(helloResponse);
230+
Assertions.assertNotNull(helloResponse);
231231
String rspMessage = helloResponse.getMessage();
232232
logger.info("http rpc client request result: {}", rspMessage);
233-
Assert.assertTrue(rspMessage.contains(TEST_MESSAGE));
233+
Assertions.assertTrue(rspMessage.contains(TEST_MESSAGE));
234234
}
235235
} finally {
236236
backendConfig.stop();
@@ -246,7 +246,7 @@ public void testHttpRpcClientBasePath2() {
246246
backendConfig.setRequestTimeout(10000);
247247
consumerConfig.setServiceInterface(GreeterJsonService.class);
248248
consumerConfig.setBackendConfig(backendConfig);
249-
backendConfig.setNamingUrl("ip://127.0.0.1:18085");
249+
backendConfig.setNamingUrl("ip://localhost:18085");
250250
backendConfig.setKeepAlive(false);
251251
backendConfig.setConnsPerAddr(4);
252252
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -262,10 +262,10 @@ public void testHttpRpcClientBasePath2() {
262262

263263
RpcClientContext context = new RpcClientContext();
264264
Map helloResponse = proxy.sayHelloJson(context, obj);
265-
Assert.assertNotNull(helloResponse);
265+
Assertions.assertNotNull(helloResponse);
266266
String rspMessage = (String) helloResponse.get("message");
267267
logger.info("http rpc client request result: {}", rspMessage);
268-
Assert.assertTrue(rspMessage.contains(TEST_MESSAGE));
268+
Assertions.assertTrue(rspMessage.contains(TEST_MESSAGE));
269269
}
270270
} finally {
271271
backendConfig.stop();
@@ -281,7 +281,7 @@ public void testHttpRpcClientBasePathNotExist() {
281281
backendConfig.setRequestTimeout(10000);
282282
consumerConfig.setServiceInterface(GreeterService.class);
283283
consumerConfig.setBackendConfig(backendConfig);
284-
backendConfig.setNamingUrl("ip://127.0.0.1:18085");
284+
backendConfig.setNamingUrl("ip://localhost:18085");
285285
backendConfig.setKeepAlive(false);
286286
backendConfig.setConnsPerAddr(4);
287287
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -293,7 +293,7 @@ public void testHttpRpcClientBasePathNotExist() {
293293

294294
RpcClientContext context = new RpcClientContext();
295295
HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
296-
Assert.fail("no exception thrown");
296+
Assertions.fail("no exception thrown");
297297
} catch (TRpcException e) {
298298
logger.error("error: ", e);
299299
} finally {
@@ -310,7 +310,7 @@ public void testHttpRpcClientWithFailed() {
310310
backendConfig.setRequestTimeout(10000);
311311
consumerConfig.setServiceInterface(GreeterService.class);
312312
consumerConfig.setBackendConfig(backendConfig);
313-
backendConfig.setNamingUrl("ip://127.0.0.1:18086");
313+
backendConfig.setNamingUrl("ip://localhost:18086");
314314
backendConfig.setKeepAlive(false);
315315
backendConfig.setConnsPerAddr(4);
316316
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -322,9 +322,9 @@ public void testHttpRpcClientWithFailed() {
322322

323323
RpcClientContext context = new RpcClientContext();
324324
proxy.sayHello(context, createPbRequest(TEST_MESSAGE));
325-
Assert.fail("no exception thrown");
325+
Assertions.fail("no exception thrown");
326326
} catch (TRpcException e) {
327-
Assert.assertEquals(0, e.getBizCode());
327+
Assertions.assertEquals(0, e.getBizCode());
328328
} finally {
329329
backendConfig.stop();
330330
}
@@ -339,7 +339,7 @@ public void testHttpRpcClientWithBlankRsp() {
339339
backendConfig.setRequestTimeout(10000);
340340
consumerConfig.setServiceInterface(GreeterService.class);
341341
consumerConfig.setBackendConfig(backendConfig);
342-
backendConfig.setNamingUrl("ip://127.0.0.1:18084");
342+
backendConfig.setNamingUrl("ip://localhost:18084");
343343
backendConfig.setKeepAlive(false);
344344
backendConfig.setConnsPerAddr(4);
345345
backendConfig.setProtocol(HTTP2_SCHEME);
@@ -351,9 +351,9 @@ public void testHttpRpcClientWithBlankRsp() {
351351
for (int i = 0; i < 20; i++) {
352352
RpcClientContext context = new RpcClientContext();
353353
String helloResponse = proxy.sayBlankHello(context, createPbRequest(TEST_MESSAGE));
354-
Assert.assertNull(helloResponse);
354+
Assertions.assertNull(helloResponse);
355355
logger.info("http rpc client request result: {}", helloResponse);
356-
Assert.assertTrue(StringUtils.isEmpty(helloResponse));
356+
Assertions.assertTrue(StringUtils.isEmpty(helloResponse));
357357
}
358358
} finally {
359359
backendConfig.stop();

0 commit comments

Comments
 (0)