Skip to content

tests: Remove hardcoded skips of certain number of messages from c8y mapper Rust unit tests#4170

Merged
Bravo555 merged 8 commits into
thin-edge:mainfrom
Bravo555:refactor/rust-tests-change-asserts
Jun 3, 2026
Merged

tests: Remove hardcoded skips of certain number of messages from c8y mapper Rust unit tests#4170
Bravo555 merged 8 commits into
thin-edge:mainfrom
Bravo555:refactor/rust-tests-change-asserts

Conversation

@Bravo555

@Bravo555 Bravo555 commented May 13, 2026

Copy link
Copy Markdown
Member

Proposed changes

Having hardcoded "skip N messages" in tests makes it harder to change what messages are published, e.g. in #4064 where adding 114 message after registration makes a bunch of tests fail because now instead of skipping N messages they need to skip N+1 messages.

This skipping was necessary as long as the tests were operating on regular MessageReceivers, which only provide an interface to receive a new message and have no way to store already received messages. I'd argue that while it's a useful foundation for expressing different types of receivers which are used in production code, it's probably a bad abstraction for tests where we can easily store incoming messages to a Vec as they come and then run asserts not on one message that we just received, but on all messages received.

Thus, the PR provides a new MockMqttBox (alternative proposed names: TestMqttBox/BufferedMqttBox) which wraps any receiver and stores its received messages. The assertions then run on all captures messages.

The assertions then can be made more flexible, but currently they still need to be adjusted to some of the intuitive usage patterns. For example, after asserting message m1 and then asserting message m2, the writer of the test probably expects that they asserted that m2 happens after m1. So for simple and common asserts we could update a cursor pointing to the least recently asserted message and only assert additional messages after that.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue


Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s. You can activate automatic signing by running just prepare-dev once)
  • I ran just format as mentioned in CODING_GUIDELINES
  • I used just check as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@Bravo555
Bravo555 temporarily deployed to Test Pull Request May 13, 2026 09:03 — with GitHub Actions Inactive
@reubenmiller reubenmiller added the theme:testing Theme: Testing label May 13, 2026
@codecov

codecov Bot commented May 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.09524% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...mqtt_ext/src/test_helpers/test_mqtt_box/asserts.rs 85.71% 7 Missing ⚠️
...dge_mqtt_ext/src/test_helpers/test_mqtt_box/mod.rs 91.66% 3 Missing and 4 partials ⚠️
...ates/extensions/tedge_mqtt_ext/src/test_helpers.rs 81.81% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Bravo555 Bravo555 added the refactoring Developer value label May 13, 2026
@github-actions

github-actions Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
934 0 7 934 100 2h50m55.305319999s

@didier-wenzek didier-wenzek 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.

This is really nice proposal! I encourage you to finalize it.

@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 4b0a22e to 4a52e0d Compare May 28, 2026 08:21
@Bravo555
Bravo555 had a problem deploying to Test Pull Request May 28, 2026 08:21 — with GitHub Actions Failure
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 4a52e0d to c107a4d Compare May 28, 2026 08:22
@Bravo555
Bravo555 had a problem deploying to Test Pull Request May 28, 2026 08:22 — with GitHub Actions Failure
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from c107a4d to 547c449 Compare May 28, 2026 08:23
@Bravo555
Bravo555 had a problem deploying to Test Pull Request May 28, 2026 08:23 — with GitHub Actions Failure
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 547c449 to 202b0a6 Compare May 28, 2026 13:22
@Bravo555
Bravo555 temporarily deployed to Test Pull Request May 28, 2026 13:22 — with GitHub Actions Inactive
@didier-wenzek

Copy link
Copy Markdown
Contributor

That commit 202b0a6 seems to have been included by accident.

@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 202b0a6 to 79ab205 Compare May 29, 2026 13:03
@Bravo555
Bravo555 temporarily deployed to Test Pull Request May 29, 2026 13:03 — with GitHub Actions Inactive
@Bravo555 Bravo555 changed the title DRAFT: Remove hardcoded skips of certain number of messages from c8y mapper Rust unit tests tests: Remove hardcoded skips of certain number of messages from c8y mapper Rust unit tests May 29, 2026
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 79ab205 to e4c4509 Compare May 29, 2026 13:04
@Bravo555
Bravo555 temporarily deployed to Test Pull Request May 29, 2026 13:05 — with GitHub Actions Inactive
@Bravo555
Bravo555 marked this pull request as ready for review May 29, 2026 13:07
Comment thread crates/extensions/c8y_mapper_ext/src/tests/mock_mqtt_box.rs Outdated
Comment on lines -149 to -150
use tedge_mqtt_ext::test_helpers::assert_received_contains_str;
use tedge_mqtt_ext::test_helpers::assert_received_includes_json;

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.

Have you considered to fully deprecate these test helpers - or, better, to move your improved implementation there? What are the blockers, if any?

