- 
                Notifications
    You must be signed in to change notification settings 
- Fork 14.8k
KAFKA-19345: Use ShareFetchUtils mock for DelayedShareFetchTest tests #20765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| assertNull(shareGroupMetrics.topicPartitionsFetchRatio(groupId)); | ||
|  | ||
| delayedShareFetch.lock().unlock(); | ||
| Mockito.verify(exceptionHandler, never()).accept(any(), any()); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exceptionHandler shouldn't be triggered in this test case.
The reason it throw error simply due to incorrect test mock up, 
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | 
replicaManager.getPartitionOrException(tp0.topicPartition()) defined in | mockTopicIdPartitionFetchBytes(replicaManager, tp0, hwmOffsetMetadata); | 
| exceptionHandler.accept( | 
| assertTrue(delayedShareFetch.lock().tryLock()); | ||
| delayedShareFetch.lock().unlock(); | ||
| Mockito.verify(exceptionHandler, times(1)).accept(any(), any()); | ||
| Mockito.verify(exceptionHandler, never()).accept(any(), any()); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
| UserQuotaTest#testThrottledRequest(String) with groupProtocol=classic is a known flaky test (KAFKA-8459). It’s unrelated to this PR and passes locally. | 
| Gentle ping , @AndrewJSchofield, @apoorvmittal10, @adixitconfluent  — sorry for the late implementation 🙏 | 
| @adixitconfluent Can you please review the PR. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @brandboat . I've left some comments.
|  | ||
| // We are testing the case when the share partition is getting fetched for the first time, so for the first time | ||
| // the fetchOffsetMetadata will return empty. Post the readFromLog call, the fetchOffsetMetadata will be | ||
| // the fetchOffsetMetadata will return empty. Post the isMinBytesSatisfied call, the fetchOffsetMetadata will be | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment that makes more sense is Post the first readFromLog call...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you’re right. I misread the ...populated for the share partition,... part. I initially thought the comment was explaining where we trigger fetchOffsetMetadata. Thanks for the correction!
| doAnswer(invocation -> buildLogReadResult(List.of(tp0))).when(replicaManager).readFromLog(any(), any(), any(ReplicaQuota.class), anyBoolean()); | ||
| BiConsumer<SharePartitionKey, Throwable> exceptionHandler = mockExceptionHandler(); | ||
|  | ||
| PartitionMaxBytesStrategy partitionMaxBytesStrategy = mockPartitionMaxBytes(Set.of(tp0)); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why this piece of code has been moved below - basically a cut and paste?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I moved the code snippet from mockTopicIdPartitionFetchBytes to here and did some refactoring.
| private void mockTopicIdPartitionFetchBytes(ReplicaManager replicaManager, TopicIdPartition topicIdPartition, LogOffsetMetadata hwmOffsetMetadata) { | ||
| LogOffsetSnapshot endOffsetSnapshot = new LogOffsetSnapshot(1, mock(LogOffsetMetadata.class), | ||
| hwmOffsetMetadata, mock(LogOffsetMetadata.class)); | ||
| Partition partition = mock(Partition.class); | ||
| when(partition.fetchOffsetSnapshot(any(), anyBoolean())).thenReturn(endOffsetSnapshot); | ||
| when(replicaManager.getPartitionOrException(topicIdPartition.topicPartition())).thenReturn(partition); | ||
| } | ||
|  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removal of this function doesn't make sense to me. I agree that the line when(replicaManager.getPartitionOrException(topicIdPartition.topicPartition())).thenReturn(partition); is problematic. We can pass the mock Partition object into this function. Example -
caller function -
Partition p0 = mock(Partition.class);
mockTopicIdPartitionFetchBytes(replicaManager, tp0, hwmOffsetMetadata, p0);
when(p0.isLeader()).thenReturn(true);
when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0);
function mockTopicIdPartitionFetchBytes -
private void mockTopicIdPartitionFetchBytes(LogOffsetMetadata hwmOffsetMetadata, Partition partition) {
        LogOffsetSnapshot endOffsetSnapshot = new LogOffsetSnapshot(1, mock(LogOffsetMetadata.class),
            hwmOffsetMetadata, mock(LogOffsetMetadata.class));
        when(partition.fetchOffsetSnapshot(any(), anyBoolean())).thenReturn(endOffsetSnapshot);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will add back the method.
|  | ||
| assertFalse(delayedShareFetch.isCompleted()); | ||
| try (MockedStatic<ShareFetchUtils> mockedShareFetchUtils = Mockito.mockStatic(ShareFetchUtils.class, Mockito.CALLS_REAL_METHODS)) { | ||
| mockedShareFetchUtils.when(() -> ShareFetchUtils.processFetchResponse(any(), any(), any(), any(), any())) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need mocking of processFetchResponse since it is not being called in this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you, remove
when(sp0.acquire(any(), anyInt(), anyInt(), anyLong(), any(), any())).thenReturn(
            createShareAcquiredRecords(new ShareFetchResponseData.AcquiredRecords().setFirstOffset(0).setLastOffset(3).setDeliveryCount((short) 1)));
in L215~215 should be enough.
| when(replicaManager.getPartitionOrException(tp0.topicPartition())).thenReturn(p0); | ||
| when(replicaManager.getPartitionOrException(tp1.topicPartition())).thenReturn(p1); | ||
|  | ||
| BiConsumer<SharePartitionKey, Throwable> exceptionHandler = mockExceptionHandler(); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For safety, should we be mocking ShareFetchUtils.processFetchResponse for the test testRemoteStorageFetchRequestCompletionOnFutureCompletionFailure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes @brandboat . The PR looks much better now. One small nit.
| LogOffsetSnapshot endOffsetSnapshot = new LogOffsetSnapshot(1, mock(LogOffsetMetadata.class), | ||
| hwmOffsetMetadata, mock(LogOffsetMetadata.class)); | ||
| Partition partition = mock(Partition.class); | ||
| hwmOffsetMetadata, mock(LogOffsetMetadata.class)); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Indentation (extra spaces)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in bcc30ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
This patch wraps ShareFetchUtils static method
processFetchResponsewith MockedStatic to improve test isolation and also fixes some
incorrect test results.
Reviewers: Abhinav Dixit [email protected], Andrew Schofield
[email protected]