Skip to content

Commit

Permalink
copy test from bevyengine#4343
Browse files Browse the repository at this point in the history
Co-authored-by: MiniaczQ<MiniaczQ@gmail.com>
  • Loading branch information
Michael Hsu committed Apr 11, 2022
1 parent 1fc63a9 commit e42acb4
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,43 @@ mod tests {
assert!(!thread_check_failed.load(Ordering::Acquire));
assert_eq!(count.load(Ordering::Acquire), 200);
}

#[test]
fn test_nested_spawn() {
let pool = TaskPool::new();

let foo = Box::new(42);
let foo = &*foo;

let count = Arc::new(AtomicI32::new(0));

let outputs: Vec<i32> = pool.scope(|scope| {
for _ in 0..10 {
let count_clone = count.clone();
let scope = scope.clone();
scope.clone().spawn(async move {
for _ in 0..10 {
let count_clone_clone = count_clone.clone();
scope.spawn(async move {
if *foo != 42 {
panic!("not 42!?!?")
} else {
count_clone_clone.fetch_add(1, Ordering::Relaxed);
*foo
}
});
}
*foo
});
}
}).collect();

for output in &outputs {
assert_eq!(*output, 42);
}

// the inner loop runs 100 times and the outer one runs 10. 100 + 10
assert_eq!(outputs.len(), 110);
assert_eq!(count.load(Ordering::Relaxed), 100);
}
}

0 comments on commit e42acb4

Please # to comment.