Skip to content

fix(Foundation): Replace timing-dependent tests with condition-based waiting#5137

Merged
matejk merged 3 commits intomainfrom
4977-fix-flaky-foundation-tests
Dec 23, 2025
Merged

fix(Foundation): Replace timing-dependent tests with condition-based waiting#5137
matejk merged 3 commits intomainfrom
4977-fix-flaky-foundation-tests

Conversation

@matejk
Copy link
Copy Markdown
Contributor

@matejk matejk commented Dec 23, 2025

Summary

Fixes #4977

This PR addresses flaky Foundation tests that were failing intermittently on slower/loaded systems (e.g., nixpkgs builds) due to tight timing tolerances with fixed Thread::sleep() calls.

Changes

  • Added waitForCondition() helper function to CppUnit for condition-based polling with timeout
  • Replaced fixed Thread::sleep() calls with condition-based waiting in Foundation tests
  • Converted unbounded while (...) Thread::sleep() loops to bounded waits that fail cleanly on timeout

Files Modified

File Changes
CppUnit/include/CppUnit/TestCase.h Added waitForCondition() template function
ActivityTest.cpp Wait for activity to actually run before stopping
AsyncNotificationCenterTest.cpp Replaced 15+ polling loops with bounded waits
ExpireCacheTest.cpp Condition-based expiration waiting
ExpireLRUCacheTest.cpp Condition-based expiration waiting
UniqueExpireCacheTest.cpp Condition-based expiration waiting
UniqueExpireLRUCacheTest.cpp Condition-based expiration waiting
TaskTest.cpp Bounded waits for task state transitions
TaskManagerTest.cpp Bounded waits for task manager operations
NotificationQueueTest.cpp Bounded wait for queue draining
TimestampTest.cpp Increased timing tolerance
SemaphoreTest.cpp Added using statement for consistency

Pattern Applied

// Before (unbounded, can hang forever):
while (condition) Thread::sleep(50);

// After (bounded, fails cleanly if condition never met):
assertTrue(waitForCondition([&]{ return condition; }, 5000));

waitForCondition API

template <typename Func>
bool waitForCondition(Func condition, long timeoutMs, long pollIntervalMs = 100)
  • Polls condition every 100ms (configurable)
  • Returns true if condition met, false on timeout
  • Uses pure C++17 (std::chrono::steady_clock, std::this_thread::sleep_for)

Future Improvements: Net Library

The Net testsuite has similar patterns that could benefit from waitForCondition:

High Priority (Unbounded Loops)

File Line Pattern
PollSetTest.cpp 451 while (!poller.isRunning()) Thread::sleep(100);
UDPServerTest.cpp 122-134 do { Thread::sleep(10); } while (count < i);

Medium Priority (Fixed Sleeps + Assertions)

File Count Notes
DatagramSocketTest.cpp 1 Sleep before assertTrue(ss.available() == 5)
WebSocketTest.cpp 4 Sleeps waiting for server start
SyslogTest.cpp 6 Sleeps waiting for syslog processing
TCPServerTest.cpp 10 Sleeps in server tests

Total: ~49 Thread::sleep calls across 13 files, of which 2 are critical unbounded loops and ~8-10 are good candidates for condition-based waiting.

Test Plan

  • All 69 modified Foundation tests pass
  • Tests now fail cleanly with timeout instead of hanging forever
  • CI validation

@matejk matejk force-pushed the 4977-fix-flaky-foundation-tests branch from 51c1e9b to 063afb1 Compare December 23, 2025 10:55
@matejk matejk changed the base branch from devel to main December 23, 2025 10:57
@matejk matejk requested a review from aleks-f December 23, 2025 11:00
@matejk matejk linked an issue Dec 23, 2025 that may be closed by this pull request
@matejk matejk force-pushed the 4977-fix-flaky-foundation-tests branch from ff4a0b6 to 541cecd Compare December 23, 2025 15:32
@matejk matejk force-pushed the 4977-fix-flaky-foundation-tests branch from 541cecd to 588672b Compare December 23, 2025 15:48
@matejk matejk merged commit 18c49ed into main Dec 23, 2025
90 checks passed
@matejk matejk deleted the 4977-fix-flaky-foundation-tests branch December 23, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible flaky tests when building for nixpkgs

1 participant