@@ -113,73 +113,61 @@ public void testBuildRpcInvocation() throws Exception {
113113 AbstractHttpExecutor executor = mock (AbstractHttpExecutor .class );
114114 doReturn (null ).when (executor , "parseRpcParams" , request , methodInfo );
115115 when (executor , "buildRpcInvocation" , request , methodInfo ).thenCallRealMethod ();
116-
117116 RpcInvocation invocation = Whitebox .invokeMethod (executor , "buildRpcInvocation" , request , methodInfo );
117+
118118 assertEquals ("/trpc.demo.server/hello" , invocation .getFunc ());
119119 }
120120
121121 @ Test
122122 public void testExecuteSuccess () throws Exception {
123- HttpServletRequest request = mockRequest ();
124- HttpServletResponse response = mock (HttpServletResponse .class );
125- RpcMethodInfo methodInfo = mock (RpcMethodInfo .class );
126123 ProviderInvoker <?> invoker = mock (ProviderInvoker .class );
127-
128124 DefResponse successResponse = new DefResponse ();
129125 successResponse .setValue ("success" );
130126 CompletableFuture <Response > successFuture = CompletableFuture .completedFuture (successResponse );
131127 when (invoker .invoke (any ())).thenReturn (successFuture );
132-
133128 ProviderConfig config = mockProviderConfig (0 );
134129 when (invoker .getConfig ()).thenReturn (config );
135-
130+ RpcMethodInfo methodInfo = mock ( RpcMethodInfo . class );
136131 RpcMethodInfoAndInvoker methodInfoAndInvoker = mock (RpcMethodInfoAndInvoker .class );
137132 when (methodInfoAndInvoker .getMethodInfo ()).thenReturn (methodInfo );
138133 doReturn (invoker ).when (methodInfoAndInvoker , "getInvoker" );
139-
140- AbstractHttpExecutor executor = mockExecutorWithCodec ( );
134+ HttpServletRequest request = mockRequest ();
135+ HttpServletResponse response = mock ( HttpServletResponse . class );
141136 DefRequest defRequest = mockDefRequest (request , response );
137+ AbstractHttpExecutor executor = mockExecutorWithCodec ();
142138 doReturn (defRequest ).when (executor , "buildDefRequest" , any (), any (), any ());
143139 doReturn (response ).when (executor , "getOriginalResponse" , any ());
144140 doCallRealMethod ().when (executor , "execute" , request , response , methodInfoAndInvoker );
145141 doCallRealMethod ().when (executor , "invokeRpcRequest" , any (), any (), any (), any ());
146-
147142 Whitebox .invokeMethod (executor , "execute" , request , response , methodInfoAndInvoker );
148-
149- HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
150143 verify (response ).setStatus (HttpStatus .SC_OK );
144+ HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
151145 verify (httpCodec ).writeHttpResponse (response , successResponse );
152146 verify (response ).flushBuffer ();
153147 }
154148
155149 @ Test
156150 public void testExecuteTimeout () throws Exception {
157- HttpServletRequest request = mockRequest ();
158- HttpServletResponse response = mock (HttpServletResponse .class );
159-
160- RpcMethodInfo methodInfo = mock (RpcMethodInfo .class );
161151 ProviderInvoker <?> invoker = mock (ProviderInvoker .class );
162152 CompletableFuture <Response > neverCompleteFuture = new CompletableFuture <>();
163153 when (invoker .invoke (any ())).thenReturn (neverCompleteFuture );
164-
165154 ProviderConfig config = mockProviderConfig (100 );
166155 when (invoker .getConfig ()).thenReturn (config );
167-
156+ RpcMethodInfo methodInfo = mock ( RpcMethodInfo . class );
168157 RpcMethodInfoAndInvoker methodInfoAndInvoker = mock (RpcMethodInfoAndInvoker .class );
169158 when (methodInfoAndInvoker .getMethodInfo ()).thenReturn (methodInfo );
170159 doReturn (invoker ).when (methodInfoAndInvoker , "getInvoker" );
171-
160+ HttpServletRequest request = mockRequest ();
161+ HttpServletResponse response = mock (HttpServletResponse .class );
162+ DefRequest defRequest = new DefRequest ();
172163 AbstractHttpExecutor executor = mockExecutorWithCodec ();
173164 doReturn (null ).when (executor , "parseRpcParams" , any (), any ());
174- DefRequest defRequest = new DefRequest ();
175165 doReturn (defRequest ).when (executor , "buildDefRequest" , any (), any (), any ());
176166 when (executor , "execute" , request , response , methodInfoAndInvoker ).thenCallRealMethod ();
177167 doCallRealMethod ().when (executor , "doErrorReply" , any (), any (), any ());
178168 doCallRealMethod ().when (executor , "httpErrorReply" , any (), any (), any ());
179169 when (executor , "invokeRpcRequest" , any (), any (), any (), any ()).thenCallRealMethod ();
180-
181170 Whitebox .invokeMethod (executor , "execute" , request , response , methodInfoAndInvoker );
182-
183171 verify (response ).setStatus (HttpStatus .SC_REQUEST_TIMEOUT );
184172 }
185173
@@ -190,34 +178,30 @@ public void testHandleError() throws Exception {
190178 when (request .getMethod ()).thenReturn ("POST" );
191179 when (request .getRequestURI ()).thenReturn ("/api/test" );
192180 when (request .getQueryString ()).thenReturn ("param=value" );
193-
194- HttpServletResponse response = mock (HttpServletResponse .class );
195181 DefRequest defRequest = new DefRequest ();
196182 defRequest .getAttachments ().put (HttpConstants .TRPC_ATTACH_SERVLET_REQUEST , request );
197-
183+ HttpServletResponse response = mock ( HttpServletResponse . class );
198184 AbstractHttpExecutor executor = mockExecutorWithCodec ();
199185 doCallRealMethod ().when (executor , "handleError" , any (Throwable .class ), any (DefRequest .class ),
200186 any (HttpServletResponse .class ), any (AtomicBoolean .class ), any (CompletableFuture .class ));
201187 doCallRealMethod ().when (executor , "httpErrorReply" , any (), any (), any ());
202188 doReturn (request ).when (executor , "getOriginalRequest" , any ());
203-
204189 AtomicBoolean responded = new AtomicBoolean (false );
205190 CompletableFuture <Void > completionFuture = new CompletableFuture <>();
206191 Throwable testException = new RuntimeException ("Test error" );
207192 Whitebox .invokeMethod (executor , "handleError" , testException , defRequest , response ,
208193 responded , completionFuture );
209-
210- HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
211194 assertTrue (responded .get ());
212195 assertTrue (completionFuture .isCompletedExceptionally ());
213196 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
197+ HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
214198 verify (httpCodec ).writeHttpResponse (any (HttpServletResponse .class ), any ());
215199 }
216200
217201 @ Test
218202 public void testInvokeRpcWithException () throws Exception {
219- HttpServletResponse response = mock (HttpServletResponse .class );
220203 HttpServletRequest request = mock (HttpServletRequest .class );
204+ HttpServletResponse response = mock (HttpServletResponse .class );
221205 DefRequest defRequest = mockDefRequest (request , response );
222206
223207 ProviderConfig config = mockProviderConfig (0 );
@@ -238,18 +222,18 @@ public void testInvokeRpcWithException() throws Exception {
238222 AtomicBoolean responded = new AtomicBoolean (false );
239223 CompletableFuture <Void > completionFuture = new CompletableFuture <>();
240224 Whitebox .invokeMethod (executor , "invokeRpcRequest" , invoker , defRequest , completionFuture , responded );
241-
242- HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
225+
243226 assertTrue (responded .get ());
244227 assertTrue (completionFuture .isCompletedExceptionally ());
245228 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
229+ HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
246230 verify (httpCodec ).writeHttpResponse (any (HttpServletResponse .class ), any ());
247231 }
248232
249233 @ Test
250234 public void testInvokeRpcThrowsDirectly () throws Exception {
251- HttpServletResponse response = mock (HttpServletResponse .class );
252235 HttpServletRequest request = mock (HttpServletRequest .class );
236+ HttpServletResponse response = mock (HttpServletResponse .class );
253237 DefRequest defRequest = mockDefRequest (request , response );
254238
255239 ProviderConfig config = mockProviderConfig (0 );
@@ -268,11 +252,11 @@ public void testInvokeRpcThrowsDirectly() throws Exception {
268252 AtomicBoolean responded = new AtomicBoolean (false );
269253 CompletableFuture <Void > completionFuture = new CompletableFuture <>();
270254 Whitebox .invokeMethod (executor , "invokeRpcRequest" , invoker , defRequest , completionFuture , responded );
271-
272- HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
255+
273256 assertTrue (responded .get ());
274257 assertTrue (completionFuture .isCompletedExceptionally ());
275258 verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
259+ HttpCodec httpCodec = Whitebox .getInternalState (executor , "httpCodec" );
276260 verify (httpCodec ).writeHttpResponse (any (HttpServletResponse .class ), any ());
277261 }
278262}
0 commit comments