include/systools/win32/retry_if_failed.hxx |   12 ++++--------
 sal/qa/systools/test_retry_if_failed.cxx   |   10 +++++++---
 2 files changed, 11 insertions(+), 11 deletions(-)

New commits:
commit 6ed614ae2cbd7373c01ac85d040187653a65519c
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Mar 4 09:30:28 2021 +0300
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Mar 9 21:18:24 2021 +0100

    Improve unit test accuracy
    
    I didn't take clock resolution into account when created the test,
    and it failed for me occasionally because the value was slightly
    less than expected.
    
    The typical system tick resolution is documented at
    
https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/high-resolution-timers
    
    Change-Id: Ie48b10d15b14f9ac7d292a2cc9916bcbfff44b6f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111946
    Tested-by: Mike Kaganski <mike.kagan...@collabora.com>
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    (cherry picked from commit eef43192d4c7b2867638c54a2ac31adfc26476c7)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112076
    Tested-by: Jenkins
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/include/systools/win32/retry_if_failed.hxx 
b/include/systools/win32/retry_if_failed.hxx
index 11a7e5372037..d3dd6b125be9 100644
--- a/include/systools/win32/retry_if_failed.hxx
+++ b/include/systools/win32/retry_if_failed.hxx
@@ -24,16 +24,12 @@ namespace sal::systools
 //     HRESULT hr = sal::systools::RetryIfFailed(10, 100, []{ return 
OleFlushClipboard(); });
 template <typename Func> HRESULT RetryIfFailed(unsigned times, unsigned 
msTimeout, Func func)
 {
-    HRESULT hr = E_FAIL;
-    for (unsigned i = 0; i < times; ++i)
+    for (unsigned i = 0;; ++i)
     {
-        hr = func();
-        if (SUCCEEDED(hr))
-            break;
-        if (i < times - 1)
-            Sleep(msTimeout);
+        if (HRESULT hr = func(); SUCCEEDED(hr) || i >= times)
+            return hr;
+        Sleep(msTimeout);
     }
-    return hr;
 }
 }
 
diff --git a/sal/qa/systools/test_retry_if_failed.cxx 
b/sal/qa/systools/test_retry_if_failed.cxx
index 845cba83092d..7df83cb229a1 100644
--- a/sal/qa/systools/test_retry_if_failed.cxx
+++ b/sal/qa/systools/test_retry_if_failed.cxx
@@ -13,19 +13,22 @@
 
 namespace test_systools
 {
+constexpr int ClockRes = 15; // default interval between system clock ticks is 
~15 ms on x86
+
 class test_retry_if_failed : public CppUnit::TestFixture
 {
 public:
     void test_success()
     {
         const DWORD nTicksBefore = GetTickCount();
-        HRESULT hr = sal::systools::RetryIfFailed(10, 100, Tester(5));
+        HRESULT hr = sal::systools::RetryIfFailed(10, 200, Tester(5));
         const DWORD nTicksAfter = GetTickCount();
         const DWORD nTicksElapsed = nTicksAfter > nTicksBefore ? nTicksAfter - 
nTicksBefore
                                                                : 
std::numeric_limits<DWORD>::max()
                                                                      - 
nTicksBefore + nTicksAfter;
         CPPUNIT_ASSERT(SUCCEEDED(hr));
-        CPPUNIT_ASSERT(nTicksElapsed >= 400); // 5 attempts, 4 sleeps by 100 ms
+        // 5 attempts, 4 sleeps by ~200 ms
+        CPPUNIT_ASSERT_GREATER(DWORD(800 - ClockRes), nTicksElapsed);
     }
 
     void test_failure()
@@ -37,7 +40,8 @@ public:
                                                                : 
std::numeric_limits<DWORD>::max()
                                                                      - 
nTicksBefore + nTicksAfter;
         CPPUNIT_ASSERT(FAILED(hr));
-        CPPUNIT_ASSERT(nTicksElapsed >= 900); // 10 attempts, 9 sleeps by 100 
ms
+        // 1 + 10 attempts, 10 sleeps by ~100 ms
+        CPPUNIT_ASSERT_GREATER(DWORD(1000 - ClockRes), nTicksElapsed);
     }
 
     CPPUNIT_TEST_SUITE(test_retry_if_failed);
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to