Skip to content
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

MINOR: Various cleanups in clients tests #15877

Merged
merged 3 commits into from
May 14, 2024

Conversation

mimaison
Copy link
Member

@mimaison mimaison commented May 6, 2024

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

Copy link
Contributor

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mimaison nice cleanup!

@@ -78,34 +78,30 @@ public static void main(String[] args) throws Exception {
final Time time = Time.SYSTEM;
final AtomicBoolean done = new AtomicBoolean(false);
final Object lock = new Object();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just delete this old class ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether anyone uses this. I still think we should check before deleting it.

@@ -106,10 +103,9 @@ public void setup() {
commitRequestManager = testBuilder.commitRequestManager.orElseThrow(IllegalStateException::new);
offsetsRequestManager = testBuilder.offsetsRequestManager;
coordinatorRequestManager = testBuilder.coordinatorRequestManager.orElseThrow(IllegalStateException::new);
heartbeatRequestManager = testBuilder.heartbeatRequestManager.orElseThrow(IllegalStateException::new);
memberhipsManager = testBuilder.membershipManager.orElseThrow(IllegalStateException::new);
HeartbeatRequestManager heartbeatRequestManager = testBuilder.heartbeatRequestManager.orElseThrow(IllegalStateException::new);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variables heartbeatRequestManager and membershipManager are unused. Are they used to test the existence of heartbeatRequestManager and membershipManager? If so, could we rewrite them by assertTrue? For example: assertTrue(testBuilder.heartbeatRequestManager.isPresent());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they are used to test the existence of the managers here, I would say they were just left unused so we should remove them. Managers are retrieved in this way in many other tests (ex HeartbeatRequestManagerTest), but only when needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, removing all those unused might help us remove the suppression ClassDataAbstractionCoupling, worth checking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, these can be removed and as @lianetm pointed this allows getting rid of the suppression. Thanks

@@ -1323,7 +1323,7 @@ public void testConcurrentUpdateAndFetchForSnapshotAndCluster() throws Interrupt
} else { // Thread to read metadata snapshot, once its updated
try {
if (!atleastMetadataUpdatedOnceLatch.await(5, TimeUnit.MINUTES)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using assertDoesNotThrow? For example:

                    assertTrue(assertDoesNotThrow(() -> atleastMetadataUpdatedOnceLatch.await(5, TimeUnit.MINUTES)),
                            "Test had to wait more than 5 minutes, something went wrong.");

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unusual to chain assert calls but I'll accept it

@@ -1335,7 +1335,7 @@ public void testConcurrentUpdateAndFetchForSnapshotAndCluster() throws Interrupt
});
}
if (!allThreadsDoneLatch.await(5, TimeUnit.MINUTES)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using assertTrue?

assertTrue(allThreadsDoneLatch.await(5, TimeUnit.MINUTES), "Test had to wait more than 5 minutes, something went wrong.");

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's even better!

Iterator<Header> headerIterator = headers.iterator();
while (headerIterator.hasNext()) {
headerIterator.next();
for (Header ignore : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using toArray? for example: headers.toArray().length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion

Comment on lines 1844 to 1848
consumer.subscribe(Collections.singleton(topic));
fail("Expected an InvalidGroupIdException");
} catch (InvalidGroupIdException e) {
// OK, expected
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're cleaning up this, maybe just assertThrows? (same for the following 3 try)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was able to simplify a few of these. Thanks!

@@ -3232,7 +3231,7 @@ public void testProducerBatchRetriesWhenPartitionLeaderChanges() throws Exceptio
assertTrue(client.hasInFlightRequests());
client.respond(produceResponse(tp0, -1, Errors.NOT_LEADER_OR_FOLLOWER, 0));
sender.runOnce(); // receive produce response, batch scheduled for retry
assertTrue(!futureIsProduced.isDone(), "Produce request is yet not done.");
assertFalse(futureIsProduced.isDone(), "Produce request is yet not done.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to me this error message is inverted too right? it's stating the desired state, not the failure: if the assert fails, it's because the produce request is done when it shouldn't (not because "is yet not done"). Maybe just "Produce request shouldn't complete yet"...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I updated the messages

@@ -3253,13 +3252,13 @@ public void testProducerBatchRetriesWhenPartitionLeaderChanges() throws Exceptio
assertTrue(client.hasInFlightRequests());
client.respond(produceResponse(tp0, -1, Errors.NOT_LEADER_OR_FOLLOWER, 0));
sender.runOnce(); // receive produce response, schedule batch for retry.
assertTrue(!futureIsProduced.isDone(), "Produce request is yet not done.");
assertFalse(futureIsProduced.isDone(), "Produce request is yet not done.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -3492,9 +3491,9 @@ private void verifyErrorMessage(ProduceResponse response, String expectedMessage
assertEquals(expectedMessage, e1.getCause().getMessage());
}

class AssertEndTxnRequestMatcher implements MockClient.RequestMatcher {
static class AssertEndTxnRequestMatcher implements MockClient.RequestMatcher {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems could be private too

Comment on lines 117 to 121
for (int i = 0; i < testElements.length; i++) {
assertTrue(multiSet.add(testElements[i]));
for (TestElement testElement : testElements) {
assertTrue(multiSet.add(testElement));
}
for (int i = 0; i < testElements.length; i++) {
assertFalse(multiSet.add(testElements[i]));
for (TestElement testElement : testElements) {
assertFalse(multiSet.add(testElement));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be simplified to a single loop with the 2 asserts in it? we would achieve the same, validate an elem can be added only if new, for all elems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we can merge both loops

} catch (Throwable e) {
assertEquals(partitionRevoked + singleTopicPartition, e.getCause().getCause().getMessage());
}
t = assertThrows(Throwable.class, () -> consumer.close(Duration.ofMillis(0)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced by this PR, but here we could be more specific and assertThrows(KafkaException.class, ...). Regardless of the type of exception thrown in the rebalance callbacks, a KafkaException is what's always propagated (legacy and new consumer).

Btw, reviewing this PR I noticed that this KafkaConsumerTest has lots of tests not enabled for the new consumer (with lots of TODOs about it). I created KAFKA-16737 to make sure we address it, enable the tests that apply and clean up the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I pushed an update.
Thanks for raising KAFKA-16737

Copy link
Contributor

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mimaison
Copy link
Member Author

Thanks for the reviews! Test failures are not related, merging to trunk

@mimaison mimaison merged commit 0587a9a into apache:trunk May 14, 2024
1 check failed
@mimaison mimaison deleted the cleanups-clients-test branch May 14, 2024 08:19
@chia7712 chia7712 mentioned this pull request May 23, 2024
TaiJuWu pushed a commit to TaiJuWu/kafka that referenced this pull request Jun 8, 2024
Reviewers: Chia-Ping Tsai <chia7712@gmail.com>,  Lianet Magrans <lianetmr@gmail.com>
gongxuanzhang pushed a commit to gongxuanzhang/kafka that referenced this pull request Jun 12, 2024
Reviewers: Chia-Ping Tsai <chia7712@gmail.com>,  Lianet Magrans <lianetmr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants