Skip to content

Commit be15d81

Browse files
committed
提高单测覆盖率
1 parent 84ed440 commit be15d81

1 file changed

Lines changed: 51 additions & 51 deletions

File tree

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

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ public class AbstractHttpExecutorTest {
5757
@Test
5858
public void buildRpcInvocation_shouldSuccess() throws Exception {
5959
HttpServletRequest request = mock(HttpServletRequest.class);
60+
doReturn("trpc.demo.server").when(request).getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_SERVICE);
61+
doReturn("hello").when(request).getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_METHOD);
62+
6063
RpcMethodInfo methodInfo = mock(RpcMethodInfo.class);
6164
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
6265
doReturn(null).when(abstractHttpExecutor, "parseRpcParams", request, methodInfo);
63-
doReturn("trpc.demo.server").when(request).getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_SERVICE);
64-
doReturn("hello").when(request).getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_METHOD);
6566
when(abstractHttpExecutor, "buildRpcInvocation", request, methodInfo).thenCallRealMethod();
67+
6668
RpcInvocation rpcInvocation = Whitebox.invokeMethod(abstractHttpExecutor, "buildRpcInvocation", request,
6769
methodInfo);
6870
assertEquals(rpcInvocation.getFunc(), "/trpc.demo.server/hello");
@@ -71,36 +73,37 @@ public void buildRpcInvocation_shouldSuccess() throws Exception {
7173
@Test
7274
public void execute_shouldHandleTimeoutException() throws Exception {
7375
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+
7481
HttpServletResponse response = mock(HttpServletResponse.class);
75-
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
76-
RpcMethodInfoAndInvoker methodInfoAndInvoker = mock(RpcMethodInfoAndInvoker.class);
82+
7783
RpcMethodInfo methodInfo = mock(RpcMethodInfo.class);
7884
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
85+
CompletableFuture<com.tencent.trpc.core.rpc.Response> neverCompleteFuture = new CompletableFuture<>();
86+
when(invoker.invoke(any())).thenReturn(neverCompleteFuture);
87+
7988
ProviderConfig providerConfig = mock(ProviderConfig.class);
89+
when(providerConfig.getRequestTimeout()).thenReturn(100); // 设置100ms超时
8090
WorkerPool workerPool = mock(WorkerPool.class);
91+
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
92+
when(invoker.getConfig()).thenReturn(providerConfig);
8193

94+
RpcMethodInfoAndInvoker methodInfoAndInvoker = mock(RpcMethodInfoAndInvoker.class);
8295
when(methodInfoAndInvoker.getMethodInfo()).thenReturn(methodInfo);
8396
doReturn(invoker).when(methodInfoAndInvoker, "getInvoker");
84-
when(invoker.getConfig()).thenReturn(providerConfig);
85-
when(providerConfig.getRequestTimeout()).thenReturn(100); // 设置100ms超时
86-
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
87-
88-
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_SERVICE)).thenReturn("trpc.demo.server");
89-
when(request.getAttribute(HttpConstants.REQUEST_ATTRIBUTE_TRPC_METHOD)).thenReturn("hello");
90-
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
91-
when(request.getRemotePort()).thenReturn(8080);
92-
93-
CompletableFuture<com.tencent.trpc.core.rpc.Response> neverCompleteFuture = new CompletableFuture<>();
94-
when(invoker.invoke(any())).thenReturn(neverCompleteFuture);
9597

98+
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
9699
doReturn(null).when(abstractHttpExecutor, "parseRpcParams", any(), any());
97-
when(abstractHttpExecutor, "execute", request, response, methodInfoAndInvoker).thenCallRealMethod();
98-
doCallRealMethod().when(abstractHttpExecutor, "doErrorReply", any(), any(), any());
99-
doCallRealMethod().when(abstractHttpExecutor, "httpErrorReply", any(), any(), any());
100100
DefRequest defRequest = new DefRequest();
101101
doReturn(defRequest).when(abstractHttpExecutor, "buildDefRequest", any(), any(), any());
102102
HttpCodec httpCodec = mock(HttpCodec.class);
103103
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
104+
when(abstractHttpExecutor, "execute", request, response, methodInfoAndInvoker).thenCallRealMethod();
105+
doCallRealMethod().when(abstractHttpExecutor, "doErrorReply", any(), any(), any());
106+
doCallRealMethod().when(abstractHttpExecutor, "httpErrorReply", any(), any(), any());
104107
when(abstractHttpExecutor, "invokeRpcRequest", any(), any(), any(), any()).thenCallRealMethod();
105108

