@@ -66,7 +66,6 @@ public void buildRpcInvocation_shouldSuccess() throws Exception {
6666
6767 @ Test
6868 public void execute_shouldHandleTimeoutException () throws Exception {
69- // 准备测试数据
7069 HttpServletRequest request = mock (HttpServletRequest .class );
7170 HttpServletResponse response = mock (HttpServletResponse .class );
7271 AbstractHttpExecutor abstractHttpExecutor = mock (AbstractHttpExecutor .class );
@@ -76,39 +75,32 @@ public void execute_shouldHandleTimeoutException() throws Exception {
7675 ProviderConfig providerConfig = mock (ProviderConfig .class );
7776 WorkerPool workerPool = mock (WorkerPool .class );
7877
79- // 模拟配置和基本信息
8078 when (methodInfoAndInvoker .getMethodInfo ()).thenReturn (methodInfo );
8179 doReturn (invoker ).when (methodInfoAndInvoker , "getInvoker" );
8280 when (invoker .getConfig ()).thenReturn (providerConfig );
8381 when (providerConfig .getRequestTimeout ()).thenReturn (100 ); // 设置100ms超时
8482 when (providerConfig .getWorkerPoolObj ()).thenReturn (workerPool );
8583
86- // 模拟请求属性
8784 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_SERVICE )).thenReturn ("trpc.demo.server" );
8885 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_METHOD )).thenReturn ("hello" );
8986 when (request .getRemoteAddr ()).thenReturn ("127.0.0.1" );
9087 when (request .getRemotePort ()).thenReturn (8080 );
9188
92- // 模拟一个永远不会完成的CompletionStage,导致超时
9389 CompletableFuture <com .tencent .trpc .core .rpc .Response > neverCompleteFuture = new CompletableFuture <>();
9490 when (invoker .invoke (any ())).thenReturn (neverCompleteFuture );
9591
96- // 模拟私有方法调用
9792 doReturn (null ).when (abstractHttpExecutor , "parseRpcParams" , any (), any ());
9893 when (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker ).thenCallRealMethod ();
9994 doCallRealMethod ().when (abstractHttpExecutor , "doErrorReply" , any (), any (), any ());
10095 doCallRealMethod ().when (abstractHttpExecutor , "httpErrorReply" , any (), any (), any ());
10196
102- // 执行测试
10397 Whitebox .invokeMethod (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker );
10498
105- // 验证超时后调用了错误响应(由于异步执行,超时也通过handleError处理,返回503)
10699 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
107100 }
108101
109102 @ Test
110103 public void execute_shouldHandleInvokeException () throws Exception {
111- // 准备测试数据
112104 HttpServletRequest request = mock (HttpServletRequest .class );
113105 HttpServletResponse response = mock (HttpServletResponse .class );
114106 AbstractHttpExecutor abstractHttpExecutor = mock (AbstractHttpExecutor .class );
@@ -118,49 +110,41 @@ public void execute_shouldHandleInvokeException() throws Exception {
118110 ProviderConfig providerConfig = mock (ProviderConfig .class );
119111 WorkerPool workerPool = mock (WorkerPool .class );
120112
121- // 模拟配置和基本信息
122113 when (methodInfoAndInvoker .getMethodInfo ()).thenReturn (methodInfo );
123114 doReturn (invoker ).when (methodInfoAndInvoker , "getInvoker" );
124115 when (invoker .getConfig ()).thenReturn (providerConfig );
125116 when (providerConfig .getRequestTimeout ()).thenReturn (5000 );
126117 when (providerConfig .getWorkerPoolObj ()).thenReturn (workerPool );
127118
128- // 模拟请求属性
129119 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_SERVICE )).thenReturn ("trpc.demo.server" );
130120 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_METHOD )).thenReturn ("hello" );
131121 when (request .getRemoteAddr ()).thenReturn ("127.0.0.1" );
132122 when (request .getRemotePort ()).thenReturn (8080 );
133123 when (request .getMethod ()).thenReturn ("POST" );
134124 when (request .getRequestURI ()).thenReturn ("/api/test" );
135125
136- // 模拟WorkerPool同步执行任务
137126 doAnswer (invocation -> {
138127 Task task = invocation .getArgumentAt (0 , Task .class );
139128 task .run ();
140129 return null ;
141130 }).when (workerPool ).execute (any (Task .class ));
142131
143- // 模拟invoke抛出异常
144132 CompletableFuture <com .tencent .trpc .core .rpc .Response > failedFuture = new CompletableFuture <>();
145133 failedFuture .completeExceptionally (new RuntimeException ("Service invoke failed" ));
146134 when (invoker .invoke (any ())).thenReturn (failedFuture );
147135
148- // 模拟私有方法调用
149136 doReturn (null ).when (abstractHttpExecutor , "parseRpcParams" , any (), any ());
150137 when (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker ).thenCallRealMethod ();
151138 doCallRealMethod ().when (abstractHttpExecutor , "doErrorReply" , any (), any (), any ());
152139 doCallRealMethod ().when (abstractHttpExecutor , "httpErrorReply" , any (), any (), any ());
153140
154- // 执行测试
155141 Whitebox .invokeMethod (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker );
156142
157- // 验证调用了错误响应(invoke异常在handleError中处理,返回503)
158143 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
159144 }
160145
161146 @ Test
162147 public void execute_shouldHandleResponseException () throws Exception {
163- // 准备测试数据
164148 HttpServletRequest request = mock (HttpServletRequest .class );
165149 HttpServletResponse response = mock (HttpServletResponse .class );
166150 AbstractHttpExecutor abstractHttpExecutor = mock (AbstractHttpExecutor .class );
@@ -170,45 +154,38 @@ public void execute_shouldHandleResponseException() throws Exception {
170154 ProviderConfig providerConfig = mock (ProviderConfig .class );
171155 WorkerPool workerPool = mock (WorkerPool .class );
172156
173- // 模拟配置和基本信息
174157 when (methodInfoAndInvoker .getMethodInfo ()).thenReturn (methodInfo );
175158 doReturn (invoker ).when (methodInfoAndInvoker , "getInvoker" );
176159 when (invoker .getConfig ()).thenReturn (providerConfig );
177160 when (providerConfig .getRequestTimeout ()).thenReturn (5000 );
178161 when (providerConfig .getWorkerPoolObj ()).thenReturn (workerPool );
179162
180- // 模拟请求属性
181163 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_SERVICE )).thenReturn ("trpc.demo.server" );
182164 when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_METHOD )).thenReturn ("hello" );
183165 when (request .getRemoteAddr ()).thenReturn ("127.0.0.1" );
184166 when (request .getRemotePort ()).thenReturn (8080 );
185167 when (request .getMethod ()).thenReturn ("POST" );
186168 when (request .getRequestURI ()).thenReturn ("/api/test" );
187169
188- // 模拟WorkerPool同步执行任务
189170 doAnswer (invocation -> {
190171 Task task = invocation .getArgumentAt (0 , Task .class );
191172 task .run ();
192173 return null ;
193174 }).when (workerPool ).execute (any (Task .class ));
194175
195- // 模拟Response包含异常
196176 com .tencent .trpc .core .rpc .Response rpcResponse = mock (com .tencent .trpc .core .rpc .Response .class );
197177 when (rpcResponse .getException ()).thenReturn (
198178 TRpcException .newFrameException (ErrorCode .TRPC_SERVER_VALIDATE_ERR , "Validation failed" ));
199179 CompletableFuture <com .tencent .trpc .core .rpc .Response > responseFuture = CompletableFuture .completedFuture (rpcResponse );
200180 when (invoker .invoke (any ())).thenReturn (responseFuture );
201181
202- // 模拟私有方法调用
203182 doReturn (null ).when (abstractHttpExecutor , "parseRpcParams" , any (), any ());
204183 when (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker ).thenCallRealMethod ();
205184 doCallRealMethod ().when (abstractHttpExecutor , "doErrorReply" , any (), any (), any ());
206185 doCallRealMethod ().when (abstractHttpExecutor , "httpErrorReply" , any (), any (), any ());
207186
208- // 执行测试
209187 Whitebox .invokeMethod (abstractHttpExecutor , "execute" , request , response , methodInfoAndInvoker );
210188
211- // 验证调用了错误响应(Response异常在handleError中处理,返回503)
212189 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
213190 }
214191}
0 commit comments