Bump version to 24.04.3.4
[LibreOffice.git] / sal / qa / systools / test_retry_if_failed.cxx
bloba218b3e9ff9979e2bd82f87e0b171fbb1d7a758b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <cppunit/plugin/TestPlugIn.h>
12 #include <systools/win32/retry_if_failed.hxx>
14 namespace test_systools
16 constexpr int ClockRes = 15; // default interval between system clock ticks is ~15 ms on x86
18 class test_retry_if_failed : public CppUnit::TestFixture
20 public:
21 void test_success()
23 unsigned counter = 0;
24 const DWORD nTicksBefore = GetTickCount();
25 HRESULT hr = sal::systools::RetryIfFailed(10, 200, Tester(5, counter));
26 const DWORD nTicksAfter = GetTickCount();
27 const DWORD nTicksElapsed = nTicksAfter > nTicksBefore ? nTicksAfter - nTicksBefore
28 : std::numeric_limits<DWORD>::max()
29 - nTicksBefore + nTicksAfter;
30 CPPUNIT_ASSERT(SUCCEEDED(hr));
31 // 5 attempts, 4 sleeps by ~200 ms
32 CPPUNIT_ASSERT_EQUAL(5U, counter);
33 CPPUNIT_ASSERT_GREATER(DWORD(800 - ClockRes), nTicksElapsed);
36 void test_failure()
38 unsigned counter = 0;
39 const DWORD nTicksBefore = GetTickCount();
40 HRESULT hr = sal::systools::RetryIfFailed(10, 100, Tester(15, counter));
41 const DWORD nTicksAfter = GetTickCount();
42 const DWORD nTicksElapsed = nTicksAfter > nTicksBefore ? nTicksAfter - nTicksBefore
43 : std::numeric_limits<DWORD>::max()
44 - nTicksBefore + nTicksAfter;
45 CPPUNIT_ASSERT(FAILED(hr));
46 // 1 + 10 attempts, 10 sleeps by ~100 ms
47 CPPUNIT_ASSERT_EQUAL(11U, counter);
48 CPPUNIT_ASSERT_GREATER(DWORD(1000 - ClockRes), nTicksElapsed);
51 CPPUNIT_TEST_SUITE(test_retry_if_failed);
52 CPPUNIT_TEST(test_success);
53 CPPUNIT_TEST(test_failure);
54 CPPUNIT_TEST_SUITE_END();
56 private:
57 struct Tester
59 Tester(unsigned triesBeforeSuccess, unsigned& counter)
60 : m_nTriesBeforeSuccess(triesBeforeSuccess)
61 , m_rCounter(counter)
65 HRESULT operator()() { return ++m_rCounter >= m_nTriesBeforeSuccess ? S_OK : E_FAIL; }
67 unsigned m_nTriesBeforeSuccess;
68 unsigned& m_rCounter;
72 CPPUNIT_TEST_SUITE_REGISTRATION(test_systools::test_retry_if_failed);
74 } // namespace test_systools
76 CPPUNIT_PLUGIN_IMPLEMENT();
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */