4444import org .apache .rocketmq .common .MixAll ;
4545import org .apache .rocketmq .common .message .MessageDecoder ;
4646import org .apache .rocketmq .common .message .MessageExt ;
47+ import org .apache .rocketmq .remoting .InvokeCallback ;
4748import org .apache .rocketmq .remoting .common .SemaphoreReleaseOnlyOnce ;
4849import org .apache .rocketmq .remoting .exception .RemotingTimeoutException ;
4950import org .apache .rocketmq .remoting .netty .NettyClientConfig ;
@@ -334,12 +335,7 @@ public void testPullMessageFromSpecificBrokerAsync_timeout() throws Exception {
334335
335336 @ Test
336337 public void testPullMessageFromSpecificBrokerAsync_brokerReturn_pullStatusCode () throws Exception {
337- Channel channel = Mockito .mock (Channel .class );
338- when (channel .isActive ()).thenReturn (true );
339- NettyRemotingClient mockClient = PowerMockito .spy (new NettyRemotingClient (new NettyClientConfig ()));
340- DefaultChannelPromise promise = PowerMockito .spy (new DefaultChannelPromise (PowerMockito .mock (Channel .class ), new DefaultEventExecutor ()));
341- PowerMockito .when (mockClient , "getAndCreateChannelAsync" , any ()).thenReturn (promise );
342- when (promise .channel ()).thenReturn (channel );
338+ NettyRemotingClient mockClient = Mockito .mock (NettyRemotingClient .class );
343339 BrokerOuterAPI api = new BrokerOuterAPI (new NettyClientConfig (), new AuthConfig ());
344340 Field field = BrokerOuterAPI .class .getDeclaredField ("remotingClient" );
345341 field .setAccessible (true );
@@ -348,14 +344,12 @@ public void testPullMessageFromSpecificBrokerAsync_brokerReturn_pullStatusCode()
348344 int [] respCodes = new int [] {ResponseCode .SUCCESS , ResponseCode .PULL_NOT_FOUND , ResponseCode .PULL_RETRY_IMMEDIATELY , ResponseCode .PULL_OFFSET_MOVED };
349345 PullStatus [] respStatus = new PullStatus [] {PullStatus .FOUND , PullStatus .NO_NEW_MSG , PullStatus .NO_MATCHED_MSG , PullStatus .OFFSET_ILLEGAL };
350346 for (int i = 0 ; i < respCodes .length ; i ++) {
351- CompletableFuture <ResponseFuture > future = new CompletableFuture <>();
352- doReturn (future ).when (mockClient ).invokeImpl (any (Channel .class ), any (RemotingCommand .class ), anyLong ());
353347 RemotingCommand response = mockPullMessageResponse (respCodes [i ]);
354- ResponseFuture responseFuture = new ResponseFuture ( channel , 0 , null , 1000 ,
355- resp -> { }, new SemaphoreReleaseOnlyOnce ( new Semaphore ( 1 )) );
356- responseFuture . setResponseCommand (response );
357- promise . trySuccess ( null ) ;
358- future . complete ( responseFuture );
348+ Mockito . doAnswer ( invocation -> {
349+ InvokeCallback callback = invocation . getArgument ( 3 );
350+ callback . operationSucceed (response );
351+ return null ;
352+ }). when ( mockClient ). invokeAsync ( anyString (), any ( RemotingCommand . class ), anyLong (), any ( InvokeCallback . class ) );
359353
360354 Triple <PullResult , String , Boolean > rst = api .pullMessageFromSpecificBrokerAsync ("" , "" , "" , "" , 1 , 1 , 1 , 3000L ).join ();
361355 Assert .assertEquals (respStatus [i ], rst .getLeft ().getPullStatus ());
0 commit comments