3333import com .google .api .gax .rpc .ResponseObserver ;
3434import com .google .api .gax .rpc .StatusCode ;
3535import com .google .api .gax .rpc .StreamController ;
36+ import com .google .api .gax .rpc .UnaryCallable ;
3637import com .google .cloud .pubsub .v1 .stub .SubscriberStub ;
3738import com .google .common .collect .Lists ;
3839import com .google .protobuf .Any ;
40+ import com .google .protobuf .Empty ;
3941import com .google .pubsub .v1 .AcknowledgeRequest ;
4042import com .google .pubsub .v1 .ModifyAckDeadlineRequest ;
4143import com .google .pubsub .v1 .StreamingPullRequest ;
@@ -66,6 +68,9 @@ public class StreamingSubscriberConnectionTest {
6668 private FakeScheduledExecutorService executor ;
6769 private FakeClock clock ;
6870 private SubscriberStub mockSubscriberStub ;
71+ private BidiStreamingCallable <StreamingPullRequest , StreamingPullResponse > mockStreamingCallable ;
72+ private UnaryCallable <AcknowledgeRequest , Empty > mockAcknowledgeCallable ;
73+ private UnaryCallable <ModifyAckDeadlineRequest , Empty > mockModifyAckDeadlineCallable ;
6974
7075 private static final String MOCK_SUBSCRIPTION_NAME =
7176 "projects/MOCK-PROJECT/subscriptions/MOCK-SUBSCRIPTION" ;
@@ -103,10 +108,15 @@ public void setUp() {
103108 systemExecutor = new FakeScheduledExecutorService ();
104109 executor = new FakeScheduledExecutorService ();
105110 clock = systemExecutor .getClock ();
106- mockSubscriberStub =
107- mock (
108- SubscriberStub .class ,
109- withSettings ().withoutAnnotations ().defaultAnswer (RETURNS_DEEP_STUBS ));
111+
112+ mockStreamingCallable = mock (BidiStreamingCallable .class , withSettings ().withoutAnnotations ());
113+ mockAcknowledgeCallable = mock (UnaryCallable .class , withSettings ().withoutAnnotations ());
114+ mockModifyAckDeadlineCallable = mock (UnaryCallable .class , withSettings ().withoutAnnotations ());
115+
116+ mockSubscriberStub = mock (SubscriberStub .class , withSettings ().withoutAnnotations ());
117+ when (mockSubscriberStub .streamingPullCallable ()).thenReturn (mockStreamingCallable );
118+ when (mockSubscriberStub .acknowledgeCallable ()).thenReturn (mockAcknowledgeCallable );
119+ when (mockSubscriberStub .modifyAckDeadlineCallable ()).thenReturn (mockModifyAckDeadlineCallable );
110120 }
111121
112122 @ After
@@ -391,12 +401,9 @@ public void testSendAckOperationsExactlyOnceEnabledMessageFuturesModacks() {
391401 systemExecutor .advanceTime (Duration .ofSeconds (200 ));
392402
393403 // Assert expected behavior
394- verify (mockSubscriberStub .modifyAckDeadlineCallable (), times (1 ))
395- .futureCall (modifyAckDeadlineRequestNack );
396- verify (mockSubscriberStub .modifyAckDeadlineCallable (), times (1 ))
397- .futureCall (modifyAckDeadlineRequestInitial );
398- verify (mockSubscriberStub .modifyAckDeadlineCallable (), times (1 ))
399- .futureCall (modifyAckDeadlineRequestRetry );
404+ verify (mockModifyAckDeadlineCallable , times (1 )).futureCall (modifyAckDeadlineRequestNack );
405+ verify (mockModifyAckDeadlineCallable , times (1 )).futureCall (modifyAckDeadlineRequestInitial );
406+ verify (mockModifyAckDeadlineCallable , times (1 )).futureCall (modifyAckDeadlineRequestRetry );
400407 verify (mockSubscriberStub , never ()).acknowledgeCallable ();
401408
402409 try {
@@ -511,9 +518,8 @@ public void testSendAckOperationsExactlyOnceEnabledMessageFuturesAcks() {
511518 systemExecutor .advanceTime (Duration .ofMillis (200 ));
512519
513520 // Assert expected behavior;
514- verify (mockSubscriberStub .acknowledgeCallable (), times (1 ))
515- .futureCall (acknowledgeRequestInitial );
516- verify (mockSubscriberStub .acknowledgeCallable (), times (1 ))
521+ verify (mockAcknowledgeCallable , times (1 )).futureCall (acknowledgeRequestInitial );
522+ verify (mockAcknowledgeCallable , times (1 ))
517523 .futureCall (
518524 argThat (new CustomArgumentMatchers .AcknowledgeRequestMatcher (acknowledgeRequestRetry )));
519525 verify (mockSubscriberStub , never ()).modifyAckDeadlineCallable ();
@@ -577,7 +583,7 @@ public void testSendAckOperationsExactlyOnceEnabledErrorWithEmptyMetadataMap() {
577583 systemExecutor .advanceTime (Duration .ofMillis (200 ));
578584
579585 // Assert expected behavior;
580- verify (mockSubscriberStub . acknowledgeCallable () , times (2 )).futureCall (acknowledgeRequest );
586+ verify (mockAcknowledgeCallable , times (2 )).futureCall (acknowledgeRequest );
581587 verify (mockSubscriberStub , never ()).modifyAckDeadlineCallable ();
582588
583589 try {
@@ -677,27 +683,22 @@ public void testMaxPerRequestChanges() {
677683 .setSubscription (MOCK_SUBSCRIPTION_NAME )
678684 .addAllAckIds (mockAckIdsInRequest )
679685 .build ();
680- verify (mockSubscriberStub .acknowledgeCallable (), times (1 ))
681- .futureCall (expectedAcknowledgeRequest );
686+ verify (mockAcknowledgeCallable , times (1 )).futureCall (expectedAcknowledgeRequest );
682687
683688 ModifyAckDeadlineRequest expectedModifyAckDeadlineRequest =
684689 ModifyAckDeadlineRequest .newBuilder ()
685690 .setSubscription (MOCK_SUBSCRIPTION_NAME )
686691 .addAllAckIds (mockAckIdsInRequest )
687692 .setAckDeadlineSeconds (MOCK_ACK_EXTENSION_DEFAULT_SECONDS )
688693 .build ();
689- verify (mockSubscriberStub .modifyAckDeadlineCallable (), times (1 ))
690- .futureCall (expectedModifyAckDeadlineRequest );
694+ verify (mockModifyAckDeadlineCallable , times (1 )).futureCall (expectedModifyAckDeadlineRequest );
691695 }
692696 }
693697
694698 @ Test
695699 public void testClientPinger_pingSent () {
696- BidiStreamingCallable <StreamingPullRequest , StreamingPullResponse > mockStreamingCallable =
697- mock (BidiStreamingCallable .class , withSettings ().withoutAnnotations ());
698700 ClientStream <StreamingPullRequest > mockClientStream =
699701 mock (ClientStream .class , withSettings ().withoutAnnotations ());
700- when (mockSubscriberStub .streamingPullCallable ()).thenReturn (mockStreamingCallable );
701702 when (mockStreamingCallable .splitCall (any (ResponseObserver .class ), any ()))
702703 .thenReturn (mockClientStream );
703704
@@ -737,11 +738,8 @@ public void testClientPinger_pingSent() {
737738
738739 @ Test
739740 public void testClientPinger_pingsNotSentWhenDisabled () {
740- BidiStreamingCallable <StreamingPullRequest , StreamingPullResponse > mockStreamingCallable =
741- mock (BidiStreamingCallable .class , withSettings ().withoutAnnotations ());
742741 ClientStream <StreamingPullRequest > mockClientStream =
743742 mock (ClientStream .class , withSettings ().withoutAnnotations ());
744- when (mockSubscriberStub .streamingPullCallable ()).thenReturn (mockStreamingCallable );
745743 when (mockStreamingCallable .splitCall (any (ResponseObserver .class ), any ()))
746744 .thenReturn (mockClientStream );
747745
@@ -763,13 +761,10 @@ public void testClientPinger_pingsNotSentWhenDisabled() {
763761
764762 @ Test
765763 public void testServerMonitor_timesOut () {
766- BidiStreamingCallable <StreamingPullRequest , StreamingPullResponse > mockStreamingCallable =
767- mock (BidiStreamingCallable .class , withSettings ().withoutAnnotations ());
768764 ClientStream <StreamingPullRequest > mockClientStream =
769765 mock (ClientStream .class , withSettings ().withoutAnnotations ());
770766 ArgumentCaptor <ResponseObserver <StreamingPullResponse >> observerCaptor =
771767 ArgumentCaptor .forClass (ResponseObserver .class );
772- when (mockSubscriberStub .streamingPullCallable ()).thenReturn (mockStreamingCallable );
773768 when (mockStreamingCallable .splitCall (observerCaptor .capture (), any ()))
774769 .thenReturn (mockClientStream );
775770
@@ -813,13 +808,10 @@ public void testServerMonitor_timesOut() {
813808
814809 @ Test
815810 public void testServerMonitor_doesNotTimeOutIfResponseReceived () {
816- BidiStreamingCallable <StreamingPullRequest , StreamingPullResponse > mockStreamingCallable =
817- mock (BidiStreamingCallable .class , withSettings ().withoutAnnotations ());
818811 ClientStream <StreamingPullRequest > mockClientStream =
819812 mock (ClientStream .class , withSettings ().withoutAnnotations ());
820813 ArgumentCaptor <ResponseObserver <StreamingPullResponse >> observerCaptor =
821814 ArgumentCaptor .forClass (ResponseObserver .class );
822- when (mockSubscriberStub .streamingPullCallable ()).thenReturn (mockStreamingCallable );
823815 when (mockStreamingCallable .splitCall (observerCaptor .capture (), any ()))
824816 .thenReturn (mockClientStream );
825817
0 commit comments