Skip to content

Commit ffd0121

Browse files
Allow setting time overstep in tests (#21738)
# Objective There is currently no way to construct a `Time<Fixed>` with a non-zero overstep. The only way to test systems which use the overstep is to advance the virtual time and run the `RunFixedMainLoop` schedule, which is cumbersome. ## Solution Exposes the `accumulate_overstep` method as a counterpart of `discard_overstep`. ## Testing This method is covered by existing tests.
1 parent 6c29824 commit ffd0121

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

crates/bevy_time/src/fixed.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ impl Time<Fixed> {
182182
self.context().overstep
183183
}
184184

185+
/// Increase the overstep time accumulated towards new steps.
186+
///
187+
/// This method is provided for use in tests. Ordinarily, the [`run_fixed_main_schedule`] system is responsible for calculating the overstep.
188+
#[inline]
189+
pub fn accumulate_overstep(&mut self, delta: Duration) {
190+
self.context_mut().overstep += delta;
191+
}
192+
185193
/// Discard a part of the overstep amount.
186194
///
187195
/// If `discard` is higher than overstep, the overstep becomes zero.
@@ -205,10 +213,6 @@ impl Time<Fixed> {
205213
self.context().overstep.as_secs_f64() / self.context().timestep.as_secs_f64()
206214
}
207215

208-
fn accumulate(&mut self, delta: Duration) {
209-
self.context_mut().overstep += delta;
210-
}
211-
212216
fn expend(&mut self) -> bool {
213217
let timestep = self.timestep();
214218
if let Some(new_value) = self.context_mut().overstep.checked_sub(timestep) {
@@ -238,7 +242,9 @@ impl Default for Fixed {
238242
/// [`RunFixedMainLoopSystems`](bevy_app::prelude::RunFixedMainLoopSystems).
239243
pub fn run_fixed_main_schedule(world: &mut World) {
240244
let delta = world.resource::<Time<Virtual>>().delta();
241-
world.resource_mut::<Time<Fixed>>().accumulate(delta);
245+
world
246+
.resource_mut::<Time<Fixed>>()
247+
.accumulate_overstep(delta);
242248

243249
// Run the schedule until we run out of accumulated time
244250
let _ = world.try_schedule_scope(FixedMain, |world, schedule| {
@@ -278,7 +284,7 @@ mod test {
278284
assert_eq!(time.delta(), Duration::ZERO);
279285
assert_eq!(time.elapsed(), Duration::ZERO);
280286

281-
time.accumulate(Duration::from_secs(1));
287+
time.accumulate_overstep(Duration::from_secs(1));
282288

283289
assert_eq!(time.delta(), Duration::ZERO);
284290
assert_eq!(time.elapsed(), Duration::ZERO);
@@ -294,7 +300,7 @@ mod test {
294300
assert_eq!(time.overstep_fraction(), 0.5);
295301
assert_eq!(time.overstep_fraction_f64(), 0.5);
296302

297-
time.accumulate(Duration::from_secs(1));
303+
time.accumulate_overstep(Duration::from_secs(1));
298304

299305
assert_eq!(time.delta(), Duration::ZERO);
300306
assert_eq!(time.elapsed(), Duration::ZERO);
@@ -318,7 +324,7 @@ mod test {
318324
assert_eq!(time.overstep_fraction(), 0.0);
319325
assert_eq!(time.overstep_fraction_f64(), 0.0);
320326

321-
time.accumulate(Duration::from_secs(1));
327+
time.accumulate_overstep(Duration::from_secs(1));
322328

323329
assert_eq!(time.delta(), Duration::from_secs(2));
324330
assert_eq!(time.elapsed(), Duration::from_secs(2));
@@ -339,7 +345,7 @@ mod test {
339345
fn test_expend_multiple() {
340346
let mut time = Time::<Fixed>::from_seconds(2.0);
341347

342-
time.accumulate(Duration::from_secs(7));
348+
time.accumulate_overstep(Duration::from_secs(7));
343349
assert_eq!(time.overstep(), Duration::from_secs(7));
344350

345351
assert!(time.expend()); // true

0 commit comments

Comments
 (0)