106109
Whitebox.invokeMethod(abstractHttpExecutor, "execute", request, response, methodInfoAndInvoker);
@@ -111,28 +114,26 @@ public void execute_shouldHandleTimeoutException() throws Exception {
111114
@Test
112115
public void handleError_shouldHandleErrorCorrectly() throws Exception {
113116
HttpServletRequest request = mock(HttpServletRequest.class);
114-
HttpServletResponse response = mock(HttpServletResponse.class);
115-
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
116-
DefRequest defRequest = new DefRequest();
117-
AtomicBoolean responded = new AtomicBoolean(false);
118-
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
119-
Throwable testException = new RuntimeException("Test error");
120-
121117
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
122118
when(request.getMethod()).thenReturn("POST");
123119
when(request.getRequestURI()).thenReturn("/api/test");
124120
when(request.getQueryString()).thenReturn("param=value");
125121

122+
HttpServletResponse response = mock(HttpServletResponse.class);
123+
DefRequest defRequest = new DefRequest();
126124
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_REQUEST, request);
127125

126+
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
128127
HttpCodec httpCodec = mock(HttpCodec.class);
129128
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
130-
131129
doCallRealMethod().when(abstractHttpExecutor, "handleError", any(Throwable.class), any(DefRequest.class),
132130
any(HttpServletResponse.class), any(AtomicBoolean.class), any(CompletableFuture.class));
133131
doCallRealMethod().when(abstractHttpExecutor, "httpErrorReply", any(), any(), any());
134132
doReturn(request).when(abstractHttpExecutor, "getOriginalRequest", any());
135133

134+
AtomicBoolean responded = new AtomicBoolean(false);
135+
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
136+
Throwable testException = new RuntimeException("Test error");
136137
Whitebox.invokeMethod(abstractHttpExecutor, "handleError", testException, defRequest, response,
137138
responded, completionFuture);
138139

@@ -144,24 +145,13 @@ public void handleError_shouldHandleErrorCorrectly() throws Exception {
144145

145146
@Test
146147
public void invokeRpcRequest_shouldHandleThrowable() throws Exception {
147-
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
148-
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
149-
ProviderConfig providerConfig = mock(ProviderConfig.class);
150-
WorkerPool workerPool = mock(WorkerPool.class);
151148
HttpServletResponse response = mock(HttpServletResponse.class);
152149
HttpServletRequest request = mock(HttpServletRequest.class);
153150
DefRequest defRequest = new DefRequest();
154-
AtomicBoolean responded = new AtomicBoolean(false);
155-
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
156-
HttpCodec httpCodec = mock(HttpCodec.class);
157-
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
158-
159151
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_RESPONSE, response);
160152
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_REQUEST, request);
161153

162-
when(invoker.getConfig()).thenReturn(providerConfig);
163-
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
164-
154+
WorkerPool workerPool = mock(WorkerPool.class);
165155
doAnswer(invocation -> {
166156
Object arg = invocation.getArguments()[0];
167157
if (arg instanceof Runnable) {
@@ -172,16 +162,27 @@ public void invokeRpcRequest_shouldHandleThrowable() throws Exception {
172162
return null;
173163
}).when(workerPool).execute(any());
174164

165+
ProviderConfig providerConfig = mock(ProviderConfig.class);
166+
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
167+
168+
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
169+
when(invoker.getConfig()).thenReturn(providerConfig);
175170
CompletableFuture<Response> failedFuture = new CompletableFuture<>();
176171
failedFuture.completeExceptionally(new RuntimeException("boom"));
177172
when(invoker.invoke(any())).thenReturn(failedFuture);
173+
174+
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
175+
HttpCodec httpCodec = mock(HttpCodec.class);
176+
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
178177
doReturn(response).when(abstractHttpExecutor, "getOriginalResponse", any());
179178
doReturn(request).when(abstractHttpExecutor, "getOriginalRequest", any());
180179
doCallRealMethod().when(abstractHttpExecutor, "invokeRpcRequest", any(), any(), any(), any());
181180
doCallRealMethod().when(abstractHttpExecutor, "httpErrorReply", any(), any(), any());
182181
doCallRealMethod().when(abstractHttpExecutor, "handleError", any(Throwable.class), any(DefRequest.class),
183182
any(HttpServletResponse.class), any(AtomicBoolean.class), any(CompletableFuture.class));
184183

184+
AtomicBoolean responded = new AtomicBoolean(false);
185+
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
185186
Whitebox.invokeMethod(abstractHttpExecutor, "invokeRpcRequest", invoker, defRequest, completionFuture,
186187
responded);
187188

@@ -193,24 +194,13 @@ public void invokeRpcRequest_shouldHandleThrowable() throws Exception {
193194

194195
@Test
195196
public void invokeRpcRequest_shouldHandleInvokeThrowsExceptionDirectly() throws Exception {
196-
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
197-
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
198-
ProviderConfig providerConfig = mock(ProviderConfig.class);
199-
WorkerPool workerPool = mock(WorkerPool.class);
200197
HttpServletResponse response = mock(HttpServletResponse.class);
201198
HttpServletRequest request = mock(HttpServletRequest.class);
202199
DefRequest defRequest = new DefRequest();
203-
AtomicBoolean responded = new AtomicBoolean(false);
204-
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
205-
HttpCodec httpCodec = mock(HttpCodec.class);
206-
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
207-
208200
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_RESPONSE, response);
209201
defRequest.getAttachments().put(HttpConstants.TRPC_ATTACH_SERVLET_REQUEST, request);
210202

211-
when(invoker.getConfig()).thenReturn(providerConfig);
212-
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
213-
203+
WorkerPool workerPool = mock(WorkerPool.class);
214204
doAnswer(invocation -> {
215205
Object arg = invocation.getArguments()[0];
216206
if (arg instanceof Runnable) {
@@ -221,15 +211,25 @@ public void invokeRpcRequest_shouldHandleInvokeThrowsExceptionDirectly() throws
221211
return null;
222212
}).when(workerPool).execute(any());
223213

214+
ProviderConfig providerConfig = mock(ProviderConfig.class);
215+
when(providerConfig.getWorkerPoolObj()).thenReturn(workerPool);
216+
217+
ProviderInvoker<?> invoker = mock(ProviderInvoker.class);
218+
when(invoker.getConfig()).thenReturn(providerConfig);
224219
when(invoker.invoke(any())).thenThrow(new RuntimeException("boom-direct"));
225220

221+
AbstractHttpExecutor abstractHttpExecutor = mock(AbstractHttpExecutor.class);
222+
HttpCodec httpCodec = mock(HttpCodec.class);
223+
Whitebox.setInternalState(abstractHttpExecutor, "httpCodec", httpCodec);
226224
doReturn(response).when(abstractHttpExecutor, "getOriginalResponse", any());
227225
doReturn(request).when(abstractHttpExecutor, "getOriginalRequest", any());
228226
doCallRealMethod().when(abstractHttpExecutor, "invokeRpcRequest", any(), any(), any(), any());
229227
doCallRealMethod().when(abstractHttpExecutor, "httpErrorReply", any(), any(), any());
230228
doCallRealMethod().when(abstractHttpExecutor, "handleError", any(Throwable.class), any(DefRequest.class),
231229
any(HttpServletResponse.class), any(AtomicBoolean.class), any(CompletableFuture.class));
232230

231+
AtomicBoolean responded = new AtomicBoolean(false);
232+
CompletableFuture<Void> completionFuture = new CompletableFuture<>();
233233
Whitebox.invokeMethod(abstractHttpExecutor, "invokeRpcRequest", invoker, defRequest, completionFuture,
234234
responded);
235235

@@ -238,4 +238,4 @@ public void invokeRpcRequest_shouldHandleInvokeThrowsExceptionDirectly() throws
238238
verify(response).setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE);
239239
verify(httpCodec).writeHttpResponse(any(HttpServletResponse.class), any());
240240
}
241-
}
241+
}

0 commit comments

Comments
 (0)