From cf1c835e8016f8f1eefea6d625a913c0ac343a63 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Tue, 2 Mar 2021 15:00:56 +0300 Subject: tdf#116983 tdf#136175: retry if failed Debugging the test case from the latter bug report shows that indeed the call to OleGetClipboard may fail first time, as jasonkres had suspected in the former bug. So follow the suggestion in tdf#116983, and retry the failing calls several times in case of failure. Many thanks to Telesto for preparing a clear bug report with reliable test case. Co-authored-by: jasonkres Change-Id: Ib3c497da830bc5faac586bcfe1eededa54bfa117 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111825 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/CppunitTest_sal_retry_if_failed.mk | 16 +++++++ sal/Module_sal.mk | 1 + sal/qa/systools/test_retry_if_failed.cxx | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 sal/CppunitTest_sal_retry_if_failed.mk create mode 100644 sal/qa/systools/test_retry_if_failed.cxx (limited to 'sal') diff --git a/sal/CppunitTest_sal_retry_if_failed.mk b/sal/CppunitTest_sal_retry_if_failed.mk new file mode 100644 index 000000000000..6e131afb50dc --- /dev/null +++ b/sal/CppunitTest_sal_retry_if_failed.mk @@ -0,0 +1,16 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; fill-column: 100 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +$(eval $(call gb_CppunitTest_CppunitTest,sal_retry_if_failed)) + +$(eval $(call gb_CppunitTest_add_exception_objects,sal_retry_if_failed,\ + sal/qa/systools/test_retry_if_failed \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/sal/Module_sal.mk b/sal/Module_sal.mk index 7611bc950f07..1a190037f05b 100644 --- a/sal/Module_sal.mk +++ b/sal/Module_sal.mk @@ -26,6 +26,7 @@ $(eval $(call gb_Module_add_targets,sal,\ $(eval $(call gb_Module_add_check_targets,sal,\ $(if $(filter TRUE,$(DISABLE_DYNLOADING)),,CppunitTest_Module_DLL) \ $(if $(filter WNT,$(OS)),CppunitTest_sal_comtools) \ + $(if $(filter WNT,$(OS)),CppunitTest_sal_retry_if_failed) \ CppunitTest_sal_osl_security \ CppunitTest_sal_osl \ CppunitTest_sal_rtl \ diff --git a/sal/qa/systools/test_retry_if_failed.cxx b/sal/qa/systools/test_retry_if_failed.cxx new file mode 100644 index 000000000000..845cba83092d --- /dev/null +++ b/sal/qa/systools/test_retry_if_failed.cxx @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include + +namespace test_systools +{ +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)); + const DWORD nTicksAfter = GetTickCount(); + const DWORD nTicksElapsed = nTicksAfter > nTicksBefore ? nTicksAfter - nTicksBefore + : std::numeric_limits::max() + - nTicksBefore + nTicksAfter; + CPPUNIT_ASSERT(SUCCEEDED(hr)); + CPPUNIT_ASSERT(nTicksElapsed >= 400); // 5 attempts, 4 sleeps by 100 ms + } + + void test_failure() + { + const DWORD nTicksBefore = GetTickCount(); + HRESULT hr = sal::systools::RetryIfFailed(10, 100, Tester(15)); + const DWORD nTicksAfter = GetTickCount(); + const DWORD nTicksElapsed = nTicksAfter > nTicksBefore ? nTicksAfter - nTicksBefore + : std::numeric_limits::max() + - nTicksBefore + nTicksAfter; + CPPUNIT_ASSERT(FAILED(hr)); + CPPUNIT_ASSERT(nTicksElapsed >= 900); // 10 attempts, 9 sleeps by 100 ms + } + + CPPUNIT_TEST_SUITE(test_retry_if_failed); + CPPUNIT_TEST(test_success); + CPPUNIT_TEST(test_failure); + CPPUNIT_TEST_SUITE_END(); + +private: + struct Tester + { + Tester(unsigned triesBeforeSuccess) + : m_nTriesBeforeSuccess(triesBeforeSuccess) + { + } + + HRESULT operator()() + { + return ++m_nTriesAttempted >= m_nTriesBeforeSuccess ? S_OK : E_FAIL; + } + + unsigned m_nTriesBeforeSuccess; + unsigned m_nTriesAttempted = 0; + }; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(test_systools::test_retry_if_failed); + +} // namespace test_systools + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit