Summary
I have an application that starts another process and transfers its StdOut/StdErr to a log file using the log
crate. My application transfers the output line by line (buf_read.read_line()
). As it can be any arbitrary process, my application makes the assumption that the other process may be malicious and may try to print to stdout/sterr enormous amounts of data without a single newline, thus causing OOM in my application. Hence my application limits the number of bytes the BufReader
can read at a time using BufReader.take()
.
The problem
Ignoring all the details about chunking the input, how can I test that my logger was called X times with the correct parameters ? Let's assume my app has read one huge line and has split it in 3 parts like the MCVE below.
MCVE:
use std::thread::JoinHandle;
fn main() {
let handle = start_transfer_thread(&|x| {
println!("X={}", x);
}).join();
}
fn start_transfer_thread<F>(logger: &'static F) -> JoinHandle<()> where F: Send + Sync + Fn(&str) -> () {
std::thread::spawn(move || {
logger("1");
logger("2");
logger("3");
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_logged_in_order() {
let result = start_transfer_thread(&|x| {
match x {
"1" => (),
"2" => (),
"3" => (),
x => panic!("unexpected token: {}", x)
}
}).join();
assert!(result.is_ok());
}
}