SenderAndReceiverMock

Struct SenderAndReceiverMock 

Source
pub struct SenderAndReceiverMock<P, T>
where P: PoolItem, T: RequestWithResponse<P>,
{ /* private fields */ }
Expand description

A mock implementation of SenderAndReceiver for testing.

This mock allows you to test code that depends on thread pools without actually spawning threads. It provides two modes of operation:

  1. Response-only mode: Returns predefined responses regardless of requests
  2. Request verification mode: Asserts that requests match expectations

§Example: Response-Only Mode

Use this when you only care about the responses, not the exact requests:

use messaging_thread_pool::{SenderAndReceiver, SenderAndReceiverMock, samples::*};

// Create mock that returns predefined responses
let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![
    MeanResponse { id: 1, result: 100 },
    MeanResponse { id: 2, result: 200 },
]);

// Any requests will return the predefined responses in order
let responses: Vec<_> = mock
    .send_and_receive([MeanRequest(1), MeanRequest(2)].into_iter())
    .unwrap()
    .collect();

assert_eq!(responses[0].result, 100);
assert_eq!(responses[1].result, 200);

§Example: Request Verification Mode

Use this when you want to verify the exact requests being sent:

use messaging_thread_pool::{SenderAndReceiver, SenderAndReceiverMock, samples::*};

// Create mock with expected requests and responses
let mock = SenderAndReceiverMock::<Randoms, SumRequest>::new_with_expected_requests(
    vec![SumRequest(1), SumRequest(2)],  // Expected requests
    vec![
        SumResponse { id: 1, result: 100 },
        SumResponse { id: 2, result: 200 },
    ],
);

// The mock will assert that requests match expectations
let responses: Vec<_> = mock
    .send_and_receive([SumRequest(1), SumRequest(2)].into_iter())
    .unwrap()
    .collect();

// Verify all responses were consumed
mock.assert_is_complete();

§Verification Methods

§Type Parameters

  • P - The pool item type (e.g., Randoms)
  • T - The request type (e.g., MeanRequest)

The mock can return responses for any request type that maps to the same pool item, but verification mode requires the request types to match.

Implementations§

Source§

impl<P, T> SenderAndReceiverMock<P, T>
where P: PoolItem, T: RequestWithResponse<P>,

Source

pub fn new_with_expected_requests( expected_requests: Vec<T>, returned_responses: Vec<T::Response>, ) -> Self

Creates a mock that verifies requests match expectations.

When send_and_receive is called, the mock will:

  1. Assert that each request matches the corresponding expected request
  2. Return the corresponding response
§Panics
  • Panics if expected_requests.len() != returned_responses.len()
  • Panics during send_and_receive if a request doesn’t match expectations
§Example
use messaging_thread_pool::{SenderAndReceiverMock, samples::*};

let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
    vec![MeanRequest(1)],
    vec![MeanResponse { id: 1, result: 42 }],
);
Source

pub fn new(returned_responses: Vec<T::Response>) -> Self

Creates a mock that returns predefined responses without verifying requests.

This is useful when you only care about the returned values and don’t need to verify the exact requests being sent.

§Example
use messaging_thread_pool::{SenderAndReceiverMock, samples::*};

let mock = SenderAndReceiverMock::<Randoms, SumRequest>::new(vec![
    SumResponse { id: 1, result: 100 },
    SumResponse { id: 2, result: 200 },
]);
Source

pub fn was_called(&self) -> bool

Returns true if send_and_receive has been called at least once.

Useful for verifying that your code actually used the mock.

Source

pub fn is_complete(&self) -> bool

Returns true if all predefined responses have been consumed.

This helps verify that all expected interactions occurred.

Source

pub fn assert_is_complete(&self)

Asserts that all predefined responses have been consumed.

§Panics

Panics if any responses remain unconsumed, indicating that fewer requests were made than expected.

Trait Implementations§

Source§

impl<P, T> Debug for SenderAndReceiverMock<P, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P, T> Default for SenderAndReceiverMock<P, T>

Source§

fn default() -> SenderAndReceiverMock<P, T>

Returns the “default value” for a type. Read more
Source§

impl<T: RequestWithResponse<Randoms> + Send + Sync> InnerThreadPool for SenderAndReceiverMock<Randoms, T>

Implement InnerThreadPool for mock types to enable testing.

Source§

type ThreadPool = SenderAndReceiverMock<Randoms, T>

The concrete thread pool type that implements SenderAndReceiver<Randoms>
Source§

impl<P, T1> SenderAndReceiver<P> for SenderAndReceiverMock<P, T1>

Source§

fn send_and_receive<'a, T>( &'a self, requests: impl Iterator<Item = T> + 'a, ) -> Result<Box<dyn Iterator<Item = T::Response> + 'a>, SendError<SenderCouplet<P>>>
where T: RequestWithResponse<P> + 'a,

Send multiple requests and receive their responses. Read more
Source§

fn send_and_receive_one<'a, T>( &'a self, request: T, ) -> Result<T::Response, SendError<SenderCouplet<P>>>
where T: RequestWithResponse<P> + IdTargeted + 'a,

Convenience method for sending a single request and receiving its response. Read more

Auto Trait Implementations§

§

impl<P, T> !Freeze for SenderAndReceiverMock<P, T>

§

impl<P, T> RefUnwindSafe for SenderAndReceiverMock<P, T>

§

impl<P, T> Send for SenderAndReceiverMock<P, T>
where T: Send, <T as RequestWithResponse<P>>::Response: Send,

§

impl<P, T> Sync for SenderAndReceiverMock<P, T>
where T: Send, <T as RequestWithResponse<P>>::Response: Send,

§

impl<P, T> Unpin for SenderAndReceiverMock<P, T>
where T: Unpin, <T as RequestWithResponse<P>>::Response: Unpin,

§

impl<P, T> UnwindSafe for SenderAndReceiverMock<P, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more