mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-01 04:01:20 +00:00
* remove tokio_current_thread tests * Review changes: Removed newline Moved uds tokio test crate to top to avoid self and keep with convention of other test crates Removed sleep from uds test and block until all futures are completed.
141 lines
5.0 KiB
Rust
141 lines
5.0 KiB
Rust
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
// to deal in the Software without restriction, including without limitation
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
extern crate bytes;
|
|
extern crate futures;
|
|
extern crate libp2p_mplex as multiplex;
|
|
extern crate libp2p_core as swarm;
|
|
extern crate libp2p_tcp_transport as tcp;
|
|
extern crate tokio;
|
|
extern crate tokio_io;
|
|
|
|
use futures::future::Future;
|
|
use futures::{Sink, Stream};
|
|
use std::sync::{Arc, mpsc};
|
|
use std::thread;
|
|
use swarm::{muxing, Transport};
|
|
use tcp::TcpConfig;
|
|
use tokio_io::codec::length_delimited::Framed;
|
|
use tokio::runtime::current_thread::Runtime;
|
|
|
|
#[test]
|
|
fn client_to_server_outbound() {
|
|
// Simulate a client sending a message to a server through a multiplex upgrade.
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let bg_thread = thread::spawn(move || {
|
|
let transport =
|
|
TcpConfig::new().with_upgrade(multiplex::MplexConfig::new());
|
|
|
|
let (listener, addr) = transport
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
.unwrap();
|
|
tx.send(addr).unwrap();
|
|
|
|
let future = listener
|
|
.into_future()
|
|
.map_err(|(err, _)| err)
|
|
.and_then(|(client, _)| client.unwrap().0)
|
|
.and_then(|client| muxing::outbound_from_ref_and_wrap(Arc::new(client)))
|
|
.map(|client| Framed::<_, bytes::BytesMut>::new(client.unwrap()))
|
|
.and_then(|client| {
|
|
client
|
|
.into_future()
|
|
.map_err(|(err, _)| err)
|
|
.map(|(msg, _)| msg)
|
|
})
|
|
.and_then(|msg| {
|
|
let msg = msg.unwrap();
|
|
assert_eq!(msg, "hello world");
|
|
Ok(())
|
|
});
|
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
let _ = rt.block_on(future).unwrap();
|
|
});
|
|
|
|
let transport = TcpConfig::new().with_upgrade(multiplex::MplexConfig::new());
|
|
|
|
let future = transport
|
|
.dial(rx.recv().unwrap())
|
|
.unwrap()
|
|
.and_then(|client| muxing::inbound_from_ref_and_wrap(Arc::new(client)))
|
|
.map(|server| Framed::<_, bytes::BytesMut>::new(server.unwrap()))
|
|
.and_then(|server| server.send("hello world".into()))
|
|
.map(|_| ());
|
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
let _ = rt.block_on(future).unwrap();
|
|
bg_thread.join().unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn client_to_server_inbound() {
|
|
// Simulate a client sending a message to a server through a multiplex upgrade.
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let bg_thread = thread::spawn(move || {
|
|
let transport =
|
|
TcpConfig::new().with_upgrade(multiplex::MplexConfig::new());
|
|
|
|
let (listener, addr) = transport
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
.unwrap();
|
|
tx.send(addr).unwrap();
|
|
|
|
let future = listener
|
|
.into_future()
|
|
.map_err(|(err, _)| err)
|
|
.and_then(|(client, _)| client.unwrap().0)
|
|
.and_then(|client| muxing::inbound_from_ref_and_wrap(Arc::new(client)))
|
|
.map(|client| Framed::<_, bytes::BytesMut>::new(client.unwrap()))
|
|
.and_then(|client| {
|
|
client
|
|
.into_future()
|
|
.map_err(|(err, _)| err)
|
|
.map(|(msg, _)| msg)
|
|
})
|
|
.and_then(|msg| {
|
|
let msg = msg.unwrap();
|
|
assert_eq!(msg, "hello world");
|
|
Ok(())
|
|
});
|
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
let _ = rt.block_on(future).unwrap();
|
|
});
|
|
|
|
let transport = TcpConfig::new().with_upgrade(multiplex::MplexConfig::new());
|
|
|
|
let future = transport
|
|
.dial(rx.recv().unwrap())
|
|
.unwrap()
|
|
.and_then(|client| muxing::outbound_from_ref_and_wrap(Arc::new(client)))
|
|
.map(|server| Framed::<_, bytes::BytesMut>::new(server.unwrap()))
|
|
.and_then(|server| server.send("hello world".into()))
|
|
.map(|_| ());
|
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
let _ = rt.block_on(future).unwrap();
|
|
bg_thread.join().unwrap();
|
|
}
|