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

Improve Test Reliability by Replacing ZIO.sleep with Synchronization Primitives in ConsumerSpec #1401

Merged
merged 3 commits into from
Nov 23, 2024

Conversation

AdrielC
Copy link
Contributor

@AdrielC AdrielC commented Nov 22, 2024

Follow up on #1389

Description:

This pull request improves the reliability of tests in ConsumerSpec.scala by replacing the use of ZIO.sleep with synchronization primitives such as Promise and Ref. The previous implementation relied on arbitrary sleep durations to wait for certain conditions, which could lead to non-deterministic behavior and flakiness in the tests.

  1. Test: “handle rebalancing by completing topic-partition streams”
  • Issue: The test used ZIO.sleep to wait before starting the second consumer, which could cause timing-related flakiness.
  • Solution: Introduced a Ref[Set[Int]] to track the partitions assigned to the first consumer and a Promise[Nothing, Unit] to signal when the consumer has been assigned at least half of the partitions, replacing the ZIO.sleep.
  • Result: The second consumer now starts only after the first consumer has been assigned the required number of partitions.
  1. Test: “produce diagnostic events when rebalancing”
  • Issue: Similar to the first test, this one used ZIO.sleep to delay the start of the second consumer, leading to potential flakiness.
  • Solution: Applied the same approach as in the previous test by using a Ref[Set[Int]] and a Promise[Nothing, Unit] to synchronize the start of the second consumer.
  • Result: The second consumer starts deterministically after the first consumer is ready.
  1. Test: “it’s possible to start a new consumption session from a Consumer that had a consumption session stopped previously”
  • Issue: The test used ZIO.sleep to wait before stopping the consumer.
  • Solution: Replaced the sleep with a Ref[Int] to count the number of messages consumed and a Promise[Nothing, Unit] to signal when to stop consumption.
  • Result: The consumer is now stopped after consuming a specified number of messages.

Copy link
Collaborator

@erikvanoosten erikvanoosten left a comment

Choose a reason for hiding this comment

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

Awesome stuff. Thanks @AdrielC .
Let's wait until #1400 is merged, and then merge this PR.

@erikvanoosten erikvanoosten merged commit e23d64a into zio:master Nov 23, 2024
13 checks passed
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.

2 participants