Moved TestSocket to ethcore-network (#7633)

This commit is contained in:
Marek Kotewicz 2018-01-19 14:41:34 +01:00 committed by GitHub
parent 2af4bd195f
commit b535bf390c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 111 deletions

1
Cargo.lock generated
View File

@ -629,7 +629,6 @@ dependencies = [
"bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethcore-bytes 0.1.0", "ethcore-bytes 0.1.0",
"ethcore-devtools 1.9.0",
"ethcore-io 1.9.0", "ethcore-io 1.9.0",
"ethcore-logger 1.9.0", "ethcore-logger 1.9.0",
"ethcrypto 0.1.0", "ethcrypto 0.1.0",

View File

@ -16,12 +16,9 @@
//! dev-tools //! dev-tools
extern crate rand; extern crate rand;
mod random_path; mod random_path;
mod test_socket;
pub mod http_client; pub mod http_client;
pub use random_path::*; pub use random_path::*;
pub use test_socket::*;

View File

@ -1,95 +0,0 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::io::*;
use std::cmp;
pub struct TestSocket {
pub read_buffer: Vec<u8>,
pub write_buffer: Vec<u8>,
pub cursor: usize,
pub buf_size: usize,
}
impl Default for TestSocket {
fn default() -> Self {
TestSocket::new()
}
}
impl TestSocket {
pub fn new() -> Self {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
buf_size: 0,
}
}
pub fn new_buf(buf_size: usize) -> TestSocket {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
buf_size: buf_size,
}
}
pub fn new_ready(data: Vec<u8>) -> TestSocket {
TestSocket {
read_buffer: data,
write_buffer: vec![],
cursor: 0,
buf_size: 0,
}
}
}
impl Read for TestSocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len());
if self.cursor > end_position { return Ok(0) }
let len = cmp::max(end_position - self.cursor, 0);
match len {
0 => Ok(0),
_ => {
for i in self.cursor..end_position {
buf[i-self.cursor] = self.read_buffer[i];
}
self.cursor = end_position;
Ok(len)
}
}
}
}
impl Write for TestSocket {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
if self.buf_size == 0 || buf.len() < self.buf_size {
self.write_buffer.extend(buf.iter().cloned());
Ok(buf.len())
}
else {
self.write_buffer.extend(buf.iter().take(self.buf_size).cloned());
Ok(self.buf_size)
}
}
fn flush(&mut self) -> Result<()> {
unimplemented!();
}
}

View File

@ -35,7 +35,6 @@ serde_json = "1.0"
error-chain = { version = "0.11", default-features = false } error-chain = { version = "0.11", default-features = false }
[dev-dependencies] [dev-dependencies]
ethcore-devtools = { path = "../../devtools" }
tempdir = "0.3" tempdir = "0.3"
[features] [features]

View File

@ -504,16 +504,85 @@ pub fn test_encryption() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use std::cmp;
use std::collections::VecDeque;
use std::io::{Read, Write, Cursor, ErrorKind, Result, Error};
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use super::super::stats::*;
use std::io::{Read, Write, Error, Cursor, ErrorKind};
use mio::{Ready}; use mio::{Ready};
use std::collections::VecDeque;
use ethcore_bytes::Bytes; use ethcore_bytes::Bytes;
use devtools::TestSocket;
use io::*; use io::*;
use super::super::stats::*;
use super::*;
pub struct TestSocket {
pub read_buffer: Vec<u8>,
pub write_buffer: Vec<u8>,
pub cursor: usize,
pub buf_size: usize,
}
impl Default for TestSocket {
fn default() -> Self {
TestSocket::new()
}
}
impl TestSocket {
pub fn new() -> Self {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
buf_size: 0,
}
}
pub fn new_buf(buf_size: usize) -> TestSocket {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
buf_size: buf_size,
}
}
}
impl Read for TestSocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len());
if self.cursor > end_position { return Ok(0) }
let len = cmp::max(end_position - self.cursor, 0);
match len {
0 => Ok(0),
_ => {
for i in self.cursor..end_position {
buf[i-self.cursor] = self.read_buffer[i];
}
self.cursor = end_position;
Ok(len)
}
}
}
}
impl Write for TestSocket {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
if self.buf_size == 0 || buf.len() < self.buf_size {
self.write_buffer.extend(buf.iter().cloned());
Ok(buf.len())
}
else {
self.write_buffer.extend(buf.iter().take(self.buf_size).cloned());
Ok(self.buf_size)
}
}
fn flush(&mut self) -> Result<()> {
unimplemented!();
}
}
impl GenericSocket for TestSocket {} impl GenericSocket for TestSocket {}
@ -522,17 +591,17 @@ mod tests {
} }
impl Read for TestBrokenSocket { impl Read for TestBrokenSocket {
fn read(&mut self, _: &mut [u8]) -> Result<usize, Error> { fn read(&mut self, _: &mut [u8]) -> Result<usize> {
Err(Error::new(ErrorKind::Other, self.error.clone())) Err(Error::new(ErrorKind::Other, self.error.clone()))
} }
} }
impl Write for TestBrokenSocket { impl Write for TestBrokenSocket {
fn write(&mut self, _: &[u8]) -> Result<usize, Error> { fn write(&mut self, _: &[u8]) -> Result<usize> {
Err(Error::new(ErrorKind::Other, self.error.clone())) Err(Error::new(ErrorKind::Other, self.error.clone()))
} }
fn flush(&mut self) -> Result<(), Error> { fn flush(&mut self) -> Result<()> {
unimplemented!(); unimplemented!();
} }
} }

View File

@ -89,9 +89,6 @@ extern crate error_chain;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[cfg(test)]
extern crate ethcore_devtools as devtools;
#[cfg(test)] #[cfg(test)]
extern crate tempdir; extern crate tempdir;