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());
}
#[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<Vec<u8>>,
pub output: Arc<Mutex<Vec<u8>>>,
}
impl MockSocket {
pub(crate) fn new(input: Vec<u8>) -> Self {
pub fn new(input: Vec<u8>) -> 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<Mutex<Vec<u8>>> {
pub fn output_handle(&self) -> Arc<Mutex<Vec<u8>>> {
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();