Skip to content

Commit a03ea76

Browse files
committed
copy test from bevyengine#4343
Co-authored-by: MiniaczQ<[email protected]>
1 parent 63dc9f4 commit a03ea76

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

crates/bevy_tasks/src/task_pool.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,39 @@ mod tests {
419419
#[test]
420420
fn test_nested_spawn() {
421421
let pool = TaskPool::new();
422-
pool.scope(|scope| {
423-
scope.clone().spawn(async move {
424-
println!("hello from the first scoped thread");
425-
scope.spawn(async move { println!("nested") });
426-
});
427-
});
422+
423+
let foo = Box::new(42);
424+
let foo = &*foo;
425+
426+
let count = Arc::new(AtomicI32::new(0));
427+
428+
let outputs: Vec<i32> = pool.scope(|scope| {
429+
for _ in 0..10 {
430+
let count_clone = count.clone();
431+
let scope = scope.clone();
432+
scope.clone().spawn(async move {
433+
for _ in 0..10 {
434+
let count_clone_clone = count_clone.clone();
435+
scope.spawn(async move {
436+
if *foo != 42 {
437+
panic!("not 42!?!?")
438+
} else {
439+
count_clone_clone.fetch_add(1, Ordering::Relaxed);
440+
*foo
441+
}
442+
});
443+
}
444+
*foo
445+
});
446+
}
447+
}).collect();
448+
449+
for output in &outputs {
450+
assert_eq!(*output, 42);
451+
}
452+
453+
// the inner loop runs 100 times and the outer one runs 10. 100 + 10
454+
assert_eq!(outputs.len(), 110);
455+
assert_eq!(count.load(Ordering::Relaxed), 100);
428456
}
429457
}

0 commit comments

Comments
 (0)