Skip to content

Commit 62a8f4d

Browse files
committed
Check the entity store after a sequence of registrations
Signed-off-by: Didier Wenzek <[email protected]>
1 parent 7b4a96a commit 62a8f4d

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

crates/core/tedge_agent/proptest-regressions/entity_manager/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
# It is recommended to check this file in to source control so that
66
# everyone who runs the test benefits from these saved cases.
77
cc eb284ae3160cf4a071a3ca80175b2c72680b7c8ba19b8d577805ef1317eca2db # shrinks to registrations = [(MQTT, AddService { topic: "ac", props: [] }), (MQTT, AddService { topic: "ac", props: [] }), (MQTT, AddDevice { topic: "ac", props: [] }), (HTTP, AddService { topic: "ac", props: [("x", "4")] }), (HTTP, AddDevice { topic: "a", props: [] })]
8+
cc 6e3a5e19daf6c3a4732a0504822f2164c5d0f49ed5652e1e67c779a23aedbe77 # shrinks to registrations = [(HTTP, AddService { topic: "a", props: [] }), (MQTT, AddDevice { topic: "abc", props: [("z", "0")] }), (MQTT, AddDevice { topic: "abc", props: [("z", "8")] }), (HTTP, AddService { topic: "aa", props: [] }), (MQTT, AddDevice { topic: "abc", props: [("y", "3")] }), (MQTT, AddDevice { topic: "ab", props: [] }), (HTTP, AddService { topic: "a", props: [] }), (HTTP, AddDevice { topic: "abc", props: [("y", "7")] }), (MQTT, AddDevice { topic: "a", props: [] })]

crates/core/tedge_agent/src/entity_manager/server.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ impl EntityStoreServer {
5757
entity_auto_register,
5858
}
5959
}
60+
61+
#[cfg(test)]
62+
pub fn entity_topic_ids(&self) -> impl Iterator<Item = &EntityTopicId> {
63+
self.entity_store.entity_topic_ids()
64+
}
65+
66+
#[cfg(test)]
67+
pub fn get(&self, entity_topic_id: &EntityTopicId) -> Option<&EntityMetadata> {
68+
self.entity_store.get(entity_topic_id)
69+
}
6070
}
6171

6272
#[async_trait]

crates/core/tedge_agent/src/entity_manager/tests.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ async fn new_entity_store() {
1515
}
1616

1717
proptest! {
18+
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(1000))]
1819
#[test]
19-
fn it_works_for_any_registration_order(registrations in model::walk(6)) {
20+
fn it_works_for_any_registration_order(registrations in model::walk(10)) {
2021
tokio::runtime::Builder::new_current_thread()
2122
.enable_all()
2223
.build()
@@ -39,6 +40,21 @@ proptest! {
3940
};
4041
assert_eq!(actual_updates, expected_updates);
4142
}
43+
44+
let mut registered_topics : Vec<_> = entity_store.entity_topic_ids().collect();
45+
registered_topics.sort_by(|a,b| a.as_ref().cmp(b.as_ref()));
46+
47+
let mut expected_topics : Vec<_> = state.entity_topic_ids().collect();
48+
expected_topics.sort_by(|a,b| a.as_ref().cmp(b.as_ref()));
49+
50+
assert_eq!(registered_topics, expected_topics);
51+
52+
for topic in registered_topics {
53+
let registered = entity_store.get(topic).unwrap();
54+
let (entity_type, parent, _) = state.get(topic).unwrap();
55+
assert_eq!(&registered.r#type, entity_type);
56+
assert_eq!(registered.parent.as_ref(), parent.as_ref());
57+
}
4258
})
4359
}
4460
}
@@ -277,6 +293,23 @@ mod model {
277293
state
278294
}
279295

296+
pub fn entity_topic_ids(&self) -> impl Iterator<Item = &EntityTopicId> {
297+
self.entities
298+
.keys()
299+
.filter(|topic| self.is_registered(topic))
300+
}
301+
302+
pub fn get(
303+
&self,
304+
topic: &EntityTopicId,
305+
) -> Option<&(EntityType, Option<EntityTopicId>, PropMap)> {
306+
self.entities.get(topic)
307+
}
308+
309+
pub fn is_registered(&self, topic: &EntityTopicId) -> bool {
310+
self.registered.contains(topic)
311+
}
312+
280313
pub fn apply(&mut self, protocol: Protocol, action: Action) -> HashSet<EntityTopicId> {
281314
let topic = action.topic_id();
282315

crates/core/tedge_api/src/entity_store.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ impl EntityStore {
229229
}
230230
}
231231

232+
/// Iterates over the entity topic ids
233+
pub fn entity_topic_ids(&self) -> impl Iterator<Item = &EntityTopicId> {
234+
self.entities.entity_topic_ids()
235+
}
236+
232237
/// Returns information about an entity under a given MQTT entity topic identifier.
233238
pub fn get(&self, entity_topic_id: &EntityTopicId) -> Option<&EntityMetadata> {
234239
self.entities.get(entity_topic_id)
@@ -564,6 +569,11 @@ impl EntityTree {
564569
self.entities.contains_key(topic_id)
565570
}
566571

572+
/// Iterate over the entity topic ids
573+
pub fn entity_topic_ids(&self) -> impl Iterator<Item = &EntityTopicId> {
574+
self.entities.keys()
575+
}
576+
567577
/// Returns information about an entity under a given MQTT entity topic identifier.
568578
pub fn get(&self, entity_topic_id: &EntityTopicId) -> Option<&EntityMetadata> {
569579
self.entities.get(entity_topic_id).map(EntityNode::metadata)

crates/core/tedge_api/src/mqtt_topics.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub enum EntityTopicError {
288288
///
289289
/// # Reference
290290
/// https://thin-edge.github.io/thin-edge.io/next/references/mqtt-api/#group-identifier
291-
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
291+
#[derive(Debug, Clone, Hash, Eq, Deserialize, Serialize)]
292292
#[serde(transparent)]
293293
pub struct EntityTopicId(String);
294294

@@ -309,6 +309,12 @@ impl Display for EntityTopicId {
309309
}
310310
}
311311

312+
impl AsRef<str> for EntityTopicId {
313+
fn as_ref(&self) -> &str {
314+
self.0.as_ref()
315+
}
316+
}
317+
312318
impl FromStr for EntityTopicId {
313319
type Err = TopicIdError;
314320

crates/core/tedge_api/src/store/pending_entity_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl PendingEntityStore {
125125
for message in telemetry_cache.into_iter() {
126126
match self.mqtt_schema.entity_channel_of(&message.topic) {
127127
Ok((tid, _)) => {
128-
if &tid == entity_tid {
128+
if tid == entity_tid {
129129
messages.push(message);
130130
} else {
131131
self.telemetry_cache.push(message)

0 commit comments

Comments
 (0)