Skip to content

Commit 21429e8

Browse files
optimize transport test (#122)
* feat: optimize trpc-transport test
1 parent ad88b38 commit 21429e8

19 files changed

Lines changed: 371 additions & 330 deletions

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
<central-publishing-maven-plugin.version>0.7.0</central-publishing-maven-plugin.version>
105105
<exec.maven.version>3.1.0</exec.maven.version>
106106
<junit.version>5.14.2</junit.version>
107+
<mockito-core.version>5.21.0</mockito-core.version>
108+
<mockito-inline.version>5.2.0</mockito-inline.version>
107109
<file.encoding>UTF-8</file.encoding>
108110
</properties>
109111

@@ -146,6 +148,24 @@
146148
<classifier>runtime</classifier>
147149
<scope>test</scope>
148150
</dependency>
151+
<dependency>
152+
<groupId>org.mockito</groupId>
153+
<artifactId>mockito-core</artifactId>
154+
<version>${mockito-core.version}</version>
155+
<scope>test</scope>
156+
</dependency>
157+
<dependency>
158+
<groupId>org.mockito</groupId>
159+
<artifactId>mockito-junit-jupiter</artifactId>
160+
<version>${mockito-core.version}</version>
161+
<scope>test</scope>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.mockito</groupId>
165+
<artifactId>mockito-inline</artifactId>
166+
<version>${mockito-inline.version}</version>
167+
<scope>test</scope>
168+
</dependency>
149169
</dependencies>
150170

151171
<build>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
<gson.version>2.8.9</gson.version>
8484
<guava.version>32.1.3-jre</guava.version>
8585
<httpclient.version>4.5.14</httpclient.version>
86-
<httpclient5.version>5.4.1</httpclient5.version>
86+
<httpclient5.version>5.3.1</httpclient5.version>
8787
<httpcore.version>4.4.15</httpcore.version>
88-
<httpcore5.version>5.2.1</httpcore5.version>
88+
<httpcore5.version>5.2.5</httpcore5.version>
8989
<hutool.all.version>5.8.28</hutool.all.version>
9090
<jackson.version>2.15.0-rc2</jackson.version>
9191
<jakarta.servlet.version>6.0.0</jakarta.servlet.version>

trpc-transport/trpc-transport-http/src/test/java/com/tencent/trpc/transport/http/ExecutorDispatcherTest.java

Lines changed: 6 additions & 6 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,
@@ -17,17 +17,17 @@
1717
import jakarta.servlet.ServletException;
1818
import jakarta.servlet.http.HttpServletRequest;
1919
import jakarta.servlet.http.HttpServletResponse;
20-
import org.junit.Test;
21-
import org.powermock.api.mockito.PowerMockito;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mockito;
2222

2323
public class ExecutorDispatcherTest {
2424

2525
@Test
2626
public void test() throws ServletException, IOException {
2727
ExecutorDispatcher dispatcher = new ExecutorDispatcher();
28-
HttpServletRequest req = PowerMockito.mock(HttpServletRequest.class);
29-
PowerMockito.when(req.getLocalPort()).thenReturn(1024);
30-
HttpServletResponse rsp = PowerMockito.mock(HttpServletResponse.class);
28+
HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
29+
Mockito.when(req.getLocalPort()).thenReturn(1024);
30+
HttpServletResponse rsp = Mockito.mock(HttpServletResponse.class);
3131
dispatcher.service(req, rsp);
3232
org.mockito.Mockito.verify(rsp, times(1)).sendError(404,
3333
"HttpExecutor is closed: port is:1024.");

trpc-transport/trpc-transport-http/src/test/java/com/tencent/trpc/transport/http/JettyHttp2ServerTest.java

Lines changed: 18 additions & 18 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,
@@ -14,7 +14,7 @@
1414
import static com.tencent.trpc.transport.http.common.Constants.HTTP2_SCHEME;
1515
import static com.tencent.trpc.transport.http.common.Constants.KEYSTORE_PASS;
1616
import static com.tencent.trpc.transport.http.common.Constants.KEYSTORE_PATH;
17-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
1818

1919
import com.tencent.trpc.core.common.config.ProtocolConfig;
2020
import com.tencent.trpc.core.extension.ExtensionLoader;
@@ -43,10 +43,10 @@
4343
import org.apache.hc.core5.http2.ssl.ConscryptClientTlsStrategy;
4444
import org.apache.hc.core5.ssl.SSLContexts;
4545
import org.eclipse.jetty.http.HttpStatus;
46-
import org.junit.AfterClass;
47-
import org.junit.Assert;
48-
import org.junit.BeforeClass;
49-
import org.junit.Test;
46+
import org.junit.jupiter.api.AfterAll;
47+
import org.junit.jupiter.api.Assertions;
48+
import org.junit.jupiter.api.BeforeAll;
49+
import org.junit.jupiter.api.Test;
5050

5151
public class JettyHttp2ServerTest {
5252

@@ -56,7 +56,7 @@ public class JettyHttp2ServerTest {
5656

5757
private static CloseableHttpAsyncClient httpAsyncClient;
5858

59-
@BeforeClass
59+
@BeforeAll
6060
public static void beforeClass() throws CertificateException, NoSuchAlgorithmException,
6161
KeyStoreException, IOException, KeyManagementException {
6262
ProtocolConfig protocolConfig = ProtocolConfig.newInstance();
@@ -109,7 +109,7 @@ public static void beforeClass() throws CertificateException, NoSuchAlgorithmExc
109109
httpAsyncClient.start();
110110
}
111111

112-
@AfterClass
112+
@AfterAll
113113
public static void afterClass() {
114114
if (httpServer != null) {
115115
httpServer.open();
@@ -146,14 +146,14 @@ public void cancelled() {
146146
}
147147
});
148148
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
149-
Assert.assertNotEquals(null, simpleHttpResponse);
149+
Assertions.assertNotEquals(null, simpleHttpResponse);
150150
logger.error(simpleHttpResponse.getBodyText());
151-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
152-
Assert.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
151+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
152+
Assertions.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
153153
logger.info("response code is {}", simpleHttpResponse.getCode());
154-
Assert.assertEquals(200, simpleHttpResponse.getCode());
154+
Assertions.assertEquals(200, simpleHttpResponse.getCode());
155155
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
156-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
156+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
157157
}
158158

159159
@Test
@@ -178,14 +178,14 @@ public void cancelled() {
178178
}
179179
});
180180
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
181-
Assert.assertNotEquals(null, simpleHttpResponse);
181+
Assertions.assertNotEquals(null, simpleHttpResponse);
182182
logger.error(simpleHttpResponse.getBodyText());
183-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
184-
Assert.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
183+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
184+
Assertions.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
185185
logger.info("response code is {}", simpleHttpResponse.getCode());
186-
Assert.assertEquals(404, simpleHttpResponse.getCode());
186+
Assertions.assertEquals(404, simpleHttpResponse.getCode());
187187
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
188-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
188+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
189189
}
190190

191191
}

trpc-transport/trpc-transport-http/src/test/java/com/tencent/trpc/transport/http/JettyHttp2cServerTest.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
package com.tencent.trpc.transport.http;
1313

1414
import static com.tencent.trpc.transport.http.common.Constants.HTTP2_SCHEME;
15-
import static org.junit.Assert.assertTrue;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
1616

1717
import com.tencent.trpc.core.common.config.ProtocolConfig;
1818
import com.tencent.trpc.core.extension.ExtensionLoader;
@@ -34,10 +34,10 @@
3434
import org.apache.http.impl.client.CloseableHttpClient;
3535
import org.apache.http.impl.client.HttpClients;
3636
import org.eclipse.jetty.http.HttpStatus;
37-
import org.junit.AfterClass;
38-
import org.junit.Assert;
39-
import org.junit.BeforeClass;
40-
import org.junit.Test;
37+
import org.junit.jupiter.api.AfterAll;
38+
import org.junit.jupiter.api.Assertions;
39+
import org.junit.jupiter.api.BeforeAll;
40+
import org.junit.jupiter.api.Test;
4141

4242
public class JettyHttp2cServerTest {
4343

@@ -49,7 +49,7 @@ public class JettyHttp2cServerTest {
4949

5050
private static CloseableHttpClient httpClient;
5151

52-
@BeforeClass
52+
@BeforeAll
5353
public static void beforeClass() {
5454
ProtocolConfig protocolConfig = ProtocolConfig.newInstance();
5555
protocolConfig.setIp("localhost");
@@ -88,7 +88,7 @@ public static void beforeClass() {
8888
httpClient = HttpClients.custom().build();
8989
}
9090

91-
@AfterClass
91+
@AfterAll
9292
public static void afterClass() {
9393
if (httpServer != null) {
9494
httpServer.open();
@@ -125,14 +125,14 @@ public void cancelled() {
125125
}
126126
});
127127
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
128-
Assert.assertNotEquals(null, simpleHttpResponse);
128+
Assertions.assertNotEquals(null, simpleHttpResponse);
129129
logger.error(simpleHttpResponse.getBodyText());
130-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
131-
Assert.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
130+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
131+
Assertions.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
132132
logger.info("response code is {}", simpleHttpResponse.getCode());
133-
Assert.assertEquals(200, simpleHttpResponse.getCode());
133+
Assertions.assertEquals(200, simpleHttpResponse.getCode());
134134
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
135-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
135+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
136136
}
137137

138138
@Test
@@ -157,14 +157,14 @@ public void cancelled() {
157157
}
158158
});
159159
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
160-
Assert.assertNotEquals(null, simpleHttpResponse);
160+
Assertions.assertNotEquals(null, simpleHttpResponse);
161161
logger.error(simpleHttpResponse.getBodyText());
162-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
163-
Assert.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
162+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
163+
Assertions.assertEquals(2, simpleHttpResponse.getVersion().getMajor());
164164
logger.info("response code is {}", simpleHttpResponse.getCode());
165-
Assert.assertEquals(404, simpleHttpResponse.getCode());
165+
Assertions.assertEquals(404, simpleHttpResponse.getCode());
166166
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
167-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
167+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
168168
}
169169

