Skip to content

Commit e42acb4

Browse files
author
Michael Hsu
committed
copy test from bevyengine#4343
Co-authored-by: MiniaczQ<[email protected]>
1 parent 1fc63a9 commit e42acb4

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
@@ -424,4 +424,43 @@ mod tests {
424424
assert!(!thread_check_failed.load(Ordering::Acquire));
425425
assert_eq!(count.load(Ordering::Acquire), 200);
426426
}
427+
428+
#[test]
429+
fn test_nested_spawn() {
430+
let pool = TaskPool::new();
431+
432+
let foo = Box::new(42);
433+
let foo = &*foo;
434+
435+
let count = Arc::new(AtomicI32::new(0));
436+
437+
let outputs: Vec<i32> = pool.scope(|scope| {
438+
for _ in 0..10 {
439+
let count_clone = count.clone();
440+
let scope = scope.clone();
441+
scope.clone().spawn(async move {
442+
for _ in 0..10 {
443+
let count_clone_clone = count_clone.clone();
444+
scope.spawn(async move {
445+
if *foo != 42 {
446+
panic!("not 42!?!?")
447+
} else {
448+
count_clone_clone.fetch_add(1, Ordering::Relaxed);
449+
*foo
450+
}
451+
});
452+
}
453+
*foo
454+
});
455+
}
456+
}).collect();
457+
458+
for output in &outputs {
459+
assert_eq!(*output, 42);
460+
}
461+
462+
// the inner loop runs 100 times and the outer one runs 10. 100 + 10
463+
assert_eq!(outputs.len(), 110);
464+
assert_eq!(count.load(Ordering::Relaxed), 100);
465+
}
427466
}

0 commit comments

Comments
 (0)