messaging_thread_pool 5.0.3

A library for aiding the creation of typed thread pool of objects that is communicated with via channels
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::fmt::Debug;
use std::sync::Mutex;

use crossbeam_channel::SendError;

use crate::{
    pool_item::PoolItem, request_with_response::RequestWithResponse, sender_couplet::SenderCouplet,
    thread_request_response::ThreadRequestResponse,
};

use super::SenderAndReceiver;

/// 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:
///
/// ```rust
/// 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:
///
/// ```rust
/// 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()`](Self::was_called) - Check if `send_and_receive` was invoked
/// - [`is_complete()`](Self::is_complete) - Check if all responses have been returned
/// - [`assert_is_complete()`](Self::assert_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.
#[derive(Debug, Default)]
pub struct SenderAndReceiverMock<P, T>
where
    P: PoolItem,
    T: RequestWithResponse<P>,
{
    assert_requests_equal: bool,
    was_called: Mutex<bool>,
    expected_requests: Mutex<Vec<T>>,
    returned_responses: Mutex<Vec<T::Response>>,
}

impl<P, T> SenderAndReceiverMock<P, T>
where
    P: PoolItem,
    T: RequestWithResponse<P>,
{
    /// 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
    ///
    /// ```rust
    /// use messaging_thread_pool::{SenderAndReceiverMock, samples::*};
    ///
    /// let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
    ///     vec![MeanRequest(1)],
    ///     vec![MeanResponse { id: 1, result: 42 }],
    /// );
    /// ```
    pub fn new_with_expected_requests(
        expected_requests: Vec<T>,
        returned_responses: Vec<T::Response>,
    ) -> Self {
        assert_eq!(
            expected_requests.len(),
            returned_responses.len(),
            "number of requests do not match number of responses"
        );
        Self {
            was_called: Mutex::new(false),
            assert_requests_equal: true,
            expected_requests: Mutex::new(expected_requests),
            returned_responses: Mutex::new(returned_responses),
        }
    }

    /// 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
    ///
    /// ```rust
    /// use messaging_thread_pool::{SenderAndReceiverMock, samples::*};
    ///
    /// let mock = SenderAndReceiverMock::<Randoms, SumRequest>::new(vec![
    ///     SumResponse { id: 1, result: 100 },
    ///     SumResponse { id: 2, result: 200 },
    /// ]);
    /// ```
    pub fn new(returned_responses: Vec<T::Response>) -> Self {
        Self {
            was_called: Mutex::new(false),
            assert_requests_equal: false,
            expected_requests: Mutex::new(vec![]),
            returned_responses: Mutex::new(returned_responses),
        }
    }

    /// Returns `true` if `send_and_receive` has been called at least once.
    ///
    /// Useful for verifying that your code actually used the mock.
    pub fn was_called(&self) -> bool {
        *self.was_called.lock().expect("that lock will never fail")
    }

    /// Returns `true` if all predefined responses have been consumed.
    ///
    /// This helps verify that all expected interactions occurred.
    pub fn is_complete(&self) -> bool {
        self.returned_responses
            .lock()
            .expect("that the lock will never fail")
            .is_empty()
    }

    /// Asserts that all predefined responses have been consumed.
    ///
    /// # Panics
    ///
    /// Panics if any responses remain unconsumed, indicating that fewer
    /// requests were made than expected.
    pub fn assert_is_complete(&self) {
        let unreturned_responses = self
            .returned_responses
            .lock()
            .expect("that the lock will never fail")
            .len();
        assert!(
            unreturned_responses == 0,
            "expected all responses to have been consumed, {} remaining",
            unreturned_responses
        );
    }
}

impl<P, T1> SenderAndReceiver<P> for SenderAndReceiverMock<P, T1>
where
    P: PoolItem + PartialEq,
    P::Api: PartialEq,
    P::Init: PartialEq,
    T1: RequestWithResponse<P>,
{
    #[allow(clippy::needless_collect)]
    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,
    {
        // materialize requests to establish len
        let requests: Vec<T> = requests.into_iter().collect();
        let actual_count = requests.len();
        match self.was_called.lock() {
            Ok(mut result) => *result = true,
            _ => panic!(),
        }

        if self.assert_requests_equal {
            let expected_count = self.expected_requests.lock().unwrap().iter().count();
            assert!(
                expected_count >= actual_count,
                "count of expected [{expected_count}] less than actual requests [{actual_count}]"
            );
            self.expected_requests
                .lock()
                .unwrap()
                .drain(..actual_count)
                .map(|r| <T1 as Into<ThreadRequestResponse<P>>>::into(r))
                .zip(
                    requests
                        .into_iter()
                        .map(|r| <T as Into<ThreadRequestResponse<P>>>::into(r)),
                )
                // assert individually to get better error messages
                .for_each(|(expected, actual)| {
                    assert_eq!(expected, actual, "expected and actual requests differ")
                });
        }

        // convert type U1 to U via the intermediary ThreadRequestResponse
        let mut returned_responses = self.returned_responses.lock().unwrap();
        let available_count = returned_responses.len().min(actual_count);

        let results: Vec<_> = returned_responses
            .drain(..available_count)
            .map(<T1::Response as Into<ThreadRequestResponse<P>>>::into)
            .map(<T::Response as From<ThreadRequestResponse<P>>>::from)
            .collect();

        Ok(Box::new(results.into_iter()))
    }
}

