bevy/crates/bevy_time/src
Greeble 83b2265673
Fix bevy_time tests occasionally failing on optimised Windows builds (#17349)
# Objective

Fix the `bevy_time` unit tests occasionally failing on optimised Windows
builds.

# Background

I noticed that the `bevy_time` unit tests would fail ~50% of the time
after enabling `opt-level=1` in config.toml, or adding `--release` to
cargo test.

```
> cargo test -p bevy_time --release

thread 'real::test::test_update' panicked at crates\bevy_time\src\real.rs:164:9:
assertion `left != right` failed
  left: Some(Instant { t: 9458.0756664s })
 right: Some(Instant { t: 9458.0756664s })
 ```

Disabling optimisations would fix the issue, as would switching from Windows to Linux.

The failing path is roughly:

```rust
let mut time = Time::<Real>::new(Instant::now());
time.update();
time.update();
assert_ne!(time.last_update(), time.first_update());
```

Which kinda boils down to:

```rust
let left = Instant::now();
let right = Instant::now();
assert_ne!(left, right);
```

So the failure seems legit, since there's no guarantee that `Instant::now()` increases between calls.

I suspect it only triggers with a combination of Windows + fast CPU + optimisations (Windows has a lower resolution clock than Linux/MacOS). That would explain why it doesn't fail on the Bevy Github CI (optimisations disabled, and I'm guessing the runner CPUs are clocked lower).

# Solution

Make sure `Instant::now()` has increased before calling `time.update()`.

I also considered:

1. Change the unit tests to accept `Instant:now()` not increasing.
    - In retrospect this is maybe the better change?
    - There's other unit tests that cover time increasing.
    - Could also add a deterministic test for zero delta updates.
    - I can switch the PR to this if desired.
2. Avoid any paths that hit `Instant::now()` in unit tests.
    - Arguably unit tests should always be deterministic.
    - But that would mean a bunch of paths aren't tested.

## Testing

`cargo test -p bevy_time --release`

## System Info

`os: "Windows 10 Pro", kernel: "19045", cpu: "AMD Ryzen 9 7900 12-Core Processor", core_count: "12", memory: "63.2 GiB"`

Also tested on same computer with Linux pop-os 6.9.3.

Co-authored-by: François Mockers <mockersf@gmail.com>
2025-01-14 00:30:07 +00:00
..
common_conditions.rs Remove everything except Instant from bevy_utils::time (#17158) 2025-01-05 20:36:08 +00:00
fixed.rs Remove everything except Instant from bevy_utils::time (#17158) 2025-01-05 20:36:08 +00:00
lib.rs Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
real.rs Fix bevy_time tests occasionally failing on optimised Windows builds (#17349) 2025-01-14 00:30:07 +00:00
stopwatch.rs Remove everything except Instant from bevy_utils::time (#17158) 2025-01-05 20:36:08 +00:00
time.rs Remove everything except Instant from bevy_utils::time (#17158) 2025-01-05 20:36:08 +00:00
timer.rs bevy_time: Apply #![deny(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17210) 2025-01-09 06:29:42 +00:00
virt.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00