Skip to content

Commit

Permalink
[test_fixtures] Add fixtures to mock sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmulidris committed Nov 22, 2024
1 parent a6aefd8 commit 51a599d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
/// - The difference is that [MockAsyncStream] allows access to the expected write buffer.
pub struct MockAsyncStream {
pub expected_buffer: Vec<u8>,
}

/// 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<Result<usize>> {
self.expected_write.extend_from_slice(buf);
self.expected_buffer.extend_from_slice(buf);
Poll::Ready(Ok(buf.len()))
}

Expand All @@ -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<std::result::Result<(), std::io::Error>> {
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(()))
}
}
47 changes: 47 additions & 0 deletions test_fixtures/src/tcp_stream_fixtures/mock_socket.rs
Original file line number Diff line number Diff line change
@@ -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<DuplexStream>,
pub client_write: WriteHalf<DuplexStream>,
pub server_read: ReadHalf<DuplexStream>,
pub server_write: WriteHalf<DuplexStream>,
}

/// 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,
}
}
6 changes: 4 additions & 2 deletions test_fixtures/src/tcp_stream_fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

0 comments on commit 51a599d

Please sign in to comment.