in utils/src/singleflight.rs [423:454]
fn test_multiple_threads_with_threadpool() {
let times_called = Arc::new(AtomicU32::new(0));
let threadpool = Arc::new(ThreadPool::new().unwrap());
let g: Arc<Group<usize, ()>> = Arc::new(Group::new());
let mut handlers: Vec<JoinHandle<(usize, bool)>> = Vec::new();
let threadpool_ = threadpool.clone();
let tasks = async move {
for _ in 0..10 {
let g = g.clone();
let counter = times_called.clone();
handlers.push(threadpool_.spawn(async move {
let tup = g.work("key", expensive_fn(counter, RES)).await;
let res = tup.0;
let fn_response = res.unwrap();
(fn_response, tup.1)
}));
}
let num_callers = join_all(handlers)
.await
.into_iter()
.map(|r| r.unwrap())
.filter(|(val, is_caller)| {
assert_eq!(*val, RES);
*is_caller
})
.count();
assert_eq!(1, num_callers);
assert_eq!(1, times_called.load(Ordering::SeqCst));
};
let _ = threadpool.external_run_async_task(tasks).unwrap();
}