86
86
import static org .mockito .ArgumentMatchers .anyString ;
87
87
import static org .mockito .Mockito .doAnswer ;
88
88
import static org .mockito .Mockito .doReturn ;
89
+ import static org .mockito .Mockito .doThrow ;
89
90
import static org .mockito .Mockito .mock ;
90
91
import static org .mockito .Mockito .spy ;
91
92
import static org .mockito .Mockito .times ;
@@ -725,6 +726,7 @@ public void testExceptionInMinBytesCalculation() {
725
726
Mockito .verify (replicaManager , times (1 )).readFromLog (
726
727
any (), any (), any (ReplicaQuota .class ), anyBoolean ());
727
728
Mockito .verify (delayedShareFetch , times (1 )).releasePartitionLocks (any ());
729
+ Mockito .verify (sp0 , times (1 )).releaseFetchLock ();
728
730
729
731
// Force complete the request as it's still pending. Return false from the share partition lock acquire.
730
732
when (sp0 .maybeAcquireFetchLock ()).thenReturn (false );
@@ -747,6 +749,44 @@ public void testExceptionInMinBytesCalculation() {
747
749
Mockito .verify (exceptionHandler , times (1 )).accept (any (), any ());
748
750
}
749
751
752
+ @ Test
753
+ public void testTryCompleteLocksReleasedOnCompleteException () {
754
+ ReplicaManager replicaManager = mock (ReplicaManager .class );
755
+ TopicIdPartition tp0 = new TopicIdPartition (Uuid .randomUuid (), new TopicPartition ("foo" , 0 ));
756
+ LinkedHashMap <TopicIdPartition , Integer > partitionMaxBytes = orderedMap (PARTITION_MAX_BYTES , tp0 );
757
+
758
+ SharePartition sp0 = mock (SharePartition .class );
759
+ when (sp0 .maybeAcquireFetchLock ()).thenReturn (true );
760
+ when (sp0 .canAcquireRecords ()).thenReturn (true );
761
+ when (sp0 .fetchOffsetMetadata (anyLong ())).thenReturn (Optional .of (new LogOffsetMetadata (0 , 1 , 0 )));
762
+
763
+ LinkedHashMap <TopicIdPartition , SharePartition > sharePartitions = new LinkedHashMap <>();
764
+ sharePartitions .put (tp0 , sp0 );
765
+
766
+ ShareFetch shareFetch = new ShareFetch (FETCH_PARAMS , "grp" , Uuid .randomUuid ().toString (),
767
+ new CompletableFuture <>(), partitionMaxBytes , BATCH_SIZE , MAX_FETCH_RECORDS ,
768
+ BROKER_TOPIC_STATS );
769
+
770
+ doAnswer (invocation -> buildLogReadResult (Collections .singleton (tp0 ))).when (replicaManager ).readFromLog (any (), any (), any (ReplicaQuota .class ), anyBoolean ());
771
+ mockTopicIdPartitionToReturnDataEqualToMinBytes (replicaManager , tp0 , 1 );
772
+
773
+ PartitionMaxBytesStrategy partitionMaxBytesStrategy = mockPartitionMaxBytes (Collections .singleton (tp0 ));
774
+ DelayedShareFetch delayedShareFetch = spy (DelayedShareFetchBuilder .builder ()
775
+ .withShareFetchData (shareFetch )
776
+ .withSharePartitions (sharePartitions )
777
+ .withReplicaManager (replicaManager )
778
+ .withPartitionMaxBytesStrategy (partitionMaxBytesStrategy )
779
+ .build ());
780
+ assertFalse (delayedShareFetch .isCompleted ());
781
+ // Throw exception for onComplete.
782
+ doThrow (new RuntimeException ()).when (delayedShareFetch ).onComplete ();
783
+ // Try to complete the request.
784
+ assertFalse (delayedShareFetch .tryComplete ());
785
+
786
+ Mockito .verify (delayedShareFetch , times (1 )).releasePartitionLocks (any ());
787
+ Mockito .verify (sp0 , times (1 )).releaseFetchLock ();
788
+ }
789
+
750
790
@ Test
751
791
public void testLocksReleasedForCompletedFetch () {
752
792
String groupId = "grp" ;
0 commit comments