170170
@Test
@@ -175,10 +175,10 @@ public void testNormalRequestWithHttp() throws Exception {
175175

176176
int responseCode = httpResponse.getStatusLine().getStatusCode();
177177
logger.info("response code is {}", responseCode);
178-
Assert.assertEquals(responseCode, 200);
178+
Assertions.assertEquals(responseCode, 200);
179179

180-
Assert.assertEquals(httpResponse.getProtocolVersion().getProtocol(), "HTTP");
181-
Assert.assertEquals(httpResponse.getProtocolVersion().getMajor(), 1);
180+
Assertions.assertEquals(httpResponse.getProtocolVersion().getProtocol(), "HTTP");
181+
Assertions.assertEquals(httpResponse.getProtocolVersion().getMajor(), 1);
182182

183183
}
184184

@@ -190,10 +190,10 @@ public void testNotExistRequestWithHttp1() throws Exception {
190190

191191
int responseCode = httpResponse.getStatusLine().getStatusCode();
192192
logger.info("response code is {}", responseCode);
193-
Assert.assertEquals(responseCode, 404);
193+
Assertions.assertEquals(responseCode, 404);
194194

195-
Assert.assertEquals(httpResponse.getProtocolVersion().getProtocol(), "HTTP");
196-
Assert.assertEquals(httpResponse.getProtocolVersion().getMajor(), 1);
195+
Assertions.assertEquals(httpResponse.getProtocolVersion().getProtocol(), "HTTP");
196+
Assertions.assertEquals(httpResponse.getProtocolVersion().getMajor(), 1);
197197

198198
}
199199

trpc-transport/trpc-transport-http/src/test/java/com/tencent/trpc/transport/http/JettyHttpsServerTest.java

Lines changed: 28 additions & 22 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,
@@ -14,7 +14,7 @@
1414
import static com.tencent.trpc.transport.http.common.Constants.HTTP_SCHEME;
1515
import static com.tencent.trpc.transport.http.common.Constants.KEYSTORE_PASS;
1616
import static com.tencent.trpc.transport.http.common.Constants.KEYSTORE_PATH;
17-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
1818

1919
import com.tencent.trpc.core.common.config.ProtocolConfig;
2020
import com.tencent.trpc.core.extension.ExtensionLoader;
@@ -40,13 +40,15 @@
4040
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
4141
import org.apache.hc.core5.concurrent.FutureCallback;
4242
import org.apache.hc.core5.http2.HttpVersionPolicy;
43-
import org.apache.hc.core5.http2.ssl.ConscryptClientTlsStrategy;
43+
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
44+
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
4445
import org.apache.hc.core5.ssl.SSLContexts;
46+
import org.apache.hc.core5.ssl.TrustStrategy;
4547
import org.eclipse.jetty.http.HttpStatus;
46-
import org.junit.AfterClass;
47-
import org.junit.Assert;
48-
import org.junit.BeforeClass;
49-
import org.junit.Test;
48+
import org.junit.jupiter.api.AfterAll;
49+
import org.junit.jupiter.api.Assertions;
50+
import org.junit.jupiter.api.BeforeAll;
51+
import org.junit.jupiter.api.Test;
5052

5153
public class JettyHttpsServerTest {
5254

@@ -56,7 +58,7 @@ public class JettyHttpsServerTest {
5658

5759
private static CloseableHttpAsyncClient httpAsyncClient;
5860

59-
@BeforeClass
61+
@BeforeAll
6062
public static void beforeClass() throws CertificateException, NoSuchAlgorithmException,
6163
KeyStoreException, IOException, KeyManagementException {
6264
ProtocolConfig protocolConfig = ProtocolConfig.newInstance();
@@ -97,19 +99,23 @@ public static void beforeClass() throws CertificateException, NoSuchAlgorithmExc
9799
String keyStorePath = String.valueOf(path);
98100
String keyStorePass = "init234";
99101
final SSLContext sslContext = SSLContexts.custom()
100-
.loadTrustMaterial(new File(keyStorePath), keyStorePass.toCharArray())
102+
.loadTrustMaterial(new File(keyStorePath), keyStorePass.toCharArray(),
103+
(TrustStrategy) (chain, authType) -> true)
101104
.build();
102105
final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder
103-
.create().useSystemProperties()
104-
.setTlsStrategy(new ConscryptClientTlsStrategy(sslContext))
106+
.create()
107+
.setTlsStrategy(ClientTlsStrategyBuilder.create()
108+
.setSslContext(sslContext)
109+
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
110+
.build())
105111
.build();
106112
httpAsyncClient = HttpAsyncClients.custom()
107113
.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1).setConnectionManager(cm)
108114
.build();
109115
httpAsyncClient.start();
110116
}
111117

112-
@AfterClass
118+
@AfterAll
113119
public static void afterClass() {
114120
if (httpServer != null) {
115121
httpServer.open();
@@ -146,14 +152,14 @@ public void cancelled() {
146152
}
147153
});
148154
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
149-
Assert.assertNotEquals(null, simpleHttpResponse);
155+
Assertions.assertNotEquals(null, simpleHttpResponse);
150156
logger.error(simpleHttpResponse.getBodyText());
151-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
152-
Assert.assertEquals(1, simpleHttpResponse.getVersion().getMajor());
157+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
158+
Assertions.assertEquals(1, simpleHttpResponse.getVersion().getMajor());
153159
logger.info("response code is {}", simpleHttpResponse.getCode());
154-
Assert.assertEquals(200, simpleHttpResponse.getCode());
160+
Assertions.assertEquals(200, simpleHttpResponse.getCode());
155161
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
156-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
162+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
157163
}
158164

159165
@Test
@@ -178,14 +184,14 @@ public void cancelled() {
178184
}
179185
});
180186
SimpleHttpResponse simpleHttpResponse = httpResponseFuture.get(2000, TimeUnit.MILLISECONDS);
181-
Assert.assertNotEquals(null, simpleHttpResponse);
187+
Assertions.assertNotEquals(null, simpleHttpResponse);
182188
logger.error(simpleHttpResponse.getBodyText());
183-
Assert.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
184-
Assert.assertEquals(1, simpleHttpResponse.getVersion().getMajor());
189+
Assertions.assertEquals("HTTP", simpleHttpResponse.getVersion().getProtocol());
190+
Assertions.assertEquals(1, simpleHttpResponse.getVersion().getMajor());
185191
logger.info("response code is {}", simpleHttpResponse.getCode());
186-
Assert.assertEquals(404, simpleHttpResponse.getCode());
192+
Assertions.assertEquals(404, simpleHttpResponse.getCode());
187193
logger.info("http response is: {}", simpleHttpResponse.getBodyText());
188-
Assert.assertEquals("", simpleHttpResponse.getBodyText());
194+
Assertions.assertEquals("", simpleHttpResponse.getBodyText());
189195
}
190196

191197
}

0 commit comments

Comments
 (0)