From e5263db8551092f9fbf148de81f7d0f4517f62bd Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Thu, 12 Nov 2026 08:45:46 -0800 Subject: [PATCH] fix unit test that was timing dependent --- include/box3d/box3d.h | 2 +- test/test_world.c | 28 +++++++++++----------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/box3d/box3d.h b/include/box3d/box3d.h index fcfcf54..ecc1bfc 100644 --- a/include/box3d/box3d.h +++ b/include/box3d/box3d.h @@ -116,7 +116,7 @@ B3_API b3TreeStats b3World_CastShape( b3WorldId worldId, b3Pos origin, const b3S b3QueryFilter filter, b3CastResultFcn* fcn, void* context ); /// Overlap test for all shapes that *potentially* overlap the provided AABB. This only considers static bodies, -/// so it may be invoked concurrently with b3World_Step. This is not thread-safe with creationg and destruction +/// so it may be invoked concurrently with b3World_Step. This is not thread-safe with creation and destruction /// operations. This is a workaround for The Legend of California and may be removed later if the workaround is no /// longer needed. This is not part of a strategy to make concurrent queries thread-safe. B3_API b3TreeStats b3World_OverlapAABB_Static( b3WorldId worldId, b3AABB aabb, b3QueryFilter filter, diff --git a/test/test_world.c b/test/test_world.c index a498e68..298e514 100644 --- a/test/test_world.c +++ b/test/test_world.c @@ -1807,6 +1807,7 @@ typedef struct StaticQueryShared b3AtomicInt stepping; b3AtomicInt quit; b3AtomicInt idle; + b3AtomicInt started; b3AtomicInt expectedCount; b3AtomicInt queryCount; b3AtomicInt failCount; @@ -1913,6 +1914,7 @@ static void StaticQueryThread( void* context ) } b3AtomicStoreInt( &shared->idle, 0 ); + b3AtomicStoreInt( &shared->started, 1 ); int expected = b3AtomicLoadInt( &shared->expectedCount ); StaticQueryHits hits = { shared->worldId, 0, false }; @@ -1959,13 +1961,6 @@ static int TestOverlapStaticDuringStep( void ) b3CreateHullShape( bodyId, &shapeDef, &cube.base ); } - b3World* world = b3GetWorldFromId( worldId ); - - // Islands falling asleep during the step grow the solver set array under the reader. Count the - // steps where that happened while at least two full query rounds, which read static transforms, - // ran inside the step. - int overlappedGrowthCount = 0; - StaticQueryShared shared = { 0 }; shared.worldId = worldId; b3Thread* thread = b3CreateThread( StaticQueryThread, &shared, "static query" ); @@ -1982,12 +1977,16 @@ static int TestOverlapStaticDuringStep( void ) ENSURE( b3HasTreeMoved( b3GetWorldFromId( worldId )->broadPhase.trees + b3_staticBody ) ); - int setCapacity = world->sleepingSets.capacity; - int queryCount = b3AtomicLoadInt( &shared.queryCount ); - b3AtomicStoreInt( &shared.expectedCount, staticCount ); + b3AtomicStoreInt( &shared.started, 0 ); b3AtomicStoreInt( &shared.stepping, 1 ); + // Scheduling decides how many rounds land inside the step, so only wait for one to begin + while ( b3AtomicLoadInt( &shared.started ) == 0 ) + { + b3Yield(); + } + b3World_Step( worldId, 1.0f / 60.0f, 4 ); b3AtomicStoreInt( &shared.stepping, 0 ); @@ -1997,20 +1996,15 @@ static int TestOverlapStaticDuringStep( void ) b3Yield(); } - if ( world->sleepingSets.capacity > setCapacity && b3AtomicLoadInt( &shared.queryCount ) - queryCount >= 2 ) - { - overlappedGrowthCount += 1; - } - ENSURE( b3HasTreeMoved( b3GetWorldFromId( worldId )->broadPhase.trees + b3_staticBody ) == false ); } b3AtomicStoreInt( &shared.quit, 1 ); b3JoinThread( thread ); - ENSURE( b3AtomicLoadInt( &shared.queryCount ) > 0 ); + // Each step started a round, and the thread cannot report idle until that round finishes + ENSURE( b3AtomicLoadInt( &shared.queryCount ) >= 120 ); ENSURE( b3AtomicLoadInt( &shared.failCount ) == 0 ); - ENSURE( overlappedGrowthCount > 0 ); // The serial path agrees with the regular overlap filtered to static bodies. StaticQueryHits hits = { worldId, 0, false };