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 option to query hosts via regex #77

Merged
merged 7 commits into from
Feb 8, 2023
Merged

Conversation

battesonb
Copy link
Member

This allows various host interactions via regex matches to reduce the effort to induce more complex interactions. While this has changed the explicit API contract in some cases, this should not affect most test cases as the ToIpAddrs trait implements ToIpAddr for all T that implement ToIpAddr.

This allows various host interactions via regex matches to reduce the
effort to induce more complex interactions. While this has changed the
explicit API contract in some cases, this should not affect most test
cases as the `ToIpAddrs` trait implements `ToIpAddr` for all `T` that
implement `ToIpAddr`.
@battesonb battesonb requested a review from mcches January 19, 2023 17:51
Copy link
Contributor

@mcches mcches left a comment

Choose a reason for hiding this comment

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

Looks pretty good, just wondering if/how we want to break the current public contracts.

@@ -10,6 +11,10 @@ pub trait ToIpAddr {
fn to_ip_addr(&self, dns: &mut Dns) -> IpAddr;
}

pub trait ToIpAddrs {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we break the contract and need to bump the version anyways, should we consider consolidating into a single trait?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could do, however there is the need for a trait like ToIpAddr for the sim#client and sim#host functions. As in these cases, the trait doubles as a generator rather than only a matcher.

impl ToIpAddrs for Regex {
fn to_ip_addrs(&self, dns: &mut Dns) -> Vec<IpAddr> {
#[allow(clippy::needless_collect)]
let hosts = dns.names.keys().cloned().collect::<Vec<_>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, because of the &mut?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I battled with this one for quite a while. There's an open issue for clippy not to produce a false positive in this situation: rust-lang/rust-clippy#6066

/// Lookup an IP address by host name. Use regex to match a number of hosts.
///
/// Must be called from within a Turmoil simulation.
pub fn lookup_many(addr: impl ToIpAddrs) -> Vec<IpAddr> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems only a Regex would be useful here, so perhaps we can just be explicit instead of using the trait?

Copy link
Member Author

Choose a reason for hiding this comment

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

The ToIpAddrs trait could also support a Vec of string matches (actually not with this revision, but I'll update it), so for example another valid input would be:

lookup_many(vec!["host-a", "host-b"]);

I was also looking into passing predicate callbacks, but I ran into trouble with conflicting generics with the ToIpAddr impl for ToIpAddrs.

Changing this, however, will affect all the other methods that currently take ToIpAddr (hold, release, partition). It may be a nice convenience if we asserted that the lookup_many should produce at least one element in each set, as its use could otherwise produce subtle bugs.

Copy link
Member

Choose a reason for hiding this comment

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

It may be a nice convenience if we asserted that the lookup_many should produce at least one element in each set, as its use could otherwise produce subtle bugs.

Does this mean that if we look up two hosts we must see at least two hosts returning?

src/sim.rs Outdated Show resolved Hide resolved
src/sim.rs Outdated Show resolved Hide resolved
src/sim.rs Outdated Show resolved Hide resolved
src/sim.rs Outdated Show resolved Hide resolved
src/sim.rs Outdated Show resolved Hide resolved
Updates doc comments, improves the tests and rewrites the Vec
implementation to support any `T` that implements `ToIpAddr`.
This adds a separate optional regex flag to ensure that non-std features
are optional.
This type now simply exists to shadow all existing `ToIpAddr`
implementers and add the regex option when the feature flag is enabled.
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

This seems like a good addition 👍. I think its fine atm to do small breaking changes often since this is just a library for writing tests.

Copy link
Contributor

@mcches mcches left a comment

Choose a reason for hiding this comment

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

Nice! This looks solid.

@@ -78,7 +78,7 @@ impl Rt {
let (tokio, local) = init();

_ = mem::replace(&mut self.tokio, tokio);
_ = mem::replace(&mut self.local, local);
drop(mem::replace(&mut self.local, local));
Copy link
Contributor

Choose a reason for hiding this comment

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

clippy lint?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it either wants you to explicitly drop or await the return here.

@battesonb battesonb merged commit 77bd91b into tokio-rs:main Feb 8, 2023
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