Skip to content

KAFKA-20942: Refactor empty catch blocks in clients module tests to use assertThrows - #23187

Open
kapilhadoltikar wants to merge 1 commit into
apache:trunkfrom
kapilhadoltikar:KAFKA-20942-Refactor-empty-catch-blocks-in-clients-module-tests-to-use-assertThrows
Open

KAFKA-20942: Refactor empty catch blocks in clients module tests to use assertThrows#23187
kapilhadoltikar wants to merge 1 commit into
apache:trunkfrom
kapilhadoltikar:KAFKA-20942-Refactor-empty-catch-blocks-in-clients-module-tests-to-use-assertThrows

Conversation

@kapilhadoltikar

@kapilhadoltikar kapilhadoltikar commented Aug 18, 2026

Copy link
Copy Markdown
  • Empty catch blocks can swallow unexpected errors, bugs, or exceptions
    during test execution.

  • Refactored empty catch blocks in the clients module tests to use JUnit
    5's assertThrows for strict and safe exception handling.

Changes

  • Refactored empty catch blocks to use JUnit 5's assertThrows across
    several test areas in the clients module:

    • Admin & Consumer: DescribeUserScramCredentialsResultTest,
      consumer internals AbstractCoordinatorTest, ConsumerCoordinatorTest,
      FetcherTest, FetchRequestManagerTest, and KafkaConsumerTest.

    • Producer & Internals: BufferPoolTest, ProducerBatchTest,
      ProducerMetadataTest, TransactionManagerTest, and
      KafkaProducerTest.

    • Common Config, Metrics & Network: ConfigDefTest, TopicTest,
      ApiMessageTypeTest, KafkaMbeanTest, SensorTest, and
      SelectorTest.

    • Security & Utils: FileRecordsTest, Sasl, OAuthbearer, and
      ScramMessagesTest, SslFactoryTest, JaasContextTest,
      ImplicitLinkedHashCollectionTest, and UtilsTest.

Testing

  • Ran ./gradlew :clients:test locally and verified all tests passed.

  • All CI checks passed successfully.

Reviewers: Ken Huang s7133700@gmail.com

@github-actions github-actions Bot added triage PRs from the community producer consumer tests Test fixes (including flaky tests) clients labels Aug 18, 2026
@github-actions

Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@kapilhadoltikar

Copy link
Copy Markdown
Author

Hi everyone!
Whenever someone has a spare cycle, could you please take a look?
Thank you!

@kapilhadoltikar
kapilhadoltikar force-pushed the KAFKA-20942-Refactor-empty-catch-blocks-in-clients-module-tests-to-use-assertThrows branch from aadf7b9 to 9abbf1b Compare August 27, 2026 07:45
@github-actions

Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@kapilhadoltikar

Copy link
Copy Markdown
Author

Hi! @hjtiun852
I have noticed some files from your PR #23277 overlaps with some of the test files in my earlier PR #23187. Since I submitted mine a couple of weeks ago, how would you like to handle the overlap? If you prefer to keep those files in your PR, I am happy to drop them from mine so yours can go through smoothly.
Also @m1a2st I'd really appreciate a quick review on the remaining files whenever you have a moment!

@github-actions

Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@kapilhadoltikar
kapilhadoltikar force-pushed the KAFKA-20942-Refactor-empty-catch-blocks-in-clients-module-tests-to-use-assertThrows branch from 9abbf1b to 1ba3136 Compare August 31, 2026 19:32
@kapilhadoltikar

Copy link
Copy Markdown
Author

Hi @chia7712, @m1a2st
This PR is ready for review. all merge conflicts are resolved.
Whenever you have a moment , I'd appreciate your feedback!

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

@m1a2st m1a2st left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you also take a look at the following tests?

  • OffsetFetcherTest
  • ShareConsumeRequestManagerTest
  • TopicMetadataFetcherTest
  • RecordAccumulatorTest
  • SenderTest
  • SaslChannelBuilderTest
  • ProtocolSerializationTest
  • RawTaggedFieldWriterTest
  • AbstractLegacyRecordBatchTest
  • LeaveGroupRequestTest

Comment on lines +256 to +246
try {
pool.allocate(2, maxBlockTimeMs);
fail("The buffer allocated more memory than its maximum value 2");
} catch (BufferExhaustedException e) {
// this is good
} catch (InterruptedException e) {
// this can be neglected
}
assertThrows(BufferExhaustedException.class, () -> pool.allocate(2, maxBlockTimeMs));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change drops the InterruptedException catch branch that was here before. The original code accepted both BufferExhaustedException and InterruptedException as valid outcomes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

your right! BufferExhaustedException and InterruptedException are both included in latest commit.

}

@Test
@SuppressWarnings("deprecation")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why we need this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for pointing out! Removed.

…se assertThrows

* Empty catch block can swallow unexpected errors, bugs or exceptions during test execution.

* Refactored 35 tests from clients module to use JUnit 5's `assertThrows` for strict and safe exception handling.

Signed-off-by: Kapil Hadoltikar <hadoltikar.kapil@gmail.com>
@kapilhadoltikar
kapilhadoltikar force-pushed the KAFKA-20942-Refactor-empty-catch-blocks-in-clients-module-tests-to-use-assertThrows branch from 1ba3136 to 8f49941 Compare September 2, 2026 12:54
@kapilhadoltikar

Copy link
Copy Markdown
Author

Thanks for the thorough review!
As per your suggestion, I've refactored all the legacy try-catch-fail blocks across those 10 additional test files to use assertThrows for cleaner and more idiomatic assertions.

@github-actions github-actions Bot removed needs-attention triage PRs from the community labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants