Fix all tests

This commit is contained in:
Arne Beer 2020-10-15 20:56:35 +02:00
parent 484bbf7b15
commit c3e0fd59e4
3 changed files with 19 additions and 10 deletions

View file

@ -43,18 +43,26 @@ pub fn default_pueue_path() -> Result<String> {
mod tests {
use super::*;
use std::fs::File;
use std::fs::{remove_file, File};
use std::io::prelude::*;
use anyhow::Result;
#[test]
fn test_spawn_command() -> Result<()> {
// Path can be found
fn test_create_unix_socket() -> Result<()> {
let path = get_unix_socket_path()?;
let mut file = File::create(path)?;
file.write_all(b"Hello, world!")?;
// If pueue is currently running on the system,
// simply accept that we found the correct path
if PathBuf::from(&path).exists() {
return Ok(());
}
// Otherwise try to create it and write to it
let mut file = File::create(&path)?;
assert!(file.write_all(b"Hello, world!").is_ok());
remove_file(&path)?;
Ok(())
}

View file

@ -13,7 +13,7 @@ use log::debug;
use crate::message::*;
#[async_trait]
pub trait GenericListener {
pub trait GenericListener: Sync + Send {
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>>;
}

View file

@ -1,6 +1,5 @@
use anyhow::Result;
use async_std::net::{TcpListener, TcpStream};
use async_std::prelude::*;
use async_std::task;
use pueue::message::{create_success_message, Message};
@ -16,13 +15,14 @@ async fn test_single_huge_payload() -> Result<()> {
let message = create_success_message(payload);
let original_bytes = bincode::serialize(&message).expect("Failed to serialize message.");
let listener: Box<dyn GenericListener> = Box::new(listener);
// Spawn a sub thread that:
// 1. Accepts a new connection
// 2. Reads a message
// 3. Sends the same message back
task::spawn(async move {
let mut incoming = listener.incoming();
let mut socket = incoming.next().await.unwrap().unwrap();
let mut socket = listener.accept().await.unwrap();
let message_bytes = receive_bytes(&mut socket).await.unwrap();
let message: Message = bincode::deserialize(&message_bytes).unwrap();
@ -30,7 +30,8 @@ async fn test_single_huge_payload() -> Result<()> {
send_message(message, &mut socket).await.unwrap();
});
let mut client = TcpStream::connect(&addr).await?;
let mut client: Box<dyn GenericSocket> = Box::new(TcpStream::connect(&addr).await?);
// Create a client that sends a message and instantly receives it
send_message(message, &mut client).await?;
let response_bytes = receive_bytes(&mut client).await?;