Skip to content

Commit ef8b8da

Browse files
Michael HsuMiniaczQ
authored andcommitted
copy test from bevyengine#4343
Co-authored-by: MiniaczQ <[email protected]>
1 parent 716bdcb commit ef8b8da

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

crates/bevy_tasks/src/task_pool.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,43 @@ mod tests {
406406
assert!(!thread_check_failed.load(Ordering::Acquire));
407407
assert_eq!(count.load(Ordering::Acquire), 200);
408408
}
409+
410+
#[test]
411+
fn test_nested_spawn() {
412+
let pool = TaskPool::new();
413+
414+
let foo = Box::new(42);
415+
let foo = &*foo;
416+
417+
let count = Arc::new(AtomicI32::new(0));
418+
419+
let outputs: Vec<i32> = pool.scope(|scope| {
420+
for _ in 0..10 {
421+
let count_clone = count.clone();
422+
let scope = scope.clone();
423+
scope.clone().spawn(async move {
424+
for _ in 0..10 {
425+
let count_clone_clone = count_clone.clone();
426+
scope.spawn(async move {
427+
if *foo != 42 {
428+
panic!("not 42!?!?")
429+
} else {
430+
count_clone_clone.fetch_add(1, Ordering::Relaxed);
431+
*foo
432+
}
433+
});
434+
}
435+
*foo
436+
});
437+
}
438+
}).collect();
439+
440+
for output in &outputs {
441+
assert_eq!(*output, 42);
442+
}
443+
444+
// the inner loop runs 100 times and the outer one runs 10. 100 + 10
445+
assert_eq!(outputs.len(), 110);
446+
assert_eq!(count.load(Ordering::Relaxed), 100);
447+
}
409448
}

0 commit comments

Comments
 (0)