Skip to content

Commit 7509abd

Browse files
committed
unit test comments and using mutex instead of fetch_update because fetch_update could fail (and will under miri)
1 parent 10159e6 commit 7509abd

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

crates/bevy_ecs/src/schedule/condition.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ mod tests {
13681368
fn combinators_with_maybe_failing_condition() {
13691369
use crate::system::RunSystemOnce;
13701370
use alloc::sync::Arc;
1371-
use core::sync::atomic::{AtomicUsize, Ordering};
1371+
use std::sync::Mutex;
13721372

13731373
// Things that should be tested:
13741374
// - the final result of the combinator is correct
@@ -1381,20 +1381,20 @@ mod tests {
13811381
// SystemConditions don't have mutable access to the world, so we use a
13821382
// `Res<AtomicCounter>` to count invocations.
13831383
#[derive(Resource, Default)]
1384-
struct AtomicCounter(Arc<AtomicUsize>);
1384+
struct Counter(Arc<Mutex<usize>>);
13851385

13861386
// The following constants are used to represent a system having run.
13871387
// both are prime so that when multiplied they give a unique value for any TRUE^n*FALSE^m
13881388
const FALSE: usize = 2;
13891389
const TRUE: usize = 3;
13901390

13911391
// this is a system, but has the same side effect as `test_true`
1392-
fn is_true_inc(counter: Res<AtomicCounter>) -> bool {
1392+
fn is_true_inc(counter: Res<Counter>) -> bool {
13931393
test_true(&counter)
13941394
}
13951395

13961396
// this is a system, but has the same side effect as `test_false`
1397-
fn is_false_inc(counter: Res<AtomicCounter>) -> bool {
1397+
fn is_false_inc(counter: Res<Counter>) -> bool {
13981398
test_false(&counter)
13991399
}
14001400

@@ -1403,36 +1403,39 @@ mod tests {
14031403
true
14041404
}
14051405

1406-
fn test_true(counter: &AtomicCounter) -> bool {
1407-
_ = counter
1408-
.0
1409-
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |v| Some(v * TRUE));
1406+
fn test_true(counter: &Counter) -> bool {
1407+
*counter.0.lock().unwrap() *= TRUE;
14101408
true
14111409
}
14121410

1413-
fn test_false(counter: &AtomicCounter) -> bool {
1414-
_ = counter
1415-
.0
1416-
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |v| Some(v * FALSE));
1411+
fn test_false(counter: &Counter) -> bool {
1412+
*counter.0.lock().unwrap() *= FALSE;
14171413
false
14181414
}
14191415

14201416
// Helper function that runs a logic call and returns the result, as
1421-
// well as the prime factorization of the calls.
1422-
fn logic_call_result(f: impl FnOnce(&AtomicCounter) -> bool) -> (usize, bool) {
1423-
let counter = AtomicCounter(Arc::new(AtomicUsize::new(1)));
1417+
// well as the composite number of the calls.
1418+
fn logic_call_result(f: impl FnOnce(&Counter) -> bool) -> (usize, bool) {
1419+
let counter = Counter(Arc::new(Mutex::new(1)));
14241420
let result = f(&counter);
1425-
(counter.0.load(Ordering::SeqCst), result)
1421+
(*counter.0.lock().unwrap(), result)
14261422
}
14271423

1428-
// we expect `true() || false()` to yield `true`, and short circuit after `true()`
1424+
// `test_true` and `test_false` can't fail like the systems can, and so
1425+
// we use them to model the short circuiting behaviour of rust's logical
1426+
// operators. The goal is to end up with a composite number that
1427+
// describes rust's behaviour and compare that to the result of the
1428+
// combinators.
1429+
1430+
// we expect `true() || false()` to yield `true`, and short circuit
1431+
// after `true()`
14291432
assert_eq!(
14301433
logic_call_result(|c| test_true(c) || test_false(c)),
14311434
(TRUE.pow(1) * FALSE.pow(0), true)
14321435
);
14331436

14341437
let mut world = World::new();
1435-
world.init_resource::<AtomicCounter>();
1438+
world.init_resource::<Counter>();
14361439

14371440
// ensure there are no `Vacant` entities
14381441
assert!(world.query::<&Vacant>().iter(&world).next().is_none());
@@ -1448,14 +1451,11 @@ mod tests {
14481451
fn assert_system<Marker>(
14491452
world: &mut World,
14501453
system: impl crate::system::IntoSystem<(), bool, Marker>,
1451-
equivalent_to: impl FnOnce(&AtomicCounter) -> bool,
1454+
equivalent_to: impl FnOnce(&Counter) -> bool,
14521455
) {
14531456
use crate::system::System;
14541457

1455-
world
1456-
.resource::<AtomicCounter>()
1457-
.0
1458-
.store(1, Ordering::SeqCst);
1458+
*world.resource::<Counter>().0.lock().unwrap() = 1;
14591459

14601460
let system = crate::system::IntoSystem::into_system(system);
14611461
let name = system.name();
@@ -1464,10 +1464,7 @@ mod tests {
14641464

14651465
let (expected_counter, expected) = logic_call_result(equivalent_to);
14661466
let caller = std::panic::Location::caller();
1467-
let counter = world
1468-
.resource::<AtomicCounter>()
1469-
.0
1470-
.swap(1, Ordering::SeqCst);
1467+
let counter = *world.resource::<Counter>().0.lock().unwrap();
14711468

14721469
assert_eq!(
14731470
out,

0 commit comments

Comments
 (0)