refactor: extract MockSocket to cfg(any(test, fuzz)) module

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Davíð Steinn Geirsson 2026-03-25 21:59:04 +00:00
parent d7f630500d
commit 72e9ba292e

View file

@ -7,27 +7,23 @@ pub fn verify_descriptor(desc: &[u8]) {
assert_eq!(offset, desc.len()); assert_eq!(offset, desc.len());
} }
#[cfg(test)] #[cfg(any(test, feature = "fuzz"))]
pub(crate) mod tests { pub mod mock {
use std::{ use std::{
io::*, io::*,
net::SocketAddr,
pin::Pin, pin::Pin,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
task::{Context, Poll}, task::{Context, Poll},
}; };
use tokio::{ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
io::{AsyncRead, AsyncWrite, ReadBuf},
net::{TcpListener, TcpStream},
};
pub(crate) struct MockSocket { pub struct MockSocket {
pub input: Cursor<Vec<u8>>, pub input: Cursor<Vec<u8>>,
pub output: Arc<Mutex<Vec<u8>>>, pub output: Arc<Mutex<Vec<u8>>>,
} }
impl MockSocket { impl MockSocket {
pub(crate) fn new(input: Vec<u8>) -> Self { pub fn new(input: Vec<u8>) -> Self {
Self { Self {
input: Cursor::new(input), input: Cursor::new(input),
output: Arc::new(Mutex::new(vec![])), output: Arc::new(Mutex::new(vec![])),
@ -35,7 +31,7 @@ pub(crate) mod tests {
} }
/// Get a clone of the output buffer handle for reading after the socket is consumed. /// Get a clone of the output buffer handle for reading after the socket is consumed.
pub(crate) fn output_handle(&self) -> Arc<Mutex<Vec<u8>>> { pub fn output_handle(&self) -> Arc<Mutex<Vec<u8>>> {
self.output.clone() self.output.clone()
} }
} }
@ -51,7 +47,6 @@ pub(crate) mod tests {
} }
} }
#[cfg(test)]
impl AsyncWrite for MockSocket { impl AsyncWrite for MockSocket {
fn poll_write( fn poll_write(
self: Pin<&mut Self>, self: Pin<&mut Self>,
@ -70,6 +65,13 @@ pub(crate) mod tests {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
} }
}
#[cfg(test)]
pub(crate) mod tests {
pub(crate) use super::mock::MockSocket;
use std::net::SocketAddr;
use tokio::net::{TcpListener, TcpStream};
pub(crate) async fn get_free_address() -> SocketAddr { pub(crate) async fn get_free_address() -> SocketAddr {
let stream = TcpListener::bind("127.0.0.1:0").await.unwrap(); let stream = TcpListener::bind("127.0.0.1:0").await.unwrap();