Skip to content

Commit 8301f47

Browse files
committed
bugfix: 修复http协议偶发串包问题
1 parent d905be4 commit 8301f47

3 files changed

Lines changed: 119 additions & 1 deletion

File tree

.github/workflows/pull_request.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ on:
1313
branches: [ "JDK17/Springboot3" ]
1414

1515
jobs:
16-
test:
16+
JDK17-Test:
1717
runs-on: ubuntu-latest
1818
steps:
1919
- name: Checkout codes
@@ -35,3 +35,19 @@ jobs:
3535
token: ${{ secrets.CODECOV_TOKEN }}
3636
flags: pull-request
3737
name: PR-Coverage
38+
JDK21-Test:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Checkout codes
42+
uses: actions/checkout@v3
43+
with:
44+
ref: ${{ github.event.pull_request.head.ref }}
45+
repository: ${{ github.event.pull_request.head.repo.full_name }}
46+
- name: Set up JDK 21
47+
uses: actions/setup-java@v3
48+
with:
49+
java-version: '21'
50+
distribution: 'temurin'
51+
cache: maven
52+
- name: Test with Maven
53+
run: mvn clean install -B -U --file pom.xml

trpc-proto/trpc-proto-http/src/test/java/com/tencent/trpc/proto/http/server/AbstractHttpExecutorTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.mockito.Mockito.verify;
2222
import static org.mockito.Mockito.when;
2323

24+
import com.google.protobuf.Message;
2425
import com.tencent.trpc.core.common.ConfigManager;
2526
import com.tencent.trpc.core.common.config.ProviderConfig;
2627
import com.tencent.trpc.core.exception.ErrorCode;
@@ -45,6 +46,7 @@
4546
import java.lang.reflect.ParameterizedType;
4647
import java.lang.reflect.Type;
4748
import java.util.Collections;
49+
import java.util.Enumeration;
4850
import java.util.HashMap;
4951
import java.util.Map;
5052
import java.util.concurrent.CompletableFuture;
@@ -687,8 +689,95 @@ public void testExecuteWithTransInfo() throws Exception {
687689
verify(response).setStatus(HttpStatus.SC_OK);
688690
}
689691

692+
// ==================== execute catch branch ====================
693+
694+
@Test
695+
public void testExecuteCatchBranch() throws Exception {
696+
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
697+
ProviderConfig config = mockProviderConfig(0);
698+
when(invoker.getConfig()).thenReturn(config);
699+
700+
HttpServletRequest request = mockRequest();
701+
HttpServletResponse response = mock(HttpServletResponse.class);
702+
703+
Method method = BadService.class.getMethod("hello", String.class);
704+
RpcMethodInfo methodInfo = new RpcMethodInfo(BadService.class, method);
705+
RpcMethodInfoAndInvoker methodInfoAndInvoker = new RpcMethodInfoAndInvoker();
706+
methodInfoAndInvoker.setMethodInfo(methodInfo);
707+
methodInfoAndInvoker.setInvoker(invoker);
708+
709+
AbstractHttpExecutor executor = createExecutorWithInvoker(methodInfoAndInvoker);
710+
711+
executor.execute(request, response, methodInfoAndInvoker);
712+
713+
verify(response).setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE);
714+
}
715+
716+
// ==================== parseRpcParams Protobuf ====================
717+
718+
@Test
719+
public void testParseRpcParamsProtobuf() throws Exception {
720+
HttpServletRequest request = mock(HttpServletRequest.class);
721+
RpcMethodInfo methodInfo = mock(RpcMethodInfo.class);
722+
when(methodInfo.getParamsTypes()).thenReturn(
723+
new Type[]{RpcContext.class, tests.service.HelloRequestProtocol.HelloRequest.class});
724+
725+
AbstractHttpExecutor executor = createExecutorWithCodec();
726+
HttpCodec httpCodec = (HttpCodec) getField(executor, "httpCodec");
727+
tests.service.HelloRequestProtocol.HelloRequest mockMsg =
728+
tests.service.HelloRequestProtocol.HelloRequest.getDefaultInstance();
729+
when(httpCodec.convertToPBParam(any(), any())).thenReturn(mockMsg);
730+
731+
Object[] result = (Object[]) invokePrivate(executor, "parseRpcParams",
732+
new Class[]{HttpServletRequest.class, RpcMethodInfo.class}, request, methodInfo);
733+
734+
assertNotNull(result);
735+
assertEquals(mockMsg, result[0]);
736+
}
737+
738+
// ==================== setRpcServerContext header loop ====================
739+
740+
@Test
741+
public void testExecuteWithHeaders() throws Exception {
742+
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
743+
ProviderConfig config = mockProviderConfig(0);
744+
when(invoker.getConfig()).thenReturn(config);
745+
746+
DefResponse successResponse = new DefResponse();
747+
successResponse.setValue("ok");
748+
when(invoker.invoke(any())).thenReturn(CompletableFuture.completedFuture(successResponse));
749+
750+
HttpServletRequest request = mock(HttpServletRequest.class);
751+
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_SERVICE)).thenReturn(TEST_SERVICE);
752+
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_METHOD)).thenReturn(TEST_METHOD);
753+
when(request.getRemoteAddr()).thenReturn(TEST_IP);
754+
when(request.getRemotePort()).thenReturn(TEST_PORT);
755+
when(request.getMethod()).thenReturn("POST");
756+
when(request.getRequestURI()).thenReturn("/api/test");
757+
Enumeration<String> headerNames = Collections.enumeration(Collections.singletonList("X-Custom"));
758+
when(request.getHeaderNames()).thenReturn(headerNames);
759+
when(request.getHeader("X-Custom")).thenReturn("custom-value");
760+
HttpServletResponse response = mock(HttpServletResponse.class);
761+
762+
RpcMethodInfoAndInvoker methodInfoAndInvoker = buildMethodInfoAndInvoker(invoker);
763+
AbstractHttpExecutor executor = createExecutorWithInvoker(methodInfoAndInvoker);
764+
HttpCodec httpCodec = (HttpCodec) getField(executor, "httpCodec");
765+
when(httpCodec.convertToJavaBean(any(), any())).thenReturn("param");
766+
767+
executor.execute(request, response, methodInfoAndInvoker);
768+
769+
verify(response).setStatus(HttpStatus.SC_OK);
770+
}
771+
772+
// ==================== TestService ====================
773+
690774
private interface TestService {
691775

692776
String hello(RpcContext ctx, String req);
693777
}
778+
779+
private interface BadService {
780+
781+
String hello(String req);
782+
}
694783
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="WARN">
10+
<AppenderRef ref="Console"/>
11+
</Root>
12+
</Loggers>
13+
</Configuration>

0 commit comments

Comments
 (0)