From 4de9371543fb6f291c34e601c862042c2cd58d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Sun, 15 Dec 2024 16:17:16 +0100 Subject: [PATCH] fix: Flush queued spawns in plugin build --- src/plugin.rs | 2 + tests/integration/determinism.rs | 6 +- tests/integration/reseeding.rs | 195 ++++++++++++++++++------------- 3 files changed, 121 insertions(+), 82 deletions(-) diff --git a/src/plugin.rs b/src/plugin.rs index 9ea46c6..db2fc96 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -82,6 +82,8 @@ where app.world_mut().spawn((RngSeed::::from_entropy(), Global)); } + app.world_mut().flush(); + #[cfg(feature = "experimental")] app.add_observer(crate::observers::seed_from_global::) .add_observer(crate::observers::reseed::); diff --git a/tests/integration/determinism.rs b/tests/integration/determinism.rs index d5d7670..29b8ccd 100644 --- a/tests/integration/determinism.rs +++ b/tests/integration/determinism.rs @@ -1,7 +1,11 @@ use bevy_app::prelude::*; use bevy_ecs::prelude::*; use bevy_prng::{ChaCha12Rng, ChaCha8Rng, WyRand}; -use bevy_rand::{global::GlobalSeed, prelude::{Entropy, EntropyPlugin, ForkableAsRng, ForkableRng, GlobalEntropy}, traits::SeedSource}; +use bevy_rand::{ + global::GlobalSeed, + prelude::{Entropy, EntropyPlugin, ForkableAsRng, ForkableRng, GlobalEntropy}, + traits::SeedSource, +}; use rand::prelude::Rng; use rand_core::RngCore; diff --git a/tests/integration/reseeding.rs b/tests/integration/reseeding.rs index 9f9d309..456224d 100644 --- a/tests/integration/reseeding.rs +++ b/tests/integration/reseeding.rs @@ -2,7 +2,11 @@ use bevy_app::prelude::*; use bevy_ecs::prelude::*; use bevy_prng::{ChaCha8Rng, WyRand}; use bevy_rand::{ - global::{Global, GlobalEntropy}, plugin::EntropyPlugin, prelude::Entropy, seed::RngSeed, traits::{ForkableAsSeed, ForkableSeed, SeedSource} + global::{Global, GlobalEntropy}, + plugin::EntropyPlugin, + prelude::Entropy, + seed::RngSeed, + traits::{ForkableAsSeed, ForkableSeed, SeedSource}, }; use rand_core::{RngCore, SeedableRng}; @@ -21,7 +25,10 @@ fn test_global_reseeding() { app.add_plugins(EntropyPlugin::::with_seed(seed)); { - let global_rng = app.world_mut().query_filtered::<&Entropy, With>().single(app.world()); + let global_rng = app + .world_mut() + .query_filtered::<&Entropy, With>() + .single(app.world()); // Our RNGs should be the same as each other as they were initialised with the same seed assert_eq!(global_rng, &rng_eq); @@ -30,22 +37,33 @@ fn test_global_reseeding() { app.update(); { - let global_rng = app.world_mut().query_filtered::<&Entropy, With>().single(app.world()); + let global_rng = app + .world_mut() + .query_filtered::<&Entropy, With>() + .single(app.world()); // Our RNGs should remain the same as each other as we have not run the update assert_eq!(global_rng, &rng_eq); } { - let global = app.world_mut().query_filtered::>().single(app.world()); - - app.world_mut().entity_mut(global).insert(RngSeed::::from_seed([3; 32])); + let global = app + .world_mut() + .query_filtered::>() + .single(app.world()); + + app.world_mut() + .entity_mut(global) + .insert(RngSeed::::from_seed([3; 32])); } app.update(); { - let global_rng = app.world_mut().query_filtered::<&Entropy, With>().single(app.world()); + let global_rng = app + .world_mut() + .query_filtered::<&Entropy, With>() + .single(app.world()); // Now our RNG will not be the same, even though we did not use it directly assert_ne!(global_rng, &rng_eq); @@ -68,18 +86,21 @@ fn component_fork_seed() { } }, ) - .add_systems(Update, |mut q_rng: Query<&mut Entropy, Without>| { - let rngs = q_rng.iter_mut(); + .add_systems( + Update, + |mut q_rng: Query<&mut Entropy, Without>| { + let rngs = q_rng.iter_mut(); - assert_eq!(rngs.size_hint().0, 5); + assert_eq!(rngs.size_hint().0, 5); - let values: Vec<_> = rngs.map(|mut rng| rng.next_u32()).collect(); + let values: Vec<_> = rngs.map(|mut rng| rng.next_u32()).collect(); - assert_eq!( - &values, - &[3315785188, 1951699392, 911252207, 791343233, 1599472206] - ); - }); + assert_eq!( + &values, + &[3315785188, 1951699392, 911252207, 791343233, 1599472206] + ); + }, + ); app.update(); } @@ -100,24 +121,27 @@ fn component_fork_as_seed() { } }, ) - .add_systems(Update, |mut q_rng: Query<&mut Entropy, Without>| { - let rngs = q_rng.iter_mut(); - - assert_eq!(rngs.size_hint().0, 5); - - let values: Vec<_> = rngs.map(|mut rng| rng.next_u64()).collect(); - - assert_eq!( - &values, - &[ - 10032395693880520184, - 15375025802368380325, - 10863580644061233257, - 7067543572507795213, - 7996461288508244033 - ] - ); - }); + .add_systems( + Update, + |mut q_rng: Query<&mut Entropy, Without>| { + let rngs = q_rng.iter_mut(); + + assert_eq!(rngs.size_hint().0, 5); + + let values: Vec<_> = rngs.map(|mut rng| rng.next_u64()).collect(); + + assert_eq!( + &values, + &[ + 10032395693880520184, + 15375025802368380325, + 10863580644061233257, + 7067543572507795213, + 7996461288508244033 + ] + ); + }, + ); app.update(); } @@ -147,21 +171,24 @@ fn observer_global_reseeding() { } }, ) - .add_systems(PreUpdate, |query: Query<&RngSeed, Without>| { - let expected = [ - 2484862625678185386u64, - 10323237495534242118, - 14704548354072994214, - 14638519449267265798, - 11723565746675474547, - ]; - let seeds = query.iter().map(RngSeed::::clone_seed); - - expected - .into_iter() - .zip(seeds.map(u64::from_ne_bytes)) - .for_each(|(expected, actual)| assert_eq!(expected, actual)); - }) + .add_systems( + PreUpdate, + |query: Query<&RngSeed, Without>| { + let expected = [ + 2484862625678185386u64, + 10323237495534242118, + 14704548354072994214, + 14638519449267265798, + 11723565746675474547, + ]; + let seeds = query.iter().map(RngSeed::::clone_seed); + + expected + .into_iter() + .zip(seeds.map(u64::from_ne_bytes)) + .for_each(|(expected, actual)| assert_eq!(expected, actual)); + }, + ) .add_systems( Update, |mut commands: Commands, @@ -172,21 +199,24 @@ fn observer_global_reseeding() { } }, ) - .add_systems(PostUpdate, |query: Query<&RngSeed, Without>| { - let prev_expected = [ - 2484862625678185386u64, - 10323237495534242118, - 14704548354072994214, - 14638519449267265798, - 11723565746675474547, - ]; - let seeds = query.iter().map(RngSeed::::clone_seed); - - prev_expected - .into_iter() - .zip(seeds.map(u64::from_ne_bytes)) - .for_each(|(expected, actual)| assert_ne!(expected, actual)); - }); + .add_systems( + PostUpdate, + |query: Query<&RngSeed, Without>| { + let prev_expected = [ + 2484862625678185386u64, + 10323237495534242118, + 14704548354072994214, + 14638519449267265798, + 11723565746675474547, + ]; + let seeds = query.iter().map(RngSeed::::clone_seed); + + prev_expected + .into_iter() + .zip(seeds.map(u64::from_ne_bytes)) + .for_each(|(expected, actual)| assert_ne!(expected, actual)); + }, + ); app.run(); } @@ -290,23 +320,26 @@ fn generic_observer_reseeding_children() { commands.trigger(LinkRngSourceToTarget::::default()); commands.trigger_targets(SeedFromGlobal::::default(), source); }) - .add_systems(PreUpdate, |query: Query<&RngSeed, (With, Without)>| { - let expected = [ - 6445550333322662121u64, - 14968821102299026759, - 12617564484450995185, - 908888629357954483, - 6128439264405451235, - ]; - let seeds = query.iter().map(RngSeed::::clone_seed); - - assert_eq!(seeds.size_hint().0, 5); - - expected - .into_iter() - .zip(seeds.map(u64::from_ne_bytes)) - .for_each(|(expected, actual)| assert_eq!(expected, actual)); - }) + .add_systems( + PreUpdate, + |query: Query<&RngSeed, (With, Without)>| { + let expected = [ + 6445550333322662121u64, + 14968821102299026759, + 12617564484450995185, + 908888629357954483, + 6128439264405451235, + ]; + let seeds = query.iter().map(RngSeed::::clone_seed); + + assert_eq!(seeds.size_hint().0, 5); + + expected + .into_iter() + .zip(seeds.map(u64::from_ne_bytes)) + .for_each(|(expected, actual)| assert_eq!(expected, actual)); + }, + ) .add_systems(PreUpdate, |query: Query<&RngSeed, With>| { let expected = 2484862625678185386u64; let seeds = u64::from_ne_bytes(query.single().clone_seed());