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

Add iterator and accessor to EventManager #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jiangliu
Copy link
Member

@jiangliu jiangliu commented Jun 6, 2021

Add iterator and accessor to EventManager, so we could walk all registered subscribers.

jiangliu added 2 commits June 11, 2021 16:30
Implement iterator and accessor for EventManager, so we could iterate
over all subscribers regisgtered. Also improve test cases in
basic_event_manager.rs.

Signed-off-by: Liu Jiang <[email protected]>
Add two unit test cases for SubscriberId and remote endpoint.

Signed-off-by: Liu Jiang <[email protected]>
Copy link
Member

@andreeaflorescu andreeaflorescu left a comment

Choose a reason for hiding this comment

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

Sorry for the late review.

@@ -168,6 +168,9 @@ pub trait SubscriberOps {
/// Removes the subscriber corresponding to `subscriber_id` from the watch list.
fn remove_subscriber(&mut self, subscriber_id: SubscriberId) -> Result<Self::Subscriber>;

/// Returns a reference to the subscriber corresponding to `subscriber_id`.
fn subscriber_ref(&self, subscriber_id: SubscriberId) -> Option<&Self::Subscriber>;
Copy link
Member

Choose a reason for hiding this comment

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

Can we return a Result here as well so that it in line with the other getter we have for subscribers (i.e. the subscriber_mut)?

Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test for when the subscriber was removed, and then we call subscriber_ref?

@@ -35,6 +35,15 @@ impl App {
let _ = self.event_manager.run_with_timeout(100);
}

fn iter(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

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

This check doesn't really fit here. This is the example that we're also referencing in the public documentation, so I'd like to keep it as close as we can to a real use life use case.

How about we move this to a separate test instead of having it part of the App interface? We can add a test_iter and move the following asserts there after initializing the subscribers.

use super::*;

#[test]
fn test_subscriber_id_derives() {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason for checking the derives? We considered this to be coverage tests, and we avoided them in the past. I am wondering if there is something particular about the SubscriberId that we want to validate, or we could remove them.

}

/// An iterator visiting all subscribers in arbitrary order, with immutable references.
pub(crate) fn iter(&mut self) -> Iter<'_, SubscriberId, T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this use an immutable reference &self? (duplicate of 49ba509#r1168309051)

@@ -181,6 +187,16 @@ impl<S: MutEventSubscriber> EventManager<S> {
#[cfg(feature = "remote_endpoint")]
self.dispatch_endpoint_event(endpoint_event);
}

/// An iterator visiting all subscribers in arbitrary order, with immutable references.
pub fn iter(&mut self) -> Iter<'_, SubscriberId, S> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this use an immutable reference &self? (duplicate of 49ba509#r1168308920)

// Once the app does not need events anymore, the cleanup needs to be called.
// This is particularly important when the app continues the execution, but event monitoring
// is not necessary.
app.cleanup();

assert!(app.event_manager.subscriber_ref(id).is_none());
assert!(app.event_manager.subscriber_mut(id).is_err());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this check the error more explicitly?

E.g. assert_eq!(app.event_manager.subscriber_mut(id), Err(..));

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.

3 participants