The former tedge_mqtt_ext::test_helpers are still used to test the agent and c8y_mapper_ext::availability. Since the purpose is the same it would be good to have a single abstraction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Have you considered to fully deprecate these test helpers - or, better, to move your improved implementation there?

Indeed the new assertions could probably be moved in and replace the current ones in tedge_mqtt_ext, but there's still quite a few usages of the old asserts in the agent tests and I'd like to avoid having this PR becoming too big and replacing everything in one go. Asserts in the agent tests could be changed in a follow-up PR.

The former tedge_mqtt_ext::test_helpers are still used to test the agent and c8y_mapper_ext::availability

Indeed availability module still uses old asserts and skips messages by number. Initially I thought to leave it as is, as the availability module tests don't test the entire mapper but only the availability actor, so it's possible that the availability messages won't change that much and skipped messages are sent by the same actor thus they're not unrelated noise from other actors, so skipping them is meaningful, so i though that maybe the current asserts can stay, but you changed my mind and I'll change those as well because:

  • as you say, it's better not to duplicate abstractions, but build one good abstraction and use it everywhere
  • the skips often have the form mqtt.skip(1) // 117, i.e. we're expecting the skipped message to be 117 but we're not checking that, which we probably should

So I'll replace these skips with the proper asserts or ignore method from f64978f as required.

Comment on lines +1246 to +1248
// ignore all messages up to this point
mqtt.recv_short().await;
mqtt.messages.lock().unwrap().clear();

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 is not clear. What is done? Why?

This might be a good candidate for a new test helper

Suggested change
// ignore all messages up to this point
mqtt.recv_short().await;
mqtt.messages.lock().unwrap().clear();
mqtt.ignore_messages_received_so_far();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed in f64978f

Good catch, indeed that was something I replaced too quickly and didn't realize the test was doing too much - instead of ignoring all messages, we should only ignore any received 114 messages for the child device topic to not mistake it for a 114 message that should be produced when sending command metadata message later on in the test.

@Bravo555
Bravo555 had a problem deploying to Test Pull Request June 3, 2026 10:56 — with GitHub Actions Failure

@didier-wenzek didier-wenzek 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.

Approved. Thank you. This is great improvement and refactoring c8y mapper tests will be less error prone.

@didier-wenzek didier-wenzek added the skip-release-notes Don't include the ticket in the auto generated release notes label Jun 3, 2026

@albinsuresh albinsuresh 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.

Great enhancement. It was nice to see the seamless transition in all existing tests.

@@ -0,0 +1,109 @@
use std::time::Duration;

@albinsuresh albinsuresh Jun 3, 2026

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 not move these test helpers to tedge_mqtt_ext module itself as suggested here, so that these enhancements are available to agent and other components as well?

I wouldn't remove the existing helpers, but just add these to a separate module.

@Bravo555 Bravo555 Jun 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved into tedge_mqtt_ext::test_helpers::TestMqttBox in 07069fc

I've been putting it off for some reason, but indeed it makes sense and would have needed to be done sooner or later.

/// something else asserts them later). This is most easily done by gathering
/// all received messages to a buffer and then choosing to drop selected
/// messages only once they're asserted.
pub struct MockMqttBox<Box> {

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 we use a different parameter name than Box to disambiguate it from the std::boxed::Box type?

@Bravo555 Bravo555 Jun 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed in 07069fc

Bravo555 added 6 commits June 3, 2026 16:00
These tests were failing if additional 114 message is published during
entity registration. These tests ideally should only care about
registration messages and ignore other messages.

Thus new asserts were implemented which allow other messages to appear
in between the asserted messages.

Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from f64978f to e2bb68c Compare June 3, 2026 16:00
@Bravo555
Bravo555 temporarily deployed to Test Pull Request June 3, 2026 16:00 — with GitHub Actions Inactive
@Bravo555
Bravo555 temporarily deployed to Test Pull Request June 3, 2026 16:46 — with GitHub Actions Inactive
Bravo555 added 2 commits June 3, 2026 16:50
Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
There's no mocking of any sort happening here, but it's an MQTT message
box meant to be used in tests, so the name was changed.

It was moved to tedge_mqtt_ext since it would have to be done anyway in
a follow-up PR when chaning agent tests to use it.

Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
@Bravo555
Bravo555 force-pushed the refactor/rust-tests-change-asserts branch from 1295632 to 07069fc Compare June 3, 2026 16:50
@Bravo555
Bravo555 temporarily deployed to Test Pull Request June 3, 2026 16:51 — with GitHub Actions Inactive
@Bravo555
Bravo555 added this pull request to the merge queue Jun 3, 2026
Merged via the queue into thin-edge:main with commit ac17b12 Jun 3, 2026
34 checks passed
@Bravo555
Bravo555 deleted the refactor/rust-tests-change-asserts branch June 3, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring Developer value skip-release-notes Don't include the ticket in the auto generated release notes theme:testing Theme: Testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants