-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Tests for nodes/listeners.rs #541
Conversation
…e-listeners * upstream/master: Implement GET_PROVIDERS/ADD_PROVIDER Kademlia messages (libp2p#530) Make secio almost compile for asmjs/wasm (libp2p#519) Make it possible to accept/deny nodes in CollectionsStream (libp2p#512) Fix polling in handled_node.rs (libp2p#531) Remove notifying tasks (libp2p#528) Fix handler not properly shut down, and write test for this (libp2p#514) Use unsigned-varints, add BLAKE2 support in multihash (libp2p#525) Upgrade smallvec version to fix license issue (libp2p#526)
…e-listeners * upstream/master: Add unit tests for core::nodes::NodeStream (libp2p#535)
core/src/tests/dummy_transport.rs
Outdated
(Box::new(stream), addr2) | ||
}, | ||
Async::Ready(None) => { | ||
let stream = stream::poll_fn(|| future::ok(None).poll() ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let stream = stream::empty()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is indeed a lot better. :)
core/src/tests/dummy_transport.rs
Outdated
(Box::new(stream), addr2) | ||
}, | ||
Async::Ready(Some(_)) => { | ||
let stream = stream::poll_fn(|| future::ok(Some(1usize)).poll() ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let stream = stream::repeat(1)
?
I think it would be more useful even to enumerate the events instead of constantly yielding 1, so how about:
Async::Ready(Some(n)) => {
let stream = stream::iter_ok(n..)
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it, much clearer.
core/src/tests/dummy_transport.rs
Outdated
let tupelize = move |stream| future::ok( (stream, future::ok(addr.clone())) ); | ||
Ok(match async { | ||
Async::NotReady => { | ||
let stream = stream::poll_fn(|| future::empty().poll() ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let stream = stream::poll_fn(|| Ok(Async::NotReady))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
As you can tell I struggled quite a bit to get the type-soup right here. Great suggestions, much appreciated.
core/src/tests/dummy_transport.rs
Outdated
} | ||
ListenerState::Error => Err( (self, addr2) ) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This }
looks misaligned.
core/src/tests/dummy_transport.rs
Outdated
|
||
#[derive(Debug, PartialEq, Clone, Copy)] | ||
pub(crate) enum ListenerState { | ||
Ok(Async<Option<usize>>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment about the usize
here could be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here was just to put "something" in there, preferably non-void. I see why it could be surprising to the reader; what's a better choice? I did add a comment though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I assumed it would be a dummy, in which case I would have suggested ()
instead. However one can write tests which check the item index of a listener (hence my other suggestion (stream::iter_ok(n..)
). Consequently I would write a comment stating that the usize value indexes items produced by the listener.
core/src/nodes/listeners.rs
Outdated
// set things up for the tests. | ||
impl ListenersStream<DummyTransport> { | ||
fn set_listener_state(&mut self, idx: usize, state: ListenerState) { | ||
let mut l = self.listeners.remove(idx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why removing and re-inserting? Why not just let l = &mut self.listeners[idx];
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly to satisfy the compiler (got a error[E0507]: cannot move out of indexed content
otherwise) but when I wrote it I was trying to understand the re-ordering behaviour of the code so I thought I'd make it behave "realistically". I think it is necessary though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let l = &mut self.listeners[idx];
should work.
…e-listeners * upstream/master: Fix secio compied with --no-default-features (libp2p#545) multiaddr: explain the use of `&str` for `Unix`. (libp2p#543) Model HandshakeContext with explicit state transitions. (libp2p#532) multiaddr: add support for onion protocol. (libp2p#542)
core/src/nodes/listeners.rs
Outdated
Box::new(stream) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the ListenerState::Ok
branch of this match
not be doing the same as in DummyTransport::listen_on
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely should, fixing.
.gitignore
Outdated
@@ -1,2 +1,4 @@ | |||
target | |||
Cargo.lock | |||
.idea/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The content of gitignore should be specific to the project => https://stackoverflow.com/questions/7335420/global-git-ignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this was a mistake. Reverting.
core/src/nodes/listeners.rs
Outdated
// Test helper that lets us poke in innards of individual `Listener`s and | ||
// set things up for the tests. | ||
impl ListenersStream<DummyTransport> { | ||
fn set_listener_state(&mut self, idx: usize, state: ListenerState) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a freestanding method that accepts a &mut ListenersStream<DummyTransport>
.
We don't want to hide method implementations behind #[cfg(test)]
.
ls.listen_on(addr1).expect("listen_on failed"); | ||
ls.listen_on(addr2).expect("listen_on failed"); | ||
|
||
let listener_addrs = ls.listeners().map(|ma| ma.to_string() ).collect::<Vec<String>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically shouldn't have to convert to string, but why not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trouble was that the iterator doesn't impl Debug
(and PartialEq
) so I did this to move ahead. Should I try to improve on this or not worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not worth it since it's just a test.
core/src/tests/dummy_transport.rs
Outdated
|
||
#[derive(Debug, PartialEq, Clone, Copy)] | ||
pub(crate) enum ListenerState { | ||
Ok(Async<Option<usize>>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation should be 4 spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, so it's spaces on libp2p, interesting! :)
Is it ok if I add an .editorconfig
to the project?
core/src/tests/dummy_transport.rs
Outdated
} | ||
ListenerState::Error => Err( (self, addr2) ) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
…e-handled_node * upstream/master: Add `StreamMuxer::flush`. (libp2p#534) Tests for nodes/listeners.rs (libp2p#541) Deny relative addresses for UNIX domain sockets (libp2p#548) Optimise `Multiaddr::append`. (libp2p#549)
…pl-for-node-event * upstream/master: Shut down yamux and fix mplex shutdown. (libp2p#559) Add `StreamMuxer::flush`. (libp2p#534) Tests for nodes/listeners.rs (libp2p#541) Deny relative addresses for UNIX domain sockets (libp2p#548) Optimise `Multiaddr::append`. (libp2p#549)
The relevant commits here are: