Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112852

Allow faking the progress of time for testing code using QTimer

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • None
    • Core: Event loop
    • None
    • All

    Description

      Our code uses QTimers extensively - both repeating QTimers and singleshot QTimer.

      Unfortunately this makes writing tests very sad, because the tests take a long time to run and can be flaky (when a typical timeout is eg 2 minutes this adds up quickly).

      It would be nice if there was some way we could "fake" the passage of time for testing - eg an "enableDeterministicTime() + advanceTimeBy()" API? This would allow us to run tests with deterministic timings, and most tests would complete immediately rather than taking minutes.

       


      Existing work arounds:

      • we can mock QTimer and inject, which works, but requires us to maintain and document a custom QTimer like API, convert over older code, and it doesn't work for static methods like QTimer::singleShot (this is our current approach)
      • we can write a QTimer wrapper and switch in another implementation when compiling test binaries, but this has similar issues to mocking + injection except that static methods can be implemented
      • exposing the relevant callbacks as slots and calling them directly in the tests, which exposes implementation details and doesn't work with lambda arguments
      • we can use some LD_LIBRARY_PRELOAD magic to fake the underlying calls, but this isn't portable and seems like it will break easily if Qt changes

       

      Other people that might benefit from such a feature:

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            alastair.hughes Alastair Hughes
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes