From 72e9ba292e77423c6fd56c3a7bf1626fddc30871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=AD=C3=B0=20Steinn=20Geirsson?= Date: Wed, 25 Mar 2026 21:59:04 +0000 Subject: [PATCH] refactor: extract MockSocket to cfg(any(test, fuzz)) module Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/src/util.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/src/util.rs b/lib/src/util.rs index 14c872b..f7e50c0 100644 --- a/lib/src/util.rs +++ b/lib/src/util.rs @@ -7,27 +7,23 @@ pub fn verify_descriptor(desc: &[u8]) { assert_eq!(offset, desc.len()); } -#[cfg(test)] -pub(crate) mod tests { +#[cfg(any(test, feature = "fuzz"))] +pub mod mock { use std::{ io::*, - net::SocketAddr, pin::Pin, sync::{Arc, Mutex}, task::{Context, Poll}, }; - use tokio::{ - io::{AsyncRead, AsyncWrite, ReadBuf}, - net::{TcpListener, TcpStream}, - }; + use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; - pub(crate) struct MockSocket { + pub struct MockSocket { pub input: Cursor>, pub output: Arc>>, } impl MockSocket { - pub(crate) fn new(input: Vec) -> Self { + pub fn new(input: Vec) -> Self { Self { input: Cursor::new(input), 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. - pub(crate) fn output_handle(&self) -> Arc>> { + pub fn output_handle(&self) -> Arc>> { self.output.clone() } } @@ -51,7 +47,6 @@ pub(crate) mod tests { } } - #[cfg(test)] impl AsyncWrite for MockSocket { fn poll_write( self: Pin<&mut Self>, @@ -70,6 +65,13 @@ pub(crate) mod tests { 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 { let stream = TcpListener::bind("127.0.0.1:0").await.unwrap();