grin/p2p/tests/peer_handshake.rs
Ignotus Peverell e50703d79e Externalized all peers selection and handling (#468)
Moved handling to the peer map out of the p2p server and into
its own struct. Allowed factoring code from the net adapter and
simplification of some interactions. Also removes the need for
the adapter to reference the p2p server or peers.

Fixes #430, #453 and #456
2017-12-14 01:13:05 +01:00

98 lines
2.8 KiB
Rust

// Copyright 2016 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate futures;
extern crate grin_core as core;
extern crate grin_p2p as p2p;
extern crate tokio_core;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time;
use futures::future::Future;
use tokio_core::net::TcpStream;
use tokio_core::reactor::{self, Core};
use core::core::target::Difficulty;
use core::core::hash::Hash;
use p2p::Peer;
// Starts a server and connects a client peer to it to check handshake,
// followed by a ping/pong exchange to make sure the connection is live.
#[test]
fn peer_handshake() {
let mut evtlp = Core::new().unwrap();
let handle = evtlp.handle();
let p2p_conf = p2p::P2PConfig::default();
let net_adapter = Arc::new(p2p::DummyAdapter {});
let server = p2p::Server::new(
".grin".to_owned(),
p2p::UNKNOWN,
p2p_conf,
net_adapter.clone(),
Hash::from_vec(vec![]),
).unwrap();
let run_server = server.start(handle.clone());
let my_addr = "127.0.0.1:5000".parse().unwrap();
let phandle = handle.clone();
let rhandle = handle.clone();
let timeout = reactor::Timeout::new(time::Duration::new(1, 0), &handle).unwrap();
let timeout_send = reactor::Timeout::new(time::Duration::new(2, 0), &handle).unwrap();
handle.spawn(
timeout
.from_err()
.and_then(move |_| {
let p2p_conf = p2p::P2PConfig::default();
let addr = SocketAddr::new(p2p_conf.host, p2p_conf.port);
let socket =
TcpStream::connect(&addr, &phandle).map_err(|e| p2p::Error::Connection(e));
socket
.and_then(move |socket| {
Peer::connect(
socket,
p2p::UNKNOWN,
Difficulty::one(),
my_addr,
Arc::new(p2p::handshake::Handshake::new(Hash::from_vec(vec![]))),
net_adapter.clone(),
)
})
.and_then(move |(socket, peer)| {
rhandle.spawn(peer.run(socket).map_err(|e| {
panic!("Client run failed: {:?}", e);
}));
peer.send_ping(Difficulty::one(), 0).unwrap();
timeout_send.from_err().map(|_| peer)
})
.and_then(|peer| {
let (sent, recv) = peer.transmitted_bytes();
assert!(sent > 0);
assert!(recv > 0);
Ok(())
})
.and_then(|_| {
assert!(server.peers.peer_count() > 0);
server.stop();
Ok(())
})
})
.map_err(|e| {
panic!("Client connection failed: {:?}", e);
}),
);
evtlp.run(run_server).unwrap();
}