Skip to content

Commit

Permalink
add tests for proto impl
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 21, 2018
1 parent b0023eb commit c1ccb92
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/codec/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::{self, Cursor, Error, ErrorKind};
use bytes::{BigEndian, BufMut, Bytes, BytesMut};
use byteorder::ReadBytesExt;

#[derive(Debug, PartialEq)]
pub enum CodecType {
Client,
Server,
Expand Down
2 changes: 1 addition & 1 deletion src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod common;
pub mod common;
pub mod tcp;
pub mod rtu;
11 changes: 5 additions & 6 deletions src/codec/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use bytes::{BigEndian, BufMut, Bytes, BytesMut};
use byteorder::ReadBytesExt;
use super::common::*;

#[derive(Debug, PartialEq)]
pub(crate) struct Codec {
decoder: RtuDecoder,
codec_type: CodecType,
pub(crate) decoder: RtuDecoder,
}

struct RtuDecoder {
#[derive(Debug, PartialEq)]
pub(crate) struct RtuDecoder {
codec_type: CodecType,
}

Expand All @@ -22,15 +23,13 @@ impl Codec {
decoder: RtuDecoder {
codec_type: CodecType::Client,
},
codec_type: CodecType::Client,
}
}
pub fn server() -> Codec {
Codec {
decoder: RtuDecoder {
codec_type: CodecType::Server,
},
codec_type: CodecType::Server,
}
}
}
Expand Down Expand Up @@ -125,7 +124,7 @@ impl Decoder for Codec {

fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<RtuAdu>> {
if let Some((address, data)) = self.decoder.decode(buf)? {
let pdu = match self.codec_type {
let pdu = match self.decoder.codec_type {
CodecType::Client => {
let res = if data[0] > 0x80 {
Err(ExceptionResponse::try_from(data)?)
Expand Down
8 changes: 5 additions & 3 deletions src/codec/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use super::common::*;
const HEADER_SIZE: usize = 7;
const PROTOCOL_ID: u16 = 0x0;

struct TcpDecoder;
#[derive(Debug, PartialEq)]
pub(crate) struct TcpDecoder;

#[derive(Debug, PartialEq)]
pub(crate) struct Codec {
decoder: TcpDecoder,
codec_type: CodecType,
pub(crate) decoder: TcpDecoder,
pub(crate) codec_type: CodecType,
}

impl Codec {
Expand Down
67 changes: 67 additions & 0 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ pub mod tcp {
Ok(io.framed(Codec::server()))
}
}

#[cfg(test)]
mod tests {
use super::Proto;
use codec::tcp::Codec;
use codec::common::CodecType;
use super::super::dummy_io::DummyIo;

#[test]
fn bind_transport() {
use tokio_proto::pipeline::ClientProto;
let proto = Proto;
let io = DummyIo;
let (_, codec) = proto.bind_transport(io).unwrap().into_parts_and_codec();
assert_eq!(codec.codec_type, CodecType::Client);
assert_eq!(codec, Codec::client());
}
}
}

pub mod rtu {
Expand Down Expand Up @@ -64,4 +82,53 @@ pub mod rtu {
Ok(io.framed(Codec::server()))
}
}

#[cfg(test)]
mod tests {
use super::Proto;
use codec::rtu::Codec;
use super::super::dummy_io::DummyIo;

#[test]
fn bind_transport() {
use tokio_proto::pipeline::ClientProto;
let proto = Proto;
let io = DummyIo;
let (_, codec) = proto.bind_transport(io).unwrap().into_parts_and_codec();
assert_eq!(codec, Codec::client());
}
}
}

#[cfg(test)]
mod dummy_io {
use std::io::Error;
use std::io::{Read, Write};
use tokio_io::{AsyncRead, AsyncWrite};
use futures::Async;

pub struct DummyIo;

impl Read for DummyIo {
fn read(&mut self, _: &mut [u8]) -> Result<usize, Error> {
unimplemented!();
}
}

impl Write for DummyIo {
fn write(&mut self, _: &[u8]) -> Result<usize, Error> {
unimplemented!();
}
fn flush(&mut self) -> Result<(), Error> {
unimplemented!();
}
}

impl AsyncRead for DummyIo {}

impl AsyncWrite for DummyIo {
fn shutdown(&mut self) -> Result<Async<()>, Error> {
unimplemented!();
}
}
}

0 comments on commit c1ccb92

Please sign in to comment.