Skip to content

Use virtual time in TaskQueue unit tests #435

@gavv

Description

@gavv

Last revised: Oct 2023

Update: The task is still relevant. Description is updated to address problems discussed in unfinished PR, linked below.

Currently unit tests for ctl::ControlTaskQueue (test_task_queue.cpp) are partially bound to real clock time (core::timestamp() and core::Timer) and due to this they can fail on loaded system, e.g. slow CI runners.

We can make them 100% reliable by using virtual clock instead of real time, i.e. clock that is fully controlled by unit tests.

We already use such approach in unit tests for pipeline::PipelineLoop (test_pipeline_loop.cpp). PipelineLoop doesn't call core::timestamp() directly. Instead, it have a protected virtual method to get time:

virtual core::nanoseconds_t timestamp_imp() const

In test_pipeline_loop.cpp we inherit PipelineLoop, override this method to return virtual (fake) time, and test this derived class instead of original PipelineLoop.

Now we could use the same approach test_task_queue.cpp:

  • In ctl::ControlTaskQueue, add virtual methods that isolate calls to core::timestamp() and core::Timer

    For example, we can add 3 methods:

    • core::nanoseconds_t timestamp_imp()
    • bool try_set_deadline(nanoseconds_t)
    • void wait_deadline()

    Default implementations will just call core::timestamp() and Timer methods.

  • In test_task_queue.cpp, add a class that inherits ctl::ControlTaskQueue, overrides these 3 methods to work with virtual/fake time, and adds helpers to control that fake time.

  • Adjust tests to work with virtual time instead of real time, i.e. don't use sleep and instead call helpers to control fake time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-refactoringcategory: RefactoringC-testscategory: Writing or improving testseasy hacksSolution requires minimal project contexthelp wantedLooking for contributorsmost wantedNeeded most among other help-wanted issues

    Projects

    Status

    Help wanted

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions