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 tests using Channels #1585

Merged
merged 7 commits into from
Dec 7, 2023
Merged

Improve tests using Channels #1585

merged 7 commits into from
Dec 7, 2023

Conversation

cmelchior
Copy link
Contributor

This PR streamlines how we are using Channels in tests by introducing a new TestChannel factory method which will ensure we construct the same channel across all tests.

This channel has a few changes compared to the previous one:

  • It has an unlimited buffer, which means that send should never suspend and we should never accidentally overwrite an event.
  • When the channel is canceled, it will check if some elements have not been delivered and throw an assertion if that is true (as it indicates the writer of the test doesn't fully understand the event flow)

I also refactored Channel.receiveOrFail(), onReceive in channels does not consume elements and we suspect it might be the cause of some of our flaky tests (but hasn't been 100% confirmed).

All uses of trySend() in tests has also been replaced by send() since trySend() might fail silently causing us to miss an error.

Christian Melchior added 2 commits November 28, 2023 15:55
…sert if containing elements when the channel is closed.

Add an unlimited buffer to our test channel, so we do not accidentially overwrite events.
@cmelchior cmelchior marked this pull request as ready for review December 6, 2023 12:23
@cmelchior
Copy link
Contributor Author

This PR is now ready for review

Copy link
Collaborator

@nhachicha nhachicha left a comment

Choose a reason for hiding this comment

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

Nice 👍

val job = async {
realm.query<ObjectIdPk>("_id = $0", BsonObjectId(oid)).first()
.asFlow().collect {
if (it.obj != null) {
channel.trySend(it.obj!!)
channel.send(it.obj!!)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use trySendOrFail?

Co-authored-by: Nabil Hachicha <nabil.hachicha@mongodb.com>
@cmelchior cmelchior merged commit 0834a9c into main Dec 7, 2023
1 of 2 checks passed
@cmelchior cmelchior deleted the cm/better-channel-tests branch December 7, 2023 14:25
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants