Skip to content

Commit 240d5f9

Browse files
committed
提高单测覆盖率
1 parent be15d81 commit 240d5f9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,65 @@ public void buildRpcInvocation_shouldSuccess() throws Exception {
7070
assertEquals(rpcInvocation.getFunc(), "/trpc.demo.server/hello");
7171
}
7272

73+
@Test
74+
public void execute_shouldCompleteSuccessfully() throws Exception {
75+
HttpServletRequest request = mock(HttpServletRequest.class);
76+
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_SERVICE)).thenReturn("trpc.demo.server");
77+
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_METHOD)).thenReturn("hello");
78+
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
79+
when(request.getRemotePort()).thenReturn(8080);
80+
81+
HttpServletResponse response = mock(HttpServletResponse.class);
82+
RpcMethodInfo methodInfo = mock(RpcMethodInfo.class);
83+
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
84+
85+
// 创建一个成功的响应
86+
com.tencent.trpc.core.rpc.def.DefResponse successResponse =
87+
new com.tencent.trpc.core.rpc.def.DefResponse();
88+
successResponse.setValue("success");
89+
CompletableFuture<Response> successFuture = CompletableFuture.completedFuture(successResponse);
90+
when(invoker.invoke(any())).thenReturn(successFuture);
91+
92+
ProviderConfig providerConfig = mock(ProviderConfig.class);
93+
when(providerConfig.getRequestTimeout()).thenReturn(0); // 不设置超时,走completionFuture.get()分支
94+
WorkerPool workerPool = mock(WorkerPool.class);
95+
doAnswer(invocation -> {
96+
Object arg = invocation.getArguments()[0];
97+
if (arg instanceof Runnable) {
98+
((Runnable) arg).run();
99+
} else if (arg instanceof Task) {
100+
((Task) arg).run();
101+
}
102+
return null;
103+
}).when(workerPool).execute(any());
104+
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
105+
when(invoker.getConfig()).thenReturn(providerConfig);
106+
107+
RpcMethodInfoAndInvoker methodInfoAndInvoker = mock(RpcMethodInfoAndInvoker.class);
108+
when(methodInfoAndInvoker.getMethodInfo()).thenReturn(methodInfo);
109+
doReturn(invoker).when(methodInfoAndInvoker, "getInvoker");
110+
111+
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
112+
DefRequest defRequest = new DefRequest();
113+
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_RESPONSE, response);
114+
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_REQUEST, request);
115+
doReturn(defRequest).when(abstractHttpExecutor, "buildDefRequest", any(), any(), any());
116+
117+
HttpCodec httpCodec = mock(HttpCodec.class);
118+
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
119+
doReturn(response).when(abstractHttpExecutor, "getOriginalResponse", any());
120+
doCallRealMethod().when(abstractHttpExecutor, "execute", request, response, methodInfoAndInvoker);
121+
doCallRealMethod().when(abstractHttpExecutor, "invokeRpcRequest", any(), any(), any(), any());
122+
123+
// 执行测试
124+
Whitebox.invokeMethod(abstractHttpExecutor, "execute", request, response, methodInfoAndInvoker);
125+
126+
// 验证正常响应
127+
verify(response).setStatus(HttpStatus.SC_OK);
128+
verify(httpCodec).writeHttpResponse(response, successResponse);
129+
verify(response).flushBuffer();
130+
}
131+
73132
@Test
74133
public void execute_shouldHandleTimeoutException() throws Exception {
75134
HttpServletRequest request = mock(HttpServletRequest.class);

0 commit comments

Comments
 (0)