Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more examples and tests for run criteria #4065

Closed
alice-i-cecile opened this issue Feb 28, 2022 · 4 comments
Closed

Add more examples and tests for run criteria #4065

alice-i-cecile opened this issue Feb 28, 2022 · 4 comments
Labels
A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change C-Examples An addition or correction to our examples D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Feb 28, 2022

Currently there are no examples and limited tests for these.

We should add both.

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-ECS Entities, components, systems, and events C-Examples An addition or correction to our examples C-Code-Quality A section of code that is hard to understand or change labels Feb 28, 2022
@hymm
Copy link
Contributor

hymm commented Feb 28, 2022

There's some tests here:

fn parallel_run_criteria() {
let mut world = World::new();
world.insert_resource(Vec::<usize>::new());
let mut stage = SystemStage::parallel()
.with_system(
make_parallel(0)
.label("0")
.with_run_criteria(every_other_time),
)
.with_system(make_parallel(1).after("0"));
stage.run(&mut world);
stage.run(&mut world);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
stage.run(&mut world);
assert_eq!(
*world.get_resource::<Vec<usize>>().unwrap(),
vec![0, 1, 1, 0, 1, 1]
);
world.get_resource_mut::<Vec<usize>>().unwrap().clear();
let mut stage = SystemStage::parallel()
.with_system(make_parallel(0).before("1"))
.with_system_set(
SystemSet::new()
.with_run_criteria(every_other_time)
.with_system(make_parallel(1).label("1")),
)
.with_system(make_parallel(2).after("1"));
stage.run(&mut world);
stage.run(&mut world);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
stage.run(&mut world);
assert_eq!(
*world.get_resource::<Vec<usize>>().unwrap(),
vec![0, 1, 2, 0, 2, 0, 1, 2, 0, 2]
);
// Reusing criteria.
world.get_resource_mut::<Vec<usize>>().unwrap().clear();
let mut stage = SystemStage::parallel()
.with_system_run_criteria(every_other_time.label("every other time"))
.with_system(make_parallel(0).before("1"))
.with_system(
make_parallel(1)
.label("1")
.with_run_criteria("every other time"),
)
.with_system(
make_parallel(2)
.label("2")
.after("1")
.with_run_criteria("every other time"),
)
.with_system(make_parallel(3).after("2"));
stage.run(&mut world);
stage.run(&mut world);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
stage.run(&mut world);
assert_eq!(
*world.get_resource::<Vec<usize>>().unwrap(),
vec![0, 1, 2, 3, 0, 3, 0, 1, 2, 3, 0, 3]
);
assert_eq!(stage.run_criteria.len(), 1);
// Piping criteria.
world.get_resource_mut::<Vec<usize>>().unwrap().clear();
fn eot_piped(input: In<ShouldRun>, has_ran: Local<bool>) -> ShouldRun {
if let ShouldRun::Yes | ShouldRun::YesAndCheckAgain = input.0 {
every_other_time(has_ran)
} else {
ShouldRun::No
}
}
let mut stage = SystemStage::parallel()
.with_system(make_parallel(0).label("0"))
.with_system(
make_parallel(1)
.label("1")
.after("0")
.with_run_criteria(every_other_time.label("every other time")),
)
.with_system(make_parallel(2).label("2").after("1").with_run_criteria(
RunCriteria::pipe("every other time", IntoSystem::into_system(eot_piped)),
))
.with_system(
make_parallel(3).label("3").after("2").with_run_criteria(
"every other time"
.pipe(IntoSystem::into_system(eot_piped))
.label("piped"),
),
)
.with_system(make_parallel(4).after("3").with_run_criteria("piped"));
for _ in 0..4 {
stage.run(&mut world);
}
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
for _ in 0..5 {
stage.run(&mut world);
}
assert_eq!(
*world.get_resource::<Vec<usize>>().unwrap(),
vec![0, 1, 2, 3, 4, 0, 0, 1, 0, 0, 1, 2, 3, 4, 0, 0, 1, 0, 0, 1, 2, 3, 4]
);
assert_eq!(stage.run_criteria.len(), 3);
// Discarding extra criteria with matching labels.
world.get_resource_mut::<Vec<usize>>().unwrap().clear();
let mut stage =
SystemStage::parallel()
.with_system(make_parallel(0).before("1"))
.with_system(make_parallel(1).label("1").with_run_criteria(
every_other_time.label_discard_if_duplicate("every other time"),
))
.with_system(make_parallel(2).label("2").after("1").with_run_criteria(
every_other_time.label_discard_if_duplicate("every other time"),
))
.with_system(make_parallel(3).after("2"));
stage.run(&mut world);
stage.run(&mut world);
stage.set_executor(Box::new(SingleThreadedExecutor::default()));
stage.run(&mut world);
stage.run(&mut world);
assert_eq!(
*world.get_resource::<Vec<usize>>().unwrap(),
vec![0, 1, 2, 3, 0, 3, 0, 1, 2, 3, 0, 3]
);
assert_eq!(stage.run_criteria.len(), 1);

@alice-i-cecile alice-i-cecile changed the title Add examples and tests for run criteria Add more examples and tests for run criteria Feb 28, 2022
@james7132
Copy link
Member

#7652 adds an example for run conditions, and though it seems like the tests that @hymm are no longer there after stageless's merge. Perhaps we should add those tests back in.

@LiamGallagher737
Copy link
Member

I'm happy to add in some more tests, would theses go the conditions.rs file?
https://github.com/bevyengine/bevy/blob/main/crates/bevy_ecs/src/schedule/condition.rs

@alice-i-cecile
Copy link
Member Author

Yes please, I would add a mod tests there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change C-Examples An addition or correction to our examples D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

No branches or pull requests

4 participants