From efa0257ea209f75c85b46884471d680b29139654 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 19 Jan 2021 11:59:14 +1100 Subject: [PATCH] Move tests to library test folder --- src/lib.rs | 143 ------------------- src/swarm_harness.rs => tests/harness/mod.rs | 0 tests/simple.rs | 140 ++++++++++++++++++ 3 files changed, 140 insertions(+), 143 deletions(-) rename src/swarm_harness.rs => tests/harness/mod.rs (100%) create mode 100644 tests/simple.rs diff --git a/src/lib.rs b/src/lib.rs index d9875b22..1a3222ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,6 @@ use std::convert::Infallible; use std::future::{Future, Ready}; use std::{io, iter, mem}; -#[cfg(test)] -mod swarm_harness; - type Protocol = BoxFuture<'static, Result>; type InboundProtocolFn = Box Protocol + Send + 'static>; type OutboundProtocolFn = @@ -471,143 +468,3 @@ where Poll::Pending } } - -#[cfg(test)] -#[allow(clippy::blacklisted_name)] -mod tests { - use super::*; - use crate::swarm_harness::await_events_or_timeout; - use anyhow::{Context, Error}; - use libp2p::swarm::SwarmEvent; - use swarm_harness::new_connected_swarm_pair; - use tokio::runtime::Handle; - - #[derive(serde::Serialize, serde::Deserialize, Debug)] - struct Message0 { - foo: u32, - } - #[derive(serde::Serialize, serde::Deserialize, Debug)] - struct Message1 { - bar: u32, - } - #[derive(serde::Serialize, serde::Deserialize, Debug)] - struct Message2 { - baz: u32, - } - - #[derive(Debug)] - struct AliceResult { - bar: u32, - } - #[derive(Debug)] - struct BobResult { - foo: u32, - baz: u32, - } - - #[derive(Debug)] - enum MyOutEvent { - Alice(AliceResult), - Bob(BobResult), - Failed(anyhow::Error), - } - - impl From> for MyOutEvent { - fn from(event: BehaviourOutEvent) -> Self { - match event { - BehaviourOutEvent::Inbound(_, Ok(bob)) => MyOutEvent::Bob(bob), - BehaviourOutEvent::Outbound(_, Ok(alice)) => MyOutEvent::Alice(alice), - BehaviourOutEvent::Inbound(_, Err(e)) | BehaviourOutEvent::Outbound(_, Err(e)) => { - MyOutEvent::Failed(e) - } - } - } - } - - #[derive(libp2p::NetworkBehaviour)] - #[behaviour(out_event = "MyOutEvent", event_process = false)] - struct MyBehaviour { - inner: NMessageBehaviour, - } - - impl MyBehaviour { - pub fn new() -> Self { - Self { - inner: NMessageBehaviour::new(b"/foo/bar/1.0.0"), - } - } - } - - impl MyBehaviour { - fn alice_do_protocol(&mut self, bob: PeerId, foo: u32, baz: u32) { - self.inner - .do_protocol_dialer(bob, move |mut substream| async move { - substream - .write_message( - &serde_cbor::to_vec(&Message0 { foo }) - .context("failed to serialize Message0")?, - ) - .await?; - - let bytes = substream.read_message(1024).await?; - - let message1 = serde_cbor::from_slice::(&bytes)?; - - substream - .write_message( - &serde_cbor::to_vec(&Message2 { baz }) - .context("failed to serialize Message2")?, - ) - .await?; - - Ok(AliceResult { bar: message1.bar }) - }) - } - - fn bob_do_protocol(&mut self, alice: PeerId, bar: u32) { - self.inner - .do_protocol_listener(alice, move |mut substream| async move { - let bytes = substream.read_message(1024).await?; - let message0 = serde_cbor::from_slice::(&bytes)?; - - substream - .write_message( - &serde_cbor::to_vec(&Message1 { bar }) - .context("failed to serialize Message1")?, - ) - .await?; - - let bytes = substream.read_message(1024).await?; - let message2 = serde_cbor::from_slice::(&bytes)?; - - Ok(BobResult { - foo: message0.foo, - baz: message2.baz, - }) - }) - } - } - - #[tokio::test] - async fn it_works() { - let _ = env_logger::try_init(); - - let (mut alice, mut bob) = - new_connected_swarm_pair(|_, _| MyBehaviour::new(), Handle::current()).await; - - alice.swarm.alice_do_protocol(bob.peer_id, 10, 42); - bob.swarm.bob_do_protocol(alice.peer_id, 1337); - - let (alice_event, bob_event) = - await_events_or_timeout(alice.swarm.next_event(), bob.swarm.next_event()).await; - - assert!(matches!( - alice_event, - SwarmEvent::Behaviour(MyOutEvent::Alice(AliceResult { bar: 1337 })) - )); - assert!(matches!( - bob_event, - SwarmEvent::Behaviour(MyOutEvent::Bob(BobResult { foo: 10, baz: 42 })) - )); - } -} diff --git a/src/swarm_harness.rs b/tests/harness/mod.rs similarity index 100% rename from src/swarm_harness.rs rename to tests/harness/mod.rs diff --git a/tests/simple.rs b/tests/simple.rs new file mode 100644 index 00000000..fb43512d --- /dev/null +++ b/tests/simple.rs @@ -0,0 +1,140 @@ +#![allow(clippy::blacklisted_name)] + +use anyhow::{Context, Error}; +use harness::await_events_or_timeout; +use harness::new_connected_swarm_pair; +use libp2p::swarm::SwarmEvent; +use libp2p::PeerId; +use libp2p_nmessage::{BehaviourOutEvent, NMessageBehaviour}; +use tokio::runtime::Handle; + +mod harness; + +#[derive(serde::Serialize, serde::Deserialize, Debug)] +struct Message0 { + foo: u32, +} +#[derive(serde::Serialize, serde::Deserialize, Debug)] +struct Message1 { + bar: u32, +} +#[derive(serde::Serialize, serde::Deserialize, Debug)] +struct Message2 { + baz: u32, +} + +#[derive(Debug)] +struct AliceResult { + bar: u32, +} +#[derive(Debug)] +struct BobResult { + foo: u32, + baz: u32, +} + +#[derive(Debug)] +enum MyOutEvent { + Alice(AliceResult), + Bob(BobResult), + Failed(anyhow::Error), +} + +impl From> for MyOutEvent { + fn from(event: BehaviourOutEvent) -> Self { + match event { + BehaviourOutEvent::Inbound(_, Ok(bob)) => MyOutEvent::Bob(bob), + BehaviourOutEvent::Outbound(_, Ok(alice)) => MyOutEvent::Alice(alice), + BehaviourOutEvent::Inbound(_, Err(e)) | BehaviourOutEvent::Outbound(_, Err(e)) => { + MyOutEvent::Failed(e) + } + } + } +} + +#[derive(libp2p::NetworkBehaviour)] +#[behaviour(out_event = "MyOutEvent", event_process = false)] +struct MyBehaviour { + inner: NMessageBehaviour, +} + +impl MyBehaviour { + pub fn new() -> Self { + Self { + inner: NMessageBehaviour::new(b"/foo/bar/1.0.0"), + } + } +} + +impl MyBehaviour { + fn alice_do_protocol(&mut self, bob: PeerId, foo: u32, baz: u32) { + self.inner + .do_protocol_dialer(bob, move |mut substream| async move { + substream + .write_message( + &serde_cbor::to_vec(&Message0 { foo }) + .context("failed to serialize Message0")?, + ) + .await?; + + let bytes = substream.read_message(1024).await?; + + let message1 = serde_cbor::from_slice::(&bytes)?; + + substream + .write_message( + &serde_cbor::to_vec(&Message2 { baz }) + .context("failed to serialize Message2")?, + ) + .await?; + + Ok(AliceResult { bar: message1.bar }) + }) + } + + fn bob_do_protocol(&mut self, alice: PeerId, bar: u32) { + self.inner + .do_protocol_listener(alice, move |mut substream| async move { + let bytes = substream.read_message(1024).await?; + let message0 = serde_cbor::from_slice::(&bytes)?; + + substream + .write_message( + &serde_cbor::to_vec(&Message1 { bar }) + .context("failed to serialize Message1")?, + ) + .await?; + + let bytes = substream.read_message(1024).await?; + let message2 = serde_cbor::from_slice::(&bytes)?; + + Ok(BobResult { + foo: message0.foo, + baz: message2.baz, + }) + }) + } +} + +#[tokio::test] +async fn it_works() { + let _ = env_logger::try_init(); + + let (mut alice, mut bob) = + new_connected_swarm_pair(|_, _| MyBehaviour::new(), Handle::current()).await; + + alice.swarm.alice_do_protocol(bob.peer_id, 10, 42); + bob.swarm.bob_do_protocol(alice.peer_id, 1337); + + let (alice_event, bob_event) = + await_events_or_timeout(alice.swarm.next_event(), bob.swarm.next_event()).await; + + assert!(matches!( + alice_event, + SwarmEvent::Behaviour(MyOutEvent::Alice(AliceResult { bar: 1337 })) + )); + assert!(matches!( + bob_event, + SwarmEvent::Behaviour(MyOutEvent::Bob(BobResult { foo: 10, baz: 42 })) + )); +}