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:
- Response-only mode: Returns predefined responses regardless of requests
- 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
was_called()- Check ifsend_and_receivewas invokedis_complete()- Check if all responses have been returnedassert_is_complete()- Panic if responses remain
§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>,
impl<P, T> SenderAndReceiverMock<P, T>where
P: PoolItem,
T: RequestWithResponse<P>,
Sourcepub fn new_with_expected_requests(
expected_requests: Vec<T>,
returned_responses: Vec<T::Response>,
) -> Self
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:
- Assert that each request matches the corresponding expected request
- Return the corresponding response
§Panics
- Panics if
expected_requests.len() != returned_responses.len() - Panics during
send_and_receiveif 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 }],
);Sourcepub fn new(returned_responses: Vec<T::Response>) -> Self
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 },
]);Sourcepub fn was_called(&self) -> bool
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.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Returns true if all predefined responses have been consumed.
This helps verify that all expected interactions occurred.
Sourcepub fn assert_is_complete(&self)
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>
impl<P, T> Debug for SenderAndReceiverMock<P, T>
Source§impl<P, T> Default for SenderAndReceiverMock<P, T>
impl<P, T> Default for SenderAndReceiverMock<P, T>
Source§fn default() -> SenderAndReceiverMock<P, T>
fn default() -> SenderAndReceiverMock<P, T>
Source§impl<T: RequestWithResponse<Randoms> + Send + Sync> InnerThreadPool for SenderAndReceiverMock<Randoms, T>
Implement InnerThreadPool for mock types to enable testing.
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>
type ThreadPool = SenderAndReceiverMock<Randoms, T>
SenderAndReceiver<Randoms>