Merge pull request #831 from ethcore/test-socket-devtools

Test socket to common test code (ethcore-devtools)
This commit is contained in:
Gav Wood 2016-03-26 20:07:40 +01:00
commit 785e9d1be6
6 changed files with 6 additions and 70 deletions

View File

@ -20,5 +20,7 @@
extern crate rand;
pub mod random_path;
pub mod test_socket;
pub use random_path::*;
pub use test_socket::*;

View File

@ -11,6 +11,7 @@ path = "run.rs"
"ethcore-ipc" = { path = "../rpc" }
bincode = "*"
serde = "0.7.0"
ethcore-devtools = { path = "../../devtools" }
[build-dependencies]
syntex = "0.30.0"

View File

@ -17,9 +17,9 @@
#[cfg(test)]
mod tests {
use super::super::socket::*;
use super::super::service::*;
use ipc::*;
use devtools::*;
#[test]
fn call_service() {

View File

@ -17,7 +17,7 @@
extern crate bincode;
extern crate ethcore_ipc as ipc;
extern crate serde;
extern crate ethcore_devtools as devtools;
pub mod socket;
pub mod service;
mod examples;

View File

@ -501,77 +501,10 @@ mod tests {
use std::sync::*;
use super::super::stats::*;
use std::io::{Read, Write, Error, Cursor, ErrorKind};
use std::cmp;
use mio::{EventSet};
use std::collections::VecDeque;
use bytes::*;
struct TestSocket {
read_buffer: Vec<u8>,
write_buffer: Vec<u8>,
cursor: usize,
buf_size: usize,
}
impl Default for TestSocket {
fn default() -> Self {
TestSocket::new()
}
}
impl TestSocket {
fn new() -> Self {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
buf_size: 0,
}
}
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, Error> {
let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len());
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 = self.cursor + buf.len();
Ok(len)
}
}
}
}
impl Write for TestSocket {
fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
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<(), Error> {
unimplemented!();
}
}
use devtools::*;
impl GenericSocket for TestSocket {}