Skip to content

Commit

Permalink
sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi321 committed Jun 18, 2023
1 parent 2f2adf8 commit 8d4204e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
7 changes: 3 additions & 4 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use std::{sync::Arc, time::Duration};
const FRAMEBUFFER_WIDTH: usize = 1920;
const FRAMEBUFFER_HEIGHT: usize = 1080;

async fn invoke_parse_pixelflut_commands(
fn invoke_parse_pixelflut_commands(
input: &[u8],
fb: &Arc<FrameBuffer>,
parser_state: ParserState,
) {
let mut stream = DevNullTcpStream::default();
parse_pixelflut_commands(input, fb, &mut stream, parser_state).await;
parse_pixelflut_commands(input, fb, &mut stream, parser_state);
}

fn from_elem(c: &mut Criterion) {
Expand All @@ -33,8 +33,7 @@ fn from_elem(c: &mut Criterion) {
|b, input| {
let fb = Arc::new(FrameBuffer::new(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT));
let parser_state = ParserState::default();
b.to_async(tokio::runtime::Runtime::new().unwrap())
.iter(|| invoke_parse_pixelflut_commands(input, &fb, parser_state.clone()));
b.iter(|| invoke_parse_pixelflut_commands(input, &fb, parser_state.clone()));
},
);

Expand Down
38 changes: 17 additions & 21 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod test {
use crate::test::helpers::MockTcpStream;
use rstest::{fixture, rstest};
use std::time::Duration;
use tokio::sync::mpsc::{self, Receiver};
use std::sync::mpsc::{self, Receiver};

#[fixture]
fn ip() -> IpAddr {
Expand All @@ -192,7 +192,7 @@ mod test {

#[fixture]
fn statistics_channel() -> (Sender<StatisticsEvent>, Receiver<StatisticsEvent>) {
mpsc::channel(10000)
mpsc::channel()
}

#[rstest]
Expand All @@ -208,16 +208,16 @@ mod test {
#[case("HELP", std::str::from_utf8(crate::parser::HELP_TEXT).unwrap())]
#[case("HELP\n", std::str::from_utf8(crate::parser::HELP_TEXT).unwrap())]
#[case("bla bla bla\nSIZE\nblub\nbla", "SIZE 1920 1080\n")]
#[tokio::test]
async fn test_correct_responses_to_general_commands(
#[test]
fn test_correct_responses_to_general_commands(
#[case] input: &str,
#[case] expected: &str,
ip: IpAddr,
fb: Arc<FrameBuffer>,
statistics_channel: (Sender<StatisticsEvent>, Receiver<StatisticsEvent>),
) {
let mut stream = MockTcpStream::from_input(input);
handle_connection(&mut stream, ip, fb, statistics_channel.0).await;
handle_connection(&mut stream, ip, fb, statistics_channel.0);

assert_eq!(expected, stream.get_output());
}
Expand Down Expand Up @@ -262,32 +262,32 @@ mod test {
"PX 0 0 ffffff\nPX 42 42 000000\n"
)] // The get pixel result is also offseted
#[case("OFFSET 0 0\nPX 0 42 abcdef\nPX 0 42\n", "PX 0 42 abcdef\n")]
#[tokio::test]
async fn test_setting_pixel(
#[test]
fn test_setting_pixel(
#[case] input: &str,
#[case] expected: &str,
ip: IpAddr,
fb: Arc<FrameBuffer>,
statistics_channel: (Sender<StatisticsEvent>, Receiver<StatisticsEvent>),
) {
let mut stream = MockTcpStream::from_input(input);
handle_connection(&mut stream, ip, fb, statistics_channel.0).await;
handle_connection(&mut stream, ip, fb, statistics_channel.0);

assert_eq!(expected, stream.get_output());
}

#[rstest]
#[case("PX 0 0 aaaaaa\n")]
#[case("PX 0 0 aa\n")]
#[tokio::test]
async fn test_safe(
#[test]
fn test_safe(
#[case] input: &str,
ip: IpAddr,
fb: Arc<FrameBuffer>,
statistics_channel: (Sender<StatisticsEvent>, Receiver<StatisticsEvent>),
) {
let mut stream = MockTcpStream::from_input(input);
handle_connection(&mut stream, ip, fb.clone(), statistics_channel.0).await;
handle_connection(&mut stream, ip, fb.clone(), statistics_channel.0);
// Test if it panics
assert_eq!(fb.get(0, 0).unwrap() & 0x00ff_ffff, 0xaaaaaa);
}
Expand All @@ -308,8 +308,8 @@ mod test {
#[case(500, 500, 300, 400)]
#[case(fb().get_width(), fb().get_height(), 0, 0)]
#[case(fb().get_width() - 1, fb().get_height() - 1, 1, 1)]
#[tokio::test]
async fn test_drawing_rect(
#[test]
fn test_drawing_rect(
#[case] width: usize,
#[case] height: usize,
#[case] offset_x: usize,
Expand Down Expand Up @@ -354,8 +354,7 @@ mod test {
ip,
Arc::clone(&fb),
statistics_channel.0.clone(),
)
.await;
);
assert_eq!("", stream.get_output());

// Read the pixels again
Expand All @@ -365,8 +364,7 @@ mod test {
ip,
Arc::clone(&fb),
statistics_channel.0.clone(),
)
.await;
);
assert_eq!(fill_commands, stream.get_output());

// We can also do coloring and reading in a single connection
Expand All @@ -376,8 +374,7 @@ mod test {
ip,
Arc::clone(&fb),
statistics_channel.0.clone(),
)
.await;
);
assert_eq!(combined_commands_expected, stream.get_output());

// Check that nothing else was colored
Expand All @@ -387,8 +384,7 @@ mod test {
ip,
Arc::clone(&fb),
statistics_channel.0.clone(),
)
.await;
);
assert_eq!(read_other_pixels_commands_expected, stream.get_output());
}
}

0 comments on commit 8d4204e

Please sign in to comment.