final network tests di setup

This commit is contained in:
Nikolay Volf 2016-02-03 18:05:46 +03:00
parent e86a680b23
commit df18d8104d
3 changed files with 90 additions and 60 deletions

View File

@ -425,4 +425,85 @@ pub fn test_encryption() {
encoder.encrypt(&mut RefReadBuffer::new(&before2), &mut RefWriteBuffer::new(&mut got), true).unwrap(); encoder.encrypt(&mut RefReadBuffer::new(&before2), &mut RefWriteBuffer::new(&mut got), true).unwrap();
encoder.reset(); encoder.reset();
assert_eq!(got, after2); assert_eq!(got, after2);
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::*;
use super::super::stats::*;
use std::io::{Read, Write, Error};
use std::cmp;
use mio::{EventSet};
use std::collections::VecDeque;
use bytes::*;
struct TestSocket {
read_buffer: Vec<u8>,
write_buffer: Vec<u8>,
cursor: usize,
}
impl TestSocket {
fn new() -> TestSocket {
TestSocket {
read_buffer: vec![],
write_buffer: vec![],
cursor: 0,
}
}
}
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> {
self.write_buffer.extend(buf.iter().cloned());
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Error> {
unimplemented!();
}
}
impl GenericSocket for TestSocket {}
type TestConnection = GenericConnection<TestSocket>;
impl TestConnection {
pub fn new() -> TestConnection {
TestConnection {
token: 999998888usize,
socket: TestSocket::new(),
send_queue: VecDeque::new(),
rec_buf: Bytes::new(),
rec_size: 0,
interest: EventSet::hup() | EventSet::readable(),
stats: Arc::<NetworkStats>::new(NetworkStats::new()),
}
}
}
#[test]
fn connection_expect() {
let mut connection = TestConnection::new();
connection.expect(1024);
assert_eq!(connection.rec_size, 1024);
}
} }

View File

@ -48,4 +48,13 @@ impl NetworkStats {
pub fn sessions(&self) -> usize { pub fn sessions(&self) -> usize {
self.sessions.load(Ordering::Relaxed) self.sessions.load(Ordering::Relaxed)
} }
#[cfg(test)]
pub fn new() -> NetworkStats {
NetworkStats {
recv: AtomicUsize::new(0),
send: AtomicUsize::new(0),
sessions: AtomicUsize::new(0),
}
}
} }

View File

@ -20,61 +20,6 @@ impl Default for TestProtocol {
} }
} }
struct TestSocket {
read_buffer: Vec<u8>,
write_buffer: Vec<u8>,
cursor: usize,
}
impl TestSocket {
fn new() -> TestSocket {
TestSocket {
read_buffer: vec![],
write_buffer: vec![]
}
}
}
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());
let len = cmp::max(self.end_position - self.cursor, 0);
match len {
0 => Ok(0),
_ => {
bytes::copy_memory(buf, &mut self.read_buffer[cursor..len]);
self.cursor = self.cursor + buf.len();
Ok(len);
}
}
}
}
impl Write for TestSocket {
fn write(&mut self, buf: &mut [u8]) -> Result<u8> {
self.write_buffer.extend(buf.iter().cloned());
Ok(buf.len());
}
}
impl GenericSocket for TestSocket {}
type TestConnection = GenericConnection<TestSocket>;
impl TestConnection {
pub fn new() -> Connection {
Connection {
token: 999998888usize,
socket: TestSocket::new(),
send_queue: VecDeque::new(),
rec_buf: Bytes::new(),
rec_size: 0,
interest: EventSet::hup() | EventSet::readable(),
stats: Arc::<NetworkStats>::new(),
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct TestProtocolMessage { pub struct TestProtocolMessage {
payload: u32, payload: u32,
@ -156,8 +101,3 @@ fn net_timeout() {
thread::sleep(Duration::from_millis(50)); thread::sleep(Duration::from_millis(50));
} }
} }
#[test]
fn connection_expect() {
let connection = TestConnection::new();
}