tests: Remove hardcoded skips of certain number of messages from c8y mapper Rust unit tests#4170
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! 🚀 New features to boost your workflow:
|
Robot Results
|
didier-wenzek
left a comment
There was a problem hiding this comment.
This is really nice proposal! I encourage you to finalize it.
4b0a22e to
4a52e0d
Compare
4a52e0d to
c107a4d
Compare
c107a4d to
547c449
Compare
547c449 to
202b0a6
Compare
|
That commit 202b0a6 seems to have been included by accident. |
202b0a6 to
79ab205
Compare
79ab205 to
e4c4509
Compare
| use tedge_mqtt_ext::test_helpers::assert_received_contains_str; | ||
| use tedge_mqtt_ext::test_helpers::assert_received_includes_json; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // ignore all messages up to this point | ||
| mqtt.recv_short().await; | ||
| mqtt.messages.lock().unwrap().clear(); |
There was a problem hiding this comment.
This is not clear. What is done? Why?
This might be a good candidate for a new test helper
| // ignore all messages up to this point | |
| mqtt.recv_short().await; | |
| mqtt.messages.lock().unwrap().clear(); | |
| mqtt.ignore_messages_received_so_far(); |
There was a problem hiding this comment.
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.
didier-wenzek
left a comment
There was a problem hiding this comment.
Approved. Thank you. This is great improvement and refactoring c8y mapper tests will be less error prone.
albinsuresh
left a comment
There was a problem hiding this comment.
Great enhancement. It was nice to see the seamless transition in all existing tests.
| @@ -0,0 +1,109 @@ | |||
| use std::time::Duration; | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
Could we use a different parameter name than Box to disambiguate it from the std::boxed::Box type?
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>
f64978f to
e2bb68c
Compare
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>
1295632 to
07069fc
Compare
Proposed changes
Having hardcoded "skip
Nmessages" in tests makes it harder to change what messages are published, e.g. in #4064 where adding114message after registration makes a bunch of tests fail because now instead of skippingNmessages they need to skipN+1messages.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 aVecas 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
m1and then asserting messagem2, the writer of the test probably expects that they asserted thatm2happens afterm1. 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
Paste Link to the issue
Checklist
just prepare-devonce)just formatas mentioned in CODING_GUIDELINESjust checkas mentioned in CODING_GUIDELINESFurther comments