Skip to content

Commit b675143

Browse files
committed
test(pubsub): fix JSpecify compatibility issues on Java 8 for Mockito tests
Update Mockito mocks in pubsub tests to use `withSettings().withoutAnnotations()` to avoid `ArrayStoreException` on Java 8 due to JSpecify's `@NullMarked` annotation. Explicitly mock callables instead of relying on `RETURNS_DEEP_STUBS` which does not propagate the settings. TAG=agy CONV=de3ef09d-a26c-4190-b578-4c993f807bdc
1 parent ca02c36 commit b675143

2 files changed

Lines changed: 29 additions & 35 deletions

File tree

java-pubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/MessageDispatcherTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class MessageDispatcherTest {
9494
public void setUp() {
9595
systemExecutor = new FakeScheduledExecutorService();
9696
clock = new FakeClock();
97-
mockAckLatencyDistribution = mock(Distribution.class);
97+
mockAckLatencyDistribution = mock(Distribution.class, withSettings().withoutAnnotations());
9898

9999
mockAckProcessor = mock(MessageDispatcher.AckProcessor.class);
100100
messageContainsDeliveryAttempt = true;
@@ -710,8 +710,9 @@ private MessageDispatcher getMessageDispatcherFromBuilder(
710710
.setMinDurationPerAckExtensionDefaultUsed(true)
711711
.setMaxDurationPerAckExtension(Subscriber.DEFAULT_MAX_ACK_DEADLINE_EXTENSION)
712712
.setMaxDurationPerAckExtensionDefaultUsed(true)
713-
.setAckLatencyDistribution(mock(Distribution.class))
714-
.setFlowController(mock(FlowController.class))
713+
.setAckLatencyDistribution(
714+
mock(Distribution.class, withSettings().withoutAnnotations()))
715+
.setFlowController(mock(FlowController.class, withSettings().withoutAnnotations()))
715716
.setExecutor(executor)
716717
.setSubscriptionName(MOCK_SUBSCRIPTION_NAME)
717718
.setSystemExecutor(systemExecutor)
@@ -734,8 +735,9 @@ private MessageDispatcher getMessageDispatcherFromBuilder(
734735
.setMinDurationPerAckExtensionDefaultUsed(true)
735736
.setMaxDurationPerAckExtension(Subscriber.DEFAULT_MAX_ACK_DEADLINE_EXTENSION)
736737
.setMaxDurationPerAckExtensionDefaultUsed(true)
737-
.setAckLatencyDistribution(mock(Distribution.class))
738-
.setFlowController(mock(FlowController.class))
738+
.setAckLatencyDistribution(
739+
mock(Distribution.class, withSettings().withoutAnnotations()))
740+
.setFlowController(mock(FlowController.class, withSettings().withoutAnnotations()))
739741
.setExecutor(MoreExecutors.newDirectExecutorService())
740742
.setSubscriptionName(MOCK_SUBSCRIPTION_NAME)
741743
.setSystemExecutor(systemExecutor)

java-pubsub/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/StreamingSubscriberConnectionTest.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import com.google.api.gax.rpc.ResponseObserver;
3434
import com.google.api.gax.rpc.StatusCode;
3535
import com.google.api.gax.rpc.StreamController;
36+
import com.google.api.gax.rpc.UnaryCallable;
3637
import com.google.cloud.pubsub.v1.stub.SubscriberStub;
3738
import com.google.common.collect.Lists;
3839
import com.google.protobuf.Any;
40+
import com.google.protobuf.Empty;
3941
import com.google.pubsub.v1.AcknowledgeRequest;
4042
import com.google.pubsub.v1.ModifyAckDeadlineRequest;
4143
import 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

Comments
 (0)