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

QTest::qSleep is very unreliable on Mac OS X

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 5.15.0
    • Fix Version/s: None
    • Component/s: Testing: qtestlib
    • Labels:
      None
    • Environment:
      Mac OS X 10.15.5 with Qt 5.15.0
    • Platform/s:
      macOS

      Description

      The attached test program mac-sleep.cpp uses QTest::qSleep to sleep for 50ms 25 times and prints how much time actually elapsed. When run on Linux (tested with Ubuntu 18.04.5 / Qt 5.15.0 and with Ubuntu 20.04 / Qt 5.12.8), it does consistently print 50 (except potentially for the very first invocation, where some initialization may be going on?).

      When run on Mac OS X (10.15.5 / Qt 5.15.0 on Github Actions, as I don't own a Mac), I get very erratic results, such as

      51
      118
      94
      56
      51
      106
      71
      56
      101
      114
      50
      106
      80
      109
      75
      65
      70
      57
      52
      52
      54
      147
      71
      51
      50

      I do realize that qSleep only guarantees that at least the specified amount (here: 50ms) have passed, but sometimes being off by over 100% (or 50ms) seems awfully much. In real life, this wreaks havoc when trying to unit-test timing-based code (such as animations) that can easily have a time-resolution < 0.1 s.

      From https://dshil.github.io/blog/missed-os-x-clock-guide/,] , it would seem that there are Mac-specific high-resolution timers and that POSIX (as I understand it, qSleep uses nanosleep) is just some kind of "add on" relying on message-based communication with the kernel. So maybe this issue could be resolved by using the Mac's high-resolution timers directly? (FWIW, it seems that using std::this_thread::sleep_for() is similarly unreliable)

      As I stated above, I don't own a Mac, so unfortunately I can't reliably test how this is affected by system configuration, system load, etc.

       

        Attachments

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

          Activity

            People

            Assignee:
            macadder Jason McDonald
            Reporter:
            st.loeffler Stefan Löffler
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes