use std::sync::Arc;
use messaging_thread_pool::{id_provider::id_provider_mutex::IdProviderMutex, samples::*, *};
#[test]
pub fn example_random_batches_() {
let randoms_batch_thread_pool = ThreadPool::<RandomsBatch<RandomsThreadPool>>::new(3);
let randoms_thread_pool = Arc::new(ThreadPool::<Randoms>::new(6));
let id_provider = Arc::new(IdProviderMutex::new(0));
randoms_batch_thread_pool
.send_and_receive((0..10).map(|id| RandomsBatchAddRequest {
id,
number_of_contained_randoms: 100,
id_provider: id_provider.clone(),
randoms_thread_pool: Arc::clone(&randoms_thread_pool),
}))
.expect("thread pool to be available")
.for_each(|response: AddResponse| assert!(response.result().is_ok()));
let sum_of_sums: Vec<u128> = randoms_batch_thread_pool
.send_and_receive((0..10).map(|id| SumOfSumsRequest(id, std::marker::PhantomData)))
.expect("thread pool to be available")
.map(|response: SumOfSumsResponse<RandomsThreadPool>| response.result)
.collect();
dbg!(sum_of_sums);
}
#[test]
pub fn example_random_batches_with_mock_thread_pool() {
let id_provider = Arc::new(IdProviderMutex::new(0));
let randoms_thread_pool =
SenderAndReceiverMock::<Randoms, SumRequest>::new_with_expected_requests(
vec![SumRequest(2), SumRequest(4)],
vec![
SumResponse { id: 2, result: 2 },
SumResponse { id: 4, result: 4 },
],
);
let target: RandomsBatch<SenderAndReceiverMock<_, _>> = RandomsBatch {
id: 1,
contained_random_ids: vec![2, 4],
id_provider,
randoms_thread_pool: Arc::new(randoms_thread_pool),
};
let result = target.sum_of_sums();
assert_eq!(6, result);
}