TEST-INFRA: [flaky-tests] Replace time.sleep() with time mocking in memory_service tests #3695

Open
opened 2026-04-05 22:13:13 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: task/flaky-tests-replace-time-sleep-memory-service
  • Commit Message: test(memory_service): replace time.sleep() with time mocking for deterministic timestamp tests
  • Milestone: N/A — Backlog (see note below)
  • Parent Epic: #1678

Backlog note: This issue was discovered during autonomous operation
on milestone v3.7.0. It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.

Background and Context

The Behave tests for the MemoryService in features/steps/memory_service_coverage_steps.py use time.sleep(0.01) to ensure that timestamps differ when testing the last_seen and first_seen fields of an entity. Tests that rely on fixed-time waits are inherently fragile: on heavily loaded CI systems, the sleep may not produce a sufficient timestamp delta, or the overhead of the sleep accumulates across the test suite, slowing CI runs unnecessarily.

Per the project's testing philosophy (CONTRIBUTING.md), mocks are the preferred mechanism for controlling non-deterministic dependencies in unit tests. Replacing time.sleep() with a controlled mock of time.time() eliminates the timing dependency entirely, making the tests both faster and more robust.

Expected Behavior

Timestamp-ordering assertions in memory_service_coverage_steps.py should be driven by a mocked clock rather than real elapsed time, so they pass deterministically regardless of system load.

Acceptance Criteria

  • All time.sleep() calls in features/steps/memory_service_coverage_steps.py are removed.
  • A unittest.mock.patch (or equivalent) on time.time is used to advance the clock between operations that require distinct timestamps.
  • All affected Behave scenarios continue to pass after the change.
  • No production source code is modified (mocks remain in test directories only, per CONTRIBUTING.md).
  • nox (all default sessions) passes with coverage ≥ 97%.

Supporting Information

  • Affected file: features/steps/memory_service_coverage_steps.py
  • Related epic: #1678 (CI Execution Time Optimization — flaky/slow tests are a CI reliability concern)
  • CONTRIBUTING.md: "Mocks are only permitted in unit tests; integration tests must use real services and dependencies. Dependency injection is the preferred method for swapping implementations during tests."

Subtasks

  • Identify all time.sleep() calls in features/steps/memory_service_coverage_steps.py
  • Introduce a time.time mock (e.g., via unittest.mock.patch or a Behave fixture) that allows direct control of returned timestamps
  • Replace each time.sleep() call with an advancement of the mocked clock
  • Verify all affected Behave scenarios pass with the new approach
  • Run nox -s coverage_report and confirm coverage ≥ 97%
  • Run nox (all default sessions) and fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • All time.sleep() calls in features/steps/memory_service_coverage_steps.py have been replaced with a time-mocking mechanism.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines describing the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass.
  • Coverage ≥ 97%.

Automated by CleverAgents Bot
Supervisor: Test Infrastructure | Agent: ca-new-issue-creator

## Metadata - **Branch**: `task/flaky-tests-replace-time-sleep-memory-service` - **Commit Message**: `test(memory_service): replace time.sleep() with time mocking for deterministic timestamp tests` - **Milestone**: N/A — Backlog (see note below) - **Parent Epic**: #1678 > **Backlog note:** This issue was discovered during autonomous operation > on milestone v3.7.0. It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. ## Background and Context The Behave tests for the `MemoryService` in `features/steps/memory_service_coverage_steps.py` use `time.sleep(0.01)` to ensure that timestamps differ when testing the `last_seen` and `first_seen` fields of an entity. Tests that rely on fixed-time waits are inherently fragile: on heavily loaded CI systems, the sleep may not produce a sufficient timestamp delta, or the overhead of the sleep accumulates across the test suite, slowing CI runs unnecessarily. Per the project's testing philosophy (CONTRIBUTING.md), mocks are the preferred mechanism for controlling non-deterministic dependencies in unit tests. Replacing `time.sleep()` with a controlled mock of `time.time()` eliminates the timing dependency entirely, making the tests both faster and more robust. ## Expected Behavior Timestamp-ordering assertions in `memory_service_coverage_steps.py` should be driven by a mocked clock rather than real elapsed time, so they pass deterministically regardless of system load. ## Acceptance Criteria - All `time.sleep()` calls in `features/steps/memory_service_coverage_steps.py` are removed. - A `unittest.mock.patch` (or equivalent) on `time.time` is used to advance the clock between operations that require distinct timestamps. - All affected Behave scenarios continue to pass after the change. - No production source code is modified (mocks remain in test directories only, per CONTRIBUTING.md). - `nox` (all default sessions) passes with coverage ≥ 97%. ## Supporting Information - Affected file: `features/steps/memory_service_coverage_steps.py` - Related epic: #1678 (CI Execution Time Optimization — flaky/slow tests are a CI reliability concern) - CONTRIBUTING.md: "Mocks are only permitted in unit tests; integration tests must use real services and dependencies. Dependency injection is the preferred method for swapping implementations during tests." ## Subtasks - [ ] Identify all `time.sleep()` calls in `features/steps/memory_service_coverage_steps.py` - [ ] Introduce a `time.time` mock (e.g., via `unittest.mock.patch` or a Behave fixture) that allows direct control of returned timestamps - [ ] Replace each `time.sleep()` call with an advancement of the mocked clock - [ ] Verify all affected Behave scenarios pass with the new approach - [ ] Run `nox -s coverage_report` and confirm coverage ≥ 97% - [ ] Run `nox` (all default sessions) and fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - All `time.sleep()` calls in `features/steps/memory_service_coverage_steps.py` have been replaced with a time-mocking mechanism. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines describing the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass. - Coverage ≥ 97%. --- **Automated by CleverAgents Bot** Supervisor: Test Infrastructure | Agent: ca-new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#3695
No description provided.