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

Tests for nodes/listeners.rs #541

Merged
merged 20 commits into from
Oct 10, 2018
Merged

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Oct 5, 2018

The relevant commits here are:

  • ec2a2f1: unit tests
  • 6341575: extra tests to illustrate/facilitate upcoming refactor

dvdplm added 13 commits October 3, 2018 11:16
…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)
(Box::new(stream), addr2)
},
Async::Ready(None) => {
let stream = stream::poll_fn(|| future::ok(None).poll() )
Copy link
Contributor

Choose a reason for hiding this comment

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

let stream = stream::empty()?

Copy link
Contributor Author

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. :)

(Box::new(stream), addr2)
},
Async::Ready(Some(_)) => {
let stream = stream::poll_fn(|| future::ok(Some(1usize)).poll() )
Copy link
Contributor

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..)
    ...

Copy link
Contributor Author

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.

let tupelize = move |stream| future::ok( (stream, future::ok(addr.clone())) );
Ok(match async {
Async::NotReady => {
let stream = stream::poll_fn(|| future::empty().poll() )
Copy link
Contributor

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))?

Copy link
Contributor Author

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.

}
ListenerState::Error => Err( (self, addr2) )
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This } looks misaligned.


#[derive(Debug, PartialEq, Clone, Copy)]
pub(crate) enum ListenerState {
Ok(Async<Option<usize>>),
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

// 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);
Copy link
Contributor

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];?

Copy link
Contributor Author

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?

Copy link
Contributor

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.

dvdplm added 2 commits October 9, 2018 16:26
…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)
Box::new(stream)
}
}
}
Copy link
Contributor

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?

Copy link
Contributor Author

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/**
Copy link
Member

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

Copy link
Contributor Author

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.

// 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) {
Copy link
Member

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>>();
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.


#[derive(Debug, PartialEq, Clone, Copy)]
pub(crate) enum ListenerState {
Ok(Async<Option<usize>>),
Copy link
Member

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.

Copy link
Contributor Author

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?

}
ListenerState::Error => Err( (self, addr2) )
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

@tomaka tomaka merged commit 0c7f313 into libp2p:master Oct 10, 2018
dvdplm added a commit to dvdplm/rust-libp2p that referenced this pull request Oct 11, 2018
…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)
dvdplm added a commit to dvdplm/rust-libp2p that referenced this pull request Oct 11, 2018
…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)
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