2014-09-25 11:22:39 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
/*
|
2015-10-01 13:20:07 +02:00
|
|
|
* Timers are evil beasts across platforms...
|
2014-09-25 11:22:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/bootstrapfixture.hxx>
|
|
|
|
|
|
|
|
#include <osl/thread.hxx>
|
|
|
|
#include <salhelper/thread.hxx>
|
2016-03-22 03:25:02 +05:30
|
|
|
#include <chrono>
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
#include <vcl/timer.hxx>
|
2015-01-14 13:11:28 +00:00
|
|
|
#include <vcl/idle.hxx>
|
2014-09-25 11:22:39 +01:00
|
|
|
#include <vcl/svapp.hxx>
|
2015-06-10 12:08:00 +01:00
|
|
|
#include "svdata.hxx"
|
|
|
|
#include "salinst.hxx"
|
|
|
|
|
|
|
|
// #define TEST_WATCHDOG
|
2015-08-28 08:00:20 +01:00
|
|
|
|
|
|
|
// Enables timer tests that appear to provoke windows under load unduly.
|
2015-08-25 11:04:38 +02:00
|
|
|
//#define TEST_TIMERPRECISION
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
/// Avoid our timer tests just wedging the build if they fail.
|
|
|
|
class WatchDog : public osl::Thread
|
|
|
|
{
|
|
|
|
sal_Int32 mnSeconds;
|
|
|
|
public:
|
2015-05-25 09:35:57 +01:00
|
|
|
explicit WatchDog(sal_Int32 nSeconds) :
|
2014-09-25 11:22:39 +01:00
|
|
|
Thread(),
|
|
|
|
mnSeconds( nSeconds )
|
|
|
|
{
|
|
|
|
create();
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void SAL_CALL run() override
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
2016-03-22 03:25:02 +05:30
|
|
|
osl::Thread::wait( std::chrono::seconds(mnSeconds) );
|
2015-08-28 09:40:02 +03:00
|
|
|
fprintf(stderr, "ERROR: WatchDog timer thread expired, failing the test!\n");
|
|
|
|
fflush(stderr);
|
2014-09-25 11:22:39 +01:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("watchdog triggered", false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-27 14:50:57 +02:00
|
|
|
static WatchDog aWatchDog( 120 ); // random high number in secs
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
class TimerTest : public test::BootstrapFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerTest() : BootstrapFixture(true, false) {}
|
|
|
|
|
2015-06-10 12:08:00 +01:00
|
|
|
void testIdleMainloop();
|
2014-11-05 17:57:15 +01:00
|
|
|
void testIdle();
|
2014-09-25 23:00:59 +02:00
|
|
|
#ifdef TEST_WATCHDOG
|
2014-09-25 11:22:39 +01:00
|
|
|
void testWatchdog();
|
2014-09-25 23:00:59 +02:00
|
|
|
#endif
|
2014-09-25 11:22:39 +01:00
|
|
|
void testDurations();
|
2015-08-28 08:00:20 +01:00
|
|
|
#ifdef TEST_TIMERPRECISION
|
2014-09-25 11:22:39 +01:00
|
|
|
void testAutoTimer();
|
2015-08-20 21:06:06 -04:00
|
|
|
void testMultiAutoTimers();
|
2015-08-28 08:00:20 +01:00
|
|
|
#endif
|
2014-09-25 11:22:39 +01:00
|
|
|
void testRecursiveTimer();
|
|
|
|
void testSlowTimerCallback();
|
2016-12-09 12:12:29 +02:00
|
|
|
void testTriggerIdleFromIdle();
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(TimerTest);
|
2014-11-05 17:57:15 +01:00
|
|
|
CPPUNIT_TEST(testIdle);
|
2015-06-10 12:08:00 +01:00
|
|
|
CPPUNIT_TEST(testIdleMainloop);
|
2014-09-25 23:00:59 +02:00
|
|
|
#ifdef TEST_WATCHDOG
|
|
|
|
CPPUNIT_TEST(testWatchdog);
|
|
|
|
#endif
|
2014-09-25 11:22:39 +01:00
|
|
|
CPPUNIT_TEST(testDurations);
|
2015-08-28 08:00:20 +01:00
|
|
|
#ifdef TEST_TIMERPRECISION
|
2014-09-25 11:22:39 +01:00
|
|
|
CPPUNIT_TEST(testAutoTimer);
|
2015-08-20 21:06:06 -04:00
|
|
|
CPPUNIT_TEST(testMultiAutoTimers);
|
2015-08-28 08:00:20 +01:00
|
|
|
#endif
|
2014-09-25 11:22:39 +01:00
|
|
|
CPPUNIT_TEST(testRecursiveTimer);
|
|
|
|
CPPUNIT_TEST(testSlowTimerCallback);
|
2016-12-09 12:12:29 +02:00
|
|
|
CPPUNIT_TEST(testTriggerIdleFromIdle);
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
};
|
|
|
|
|
2014-09-25 23:00:59 +02:00
|
|
|
#ifdef TEST_WATCHDOG
|
2014-09-25 11:22:39 +01:00
|
|
|
void TimerTest::testWatchdog()
|
|
|
|
{
|
|
|
|
// out-wait the watchdog.
|
2016-03-22 03:25:02 +05:30
|
|
|
osl::Thread::wait( std::chrono::seconds(12) );
|
2014-09-25 11:22:39 +01:00
|
|
|
}
|
2014-09-25 23:00:59 +02:00
|
|
|
#endif
|
2014-09-25 11:22:39 +01:00
|
|
|
|
2014-11-05 17:57:15 +01:00
|
|
|
|
2014-11-05 20:51:21 +00:00
|
|
|
class IdleBool : public Idle
|
2014-11-05 17:57:15 +01:00
|
|
|
{
|
2014-11-05 20:51:21 +00:00
|
|
|
bool &mrBool;
|
|
|
|
public:
|
2015-05-25 09:35:57 +01:00
|
|
|
explicit IdleBool( bool &rBool ) :
|
2015-01-14 13:11:28 +00:00
|
|
|
Idle(), mrBool( rBool )
|
2014-11-05 20:51:21 +00:00
|
|
|
{
|
2015-02-26 07:28:54 +00:00
|
|
|
SetPriority( SchedulerPriority::LOWEST );
|
2014-11-05 20:51:21 +00:00
|
|
|
Start();
|
|
|
|
mrBool = false;
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void Invoke() override
|
2014-11-05 20:51:21 +00:00
|
|
|
{
|
|
|
|
mrBool = true;
|
|
|
|
Application::EndYield();
|
|
|
|
}
|
|
|
|
};
|
2014-11-05 17:57:15 +01:00
|
|
|
|
2014-11-05 20:51:21 +00:00
|
|
|
void TimerTest::testIdle()
|
|
|
|
{
|
|
|
|
bool bTriggered = false;
|
|
|
|
IdleBool aTest( bTriggered );
|
2016-09-06 10:44:05 +02:00
|
|
|
Scheduler::ProcessTaskScheduling( true );
|
2015-06-10 12:08:00 +01:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("idle triggered", bTriggered);
|
|
|
|
}
|
|
|
|
|
|
|
|
// tdf#91727
|
|
|
|
void TimerTest::testIdleMainloop()
|
|
|
|
{
|
2016-02-16 14:14:43 +02:00
|
|
|
#ifndef _WIN32
|
2015-06-10 12:08:00 +01:00
|
|
|
bool bTriggered = false;
|
|
|
|
IdleBool aTest( bTriggered );
|
2015-06-12 13:08:44 +01:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and toggle bDone
|
2015-06-10 12:08:00 +01:00
|
|
|
while (!bTriggered)
|
|
|
|
{
|
|
|
|
ImplSVData* pSVData = ImplGetSVData();
|
|
|
|
|
|
|
|
// can't test this via Application::Yield since this
|
|
|
|
// also processes all tasks directly via the scheduler.
|
|
|
|
pSVData->maAppData.mnDispatchLevel++;
|
2015-06-26 13:01:51 +02:00
|
|
|
pSVData->mpDefInst->DoYield(true, false, 0);
|
2015-06-10 12:08:00 +01:00
|
|
|
pSVData->maAppData.mnDispatchLevel--;
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("mainloop idle triggered", bTriggered);
|
2015-08-20 21:06:06 -04:00
|
|
|
#endif
|
2014-11-05 17:57:15 +01:00
|
|
|
}
|
|
|
|
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
class TimerBool : public Timer
|
|
|
|
{
|
|
|
|
bool &mrBool;
|
|
|
|
public:
|
|
|
|
TimerBool( sal_uLong nMS, bool &rBool ) :
|
|
|
|
Timer(), mrBool( rBool )
|
|
|
|
{
|
|
|
|
SetTimeout( nMS );
|
|
|
|
Start();
|
|
|
|
mrBool = false;
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void Invoke() override
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
|
|
|
mrBool = true;
|
|
|
|
Application::EndYield();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TimerTest::testDurations()
|
|
|
|
{
|
|
|
|
static const sal_uLong aDurations[] = { 0, 1, 500, 1000 };
|
|
|
|
for (size_t i = 0; i < SAL_N_ELEMENTS( aDurations ); i++)
|
|
|
|
{
|
|
|
|
bool bDone = false;
|
|
|
|
TimerBool aTimer( aDurations[i], bDone );
|
2014-11-05 10:15:38 +00:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and toggle bDone
|
2014-09-25 11:22:39 +01:00
|
|
|
while( !bDone )
|
|
|
|
{
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AutoTimerCount : public AutoTimer
|
|
|
|
{
|
|
|
|
sal_Int32 &mrCount;
|
|
|
|
public:
|
|
|
|
AutoTimerCount( sal_uLong nMS, sal_Int32 &rCount ) :
|
|
|
|
AutoTimer(), mrCount( rCount )
|
|
|
|
{
|
|
|
|
SetTimeout( nMS );
|
|
|
|
Start();
|
|
|
|
mrCount = 0;
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void Invoke() override
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
|
|
|
mrCount++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-28 08:00:20 +01:00
|
|
|
#ifdef TEST_TIMERPRECISION
|
|
|
|
|
2014-09-25 11:22:39 +01:00
|
|
|
void TimerTest::testAutoTimer()
|
|
|
|
{
|
2015-08-20 21:06:06 -04:00
|
|
|
const sal_Int32 nDurationMs = 30;
|
|
|
|
const sal_Int32 nEventsCount = 5;
|
|
|
|
const double exp = (nDurationMs * nEventsCount);
|
|
|
|
|
2014-09-25 11:22:39 +01:00
|
|
|
sal_Int32 nCount = 0;
|
2015-08-20 21:06:06 -04:00
|
|
|
std::ostringstream msg;
|
|
|
|
|
|
|
|
// Repeat when we have random latencies.
|
|
|
|
// This is expected on non-realtime OSes.
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
{
|
|
|
|
const auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
nCount = 0;
|
|
|
|
AutoTimerCount aCount(nDurationMs, nCount);
|
|
|
|
while (nCount < nEventsCount) {
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto end = std::chrono::high_resolution_clock::now();
|
2015-10-14 02:51:05 +02:00
|
|
|
double dur = std::chrono::duration<double, std::milli>(end - start).count();
|
2015-08-20 21:06:06 -04:00
|
|
|
|
|
|
|
msg << std::setprecision(2) << std::fixed
|
|
|
|
<< "periodic multi-timer - dur: "
|
|
|
|
<< dur << " (" << exp << ") ms." << std::endl;
|
|
|
|
|
|
|
|
// +/- 20% should be reasonable enough a margin.
|
|
|
|
if (dur >= (exp * 0.8) && dur <= (exp * 1.2))
|
|
|
|
{
|
|
|
|
// Success.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_FAIL(msg.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerTest::testMultiAutoTimers()
|
|
|
|
{
|
|
|
|
// The behavior of the timers change drastically
|
|
|
|
// when multiple timers are present.
|
|
|
|
// The worst, in my tests, is when two
|
|
|
|
// timers with 1ms period exist with a
|
|
|
|
// third of much longer period.
|
|
|
|
|
|
|
|
const sal_Int32 nDurationMsX = 5;
|
|
|
|
const sal_Int32 nDurationMsY = 10;
|
|
|
|
const sal_Int32 nDurationMs = 40;
|
|
|
|
const sal_Int32 nEventsCount = 5;
|
|
|
|
const double exp = (nDurationMs * nEventsCount);
|
|
|
|
const double expX = (exp / nDurationMsX);
|
|
|
|
const double expY = (exp / nDurationMsY);
|
|
|
|
|
|
|
|
sal_Int32 nCountX = 0;
|
|
|
|
sal_Int32 nCountY = 0;
|
|
|
|
sal_Int32 nCount = 0;
|
|
|
|
std::ostringstream msg;
|
|
|
|
|
|
|
|
// Repeat when we have random latencies.
|
|
|
|
// This is expected on non-realtime OSes.
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
{
|
|
|
|
nCountX = 0;
|
|
|
|
nCountY = 0;
|
|
|
|
nCount = 0;
|
|
|
|
|
|
|
|
const auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
AutoTimerCount aCountX(nDurationMsX, nCountX);
|
|
|
|
AutoTimerCount aCountY(nDurationMsY, nCountY);
|
|
|
|
|
|
|
|
AutoTimerCount aCount(nDurationMs, nCount);
|
2015-08-27 08:09:18 +01:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and toggle nCount
|
2015-08-20 21:06:06 -04:00
|
|
|
while (nCount < nEventsCount) {
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto end = std::chrono::high_resolution_clock::now();
|
2015-10-14 02:51:05 +02:00
|
|
|
double dur = std::chrono::duration<double, std::milli>(end - start).count();
|
2015-08-20 21:06:06 -04:00
|
|
|
|
|
|
|
msg << std::setprecision(2) << std::fixed << "periodic multi-timer - dur: "
|
|
|
|
<< dur << " (" << exp << ") ms, nCount: " << nCount
|
|
|
|
<< " (" << nEventsCount << "), nCountX: " << nCountX
|
|
|
|
<< " (" << expX << "), nCountY: " << nCountY
|
|
|
|
<< " (" << expY << ")." << std::endl;
|
|
|
|
|
|
|
|
// +/- 20% should be reasonable enough a margin.
|
|
|
|
if (dur >= (exp * 0.8) && dur <= (exp * 1.2) &&
|
|
|
|
nCountX >= (expX * 0.8) && nCountX <= (expX * 1.2) &&
|
|
|
|
nCountY >= (expY * 0.8) && nCountY <= (expY * 1.2))
|
|
|
|
{
|
|
|
|
// Success.
|
|
|
|
return;
|
|
|
|
}
|
2014-09-25 11:22:39 +01:00
|
|
|
}
|
2015-08-20 21:06:06 -04:00
|
|
|
|
|
|
|
CPPUNIT_FAIL(msg.str().c_str());
|
2014-09-25 11:22:39 +01:00
|
|
|
}
|
2015-08-28 08:00:20 +01:00
|
|
|
#endif // TEST_TIMERPRECISION
|
2014-09-25 11:22:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class YieldTimer : public Timer
|
|
|
|
{
|
|
|
|
public:
|
2015-05-25 09:35:57 +01:00
|
|
|
explicit YieldTimer( sal_uLong nMS ) : Timer()
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
|
|
|
SetTimeout( nMS );
|
|
|
|
Start();
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void Invoke() override
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TimerTest::testRecursiveTimer()
|
|
|
|
{
|
|
|
|
sal_Int32 nCount = 0;
|
|
|
|
YieldTimer aCount(5);
|
|
|
|
AutoTimerCount aCountUp( 3, nCount );
|
2014-11-05 10:15:38 +00:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and increment nCount
|
2014-09-25 11:22:39 +01:00
|
|
|
while (nCount < 20)
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SlowCallbackTimer : public Timer
|
|
|
|
{
|
|
|
|
bool &mbSlow;
|
|
|
|
public:
|
|
|
|
SlowCallbackTimer( sal_uLong nMS, bool &bBeenSlow ) :
|
|
|
|
Timer(), mbSlow( bBeenSlow )
|
|
|
|
{
|
|
|
|
SetTimeout( nMS );
|
|
|
|
Start();
|
|
|
|
mbSlow = false;
|
|
|
|
}
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void Invoke() override
|
2014-09-25 11:22:39 +01:00
|
|
|
{
|
2016-03-22 03:25:02 +05:30
|
|
|
osl::Thread::wait( std::chrono::seconds(1) );
|
2014-09-25 11:22:39 +01:00
|
|
|
mbSlow = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TimerTest::testSlowTimerCallback()
|
|
|
|
{
|
|
|
|
bool bBeenSlow = false;
|
|
|
|
sal_Int32 nCount = 0;
|
|
|
|
AutoTimerCount aHighFreq(1, nCount);
|
|
|
|
SlowCallbackTimer aSlow(250, bBeenSlow);
|
2014-11-05 10:15:38 +00:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and toggle bBeenSlow
|
2014-09-25 11:22:39 +01:00
|
|
|
while (!bBeenSlow)
|
|
|
|
Application::Yield();
|
2014-11-05 10:15:38 +00:00
|
|
|
// coverity[loop_top] - Application::Yield allows the timer to fire and increment nCount
|
2014-09-25 11:22:39 +01:00
|
|
|
while (nCount < 200)
|
|
|
|
Application::Yield();
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:12:29 +02:00
|
|
|
|
|
|
|
class TriggerIdleFromIdle : public Idle
|
|
|
|
{
|
|
|
|
bool* mpTriggered;
|
|
|
|
TriggerIdleFromIdle* mpOther;
|
|
|
|
public:
|
|
|
|
explicit TriggerIdleFromIdle( bool* pTriggered, TriggerIdleFromIdle* pOther ) :
|
|
|
|
Idle(), mpTriggered(pTriggered), mpOther(pOther)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void Invoke() override
|
|
|
|
{
|
|
|
|
Start();
|
|
|
|
if (mpOther)
|
|
|
|
mpOther->Start();
|
|
|
|
Application::Yield();
|
|
|
|
if (mpTriggered)
|
|
|
|
*mpTriggered = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TimerTest::testTriggerIdleFromIdle()
|
|
|
|
{
|
|
|
|
bool bTriggered1 = false;
|
|
|
|
bool bTriggered2 = false;
|
|
|
|
TriggerIdleFromIdle aTest2( &bTriggered2, nullptr );
|
|
|
|
TriggerIdleFromIdle aTest1( &bTriggered1, &aTest2 );
|
|
|
|
aTest1.Start();
|
|
|
|
Application::Yield();
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("idle triggered", bTriggered1);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("idle triggered", bTriggered2);
|
|
|
|
}
|
|
|
|
|
2014-09-25 11:22:39 +01:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(TimerTest);
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|