diff --git a/test_fixtures/src/tcp_stream_fixtures/mock_tcp_stream.rs b/test_fixtures/src/tcp_stream_fixtures/mock_async_stream.rs similarity index 69% rename from test_fixtures/src/tcp_stream_fixtures/mock_tcp_stream.rs rename to test_fixtures/src/tcp_stream_fixtures/mock_async_stream.rs index b29fb56c..d0697200 100644 --- a/test_fixtures/src/tcp_stream_fixtures/mock_tcp_stream.rs +++ b/test_fixtures/src/tcp_stream_fixtures/mock_async_stream.rs @@ -23,19 +23,20 @@ use tokio::io::{AsyncRead, AsyncWrite}; /// A mock struct for the [tokio::net::TcpStream]. /// - Alternative to [tokio_test::io::Builder]. -/// - The difference is that [MockTcpStream] allows access to the expected write buffer. -pub struct MockTcpStream { - pub expected_write: Vec, +/// - The difference is that [MockAsyncStream] allows access to the expected write buffer. +pub struct MockAsyncStream { + pub expected_buffer: Vec, } -/// Implement the [AsyncWrite] trait for the mock struct. -impl AsyncWrite for MockTcpStream { +/// Implement the [AsyncWrite] trait for the mock struct. This struct also automatically +/// implements [Unpin], because it contains no self-referencing pointers. +impl AsyncWrite for MockAsyncStream { fn poll_write( mut self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8], ) -> Poll> { - self.expected_write.extend_from_slice(buf); + self.expected_buffer.extend_from_slice(buf); Poll::Ready(Ok(buf.len())) } @@ -48,17 +49,18 @@ impl AsyncWrite for MockTcpStream { } } -/// Implement the [AsyncRead] trait for the mock struct. -impl AsyncRead for MockTcpStream { +/// Implement the [AsyncRead] trait for the mock struct. This struct also automatically +/// implements [Unpin], because it contains no self-referencing pointers. +impl AsyncRead for MockAsyncStream { fn poll_read( mut self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut tokio::io::ReadBuf<'_>, ) -> std::task::Poll> { - let data = self.expected_write.as_slice(); + let data = self.expected_buffer.as_slice(); let len = std::cmp::min(data.len(), buf.remaining()); buf.put_slice(&data[..len]); - self.expected_write.drain(..len); + self.expected_buffer.drain(..len); Poll::Ready(Ok(())) } } diff --git a/test_fixtures/src/tcp_stream_fixtures/mock_socket.rs b/test_fixtures/src/tcp_stream_fixtures/mock_socket.rs new file mode 100644 index 00000000..eb29edb4 --- /dev/null +++ b/test_fixtures/src/tcp_stream_fixtures/mock_socket.rs @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 R3BL LLC + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use tokio::io::{duplex, split, DuplexStream, ReadHalf, WriteHalf}; + +pub struct MockSocket { + pub client_read: ReadHalf, + pub client_write: WriteHalf, + pub server_read: ReadHalf, + pub server_write: WriteHalf, +} + +/// A “channel” is created by [tokio::io::duplex] that can be used as in-memory IO +/// types. +/// +/// Given a "channel": +/// 1. Writing to the first of the pairs will allow that data to be read from the +/// other. +/// 2. Writing to the other pair will allow that data to be read from the first. +pub fn get_mock_socket_halves() -> MockSocket { + let (client_stream, server_stream) = duplex(1024); + + let (client_read, client_write) = split(client_stream); + + let (server_read, server_write) = split(server_stream); + + MockSocket { + client_read, + client_write, + server_read, + server_write, + } +} diff --git a/test_fixtures/src/tcp_stream_fixtures/mod.rs b/test_fixtures/src/tcp_stream_fixtures/mod.rs index 1a94e446..fe297e46 100644 --- a/test_fixtures/src/tcp_stream_fixtures/mod.rs +++ b/test_fixtures/src/tcp_stream_fixtures/mod.rs @@ -16,7 +16,9 @@ */ // Attach. -pub mod mock_tcp_stream; +pub mod mock_async_stream; +pub mod mock_socket; // Re-export. -pub use mock_tcp_stream::*; +pub use mock_async_stream::*; +pub use mock_socket::*; \ No newline at end of file