#[cfg(test)]
mod tests {

    use std::hint::black_box;

    use crate::{
        samples::{MeanRequest, MeanResponse, Randoms},
        sender_and_receiver::SenderAndReceiver,
    };

    use super::SenderAndReceiverMock;

    #[test]
    fn check_mock_send_and_sync() {
        // enforce that the mock is send and sync when the request is
        let target = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![]);
        send_and_sync(target);
    }

    fn send_and_sync<T>(_check_me: T)
    where
        T: Send + Sync,
    {
        black_box(())
    }

    #[test]
    fn two_responses_only_one_returned_is_complete_returns_false() {
        let response_0 = MeanResponse { id: 1, result: 22 };
        let response_1 = MeanResponse { id: 2, result: 44 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![
            response_0.clone(),
            response_1.clone(),
        ]);
        assert!(!mock.was_called());

        let _results_0: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(1)].into_iter())
            .unwrap()
            .collect();

        assert!(!mock.is_complete());
    }

    #[test]
    fn two_responses_returned_over_multiple_requests() {
        let response_0 = MeanResponse { id: 1, result: 22 };
        let response_1 = MeanResponse { id: 2, result: 44 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![
            response_0.clone(),
            response_1.clone(),
        ]);
        assert!(!mock.was_called());

        let results_0: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(1)].into_iter())
            .unwrap()
            .collect();
        let results_1: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(2)].into_iter())
            .unwrap()
            .collect();

        assert!(mock.is_complete());
        assert_eq!(1, results_0.len());
        assert_eq!(response_0, results_0[0]);
        assert_eq!(1, results_1.len());
        assert_eq!(response_1, results_1[0]);
    }

    #[test]
    #[should_panic]
    fn one_expected_request_differs_from_one_actual_request() {
        let request_0 = MeanRequest(1);
        let response_0 = MeanResponse { id: 1, result: 22 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
            vec![MeanRequest(2)],
            vec![response_0],
        );

        let _results: Vec<MeanResponse> = mock
            .send_and_receive(vec![request_0].into_iter())
            .unwrap()
            .collect();
        assert!(mock.was_called());
    }

    #[test]
    #[should_panic(expected = "count of expected [1] less than actual requests [2]")]
    fn one_expected_request_actual_requests_2_panics() {
        let request_0 = MeanRequest(1);
        let response_0 = MeanResponse { id: 1, result: 22 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
            vec![request_0],
            vec![response_0],
        );

        let _results: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(1), MeanRequest(2)].into_iter())
            .unwrap()
            .collect();
        assert!(mock.was_called());
    }

    #[test]
    fn empty_requests_and_responses_does_not_panic() {
        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
            vec![],
            vec![],
        );

        let _results: Vec<MeanResponse> = mock
            .send_and_receive(Vec::<MeanRequest>::default().into_iter())
            .unwrap()
            .collect();
        assert!(mock.was_called());
    }

    #[test]
    #[should_panic(expected = "number of requests do not match number of responses")]
    fn unmatched_requests_and_responses() {
        let response_0 = MeanResponse { id: 1, result: 22 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new_with_expected_requests(
            vec![],
            vec![response_0],
        );

        let _results: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(1)].into_iter())
            .unwrap()
            .collect();
    }

    #[test]
    fn one_response_only_returns_expected_response() {
        let response_0 = MeanResponse { id: 1, result: 22 };

        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![response_0.clone()]);

        let results: Vec<MeanResponse> = mock
            .send_and_receive(vec![MeanRequest(1)].into_iter())
            .unwrap()
            .collect();

        assert_eq!(1, results.len());
        assert_eq!(response_0, results[0]);
        assert!(mock.was_called());
    }

    #[test]
    fn one_response_empty_requests_returns_empty_iterator() {
        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![MeanResponse {
            id: 1,
            result: 0,
        }]);

        let results: Vec<MeanResponse> = mock
            .send_and_receive(Vec::<MeanRequest>::default().into_iter())
            .unwrap()
            .collect();

        assert_eq!(0, results.len());
        assert!(mock.was_called());
    }

    #[test]
    fn zero_responses_returns_empty_iterator() {
        let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(vec![]);

        let results: Vec<MeanResponse> = mock
            .send_and_receive(Vec::<MeanRequest>::default().into_iter())
            .unwrap()
            .collect();

        assert_eq!(0, results.len());
        assert!(mock.was_called());
    }
}