| 1 | // |
|---|---|
| 2 | // TimerTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "TimerTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Thread.h" |
| 15 | #include "Poco/Stopwatch.h" |
| 16 | |
| 17 | |
| 18 | using Poco::Timer; |
| 19 | using Poco::TimerCallback; |
| 20 | using Poco::Thread; |
| 21 | using Poco::Stopwatch; |
| 22 | |
| 23 | |
| 24 | TimerTest::TimerTest(const std::string& rName): CppUnit::TestCase(rName) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | |
| 29 | TimerTest::~TimerTest() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void TimerTest::testTimer() |
| 35 | { |
| 36 | Timer t(100, 200); |
| 37 | assertTrue (t.getStartInterval() == 100); |
| 38 | assertTrue (t.getPeriodicInterval() == 200); |
| 39 | |
| 40 | Stopwatch sw; |
| 41 | TimerCallback<TimerTest> tc(*this, &TimerTest::onTimer); |
| 42 | sw.start(); |
| 43 | t.start(tc); |
| 44 | _event.wait(); |
| 45 | sw.stop(); |
| 46 | assertTrue (sw.elapsed() >= 80000 && sw.elapsed() < 120000); |
| 47 | sw.restart(); |
| 48 | _event.wait(); |
| 49 | sw.stop(); |
| 50 | assertTrue (sw.elapsed() >= 180000 && sw.elapsed() < 250000); |
| 51 | sw.restart(); |
| 52 | _event.wait(); |
| 53 | sw.stop(); |
| 54 | assertTrue (sw.elapsed() >= 180000 && sw.elapsed() < 250000); |
| 55 | t.stop(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void TimerTest::testDuplicateStop() |
| 60 | { |
| 61 | Timer t(100, 200); |
| 62 | t.stop(); |
| 63 | t.stop(); |
| 64 | |
| 65 | TimerCallback<TimerTest> tc(*this, &TimerTest::onTimer); |
| 66 | t.start(tc); |
| 67 | _event.wait(); |
| 68 | t.stop(); |
| 69 | t.stop(); |
| 70 | } |
| 71 | |
| 72 | void TimerTest::setUp() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void TimerTest::tearDown() |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void TimerTest::onTimer(Timer& timer) |
| 83 | { |
| 84 | _event.set(); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | CppUnit::Test* TimerTest::suite() |
| 89 | { |
| 90 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimerTest"); |
| 91 | |
| 92 | CppUnit_addTest(pSuite, TimerTest, testTimer); |
| 93 | CppUnit_addTest(pSuite, TimerTest, testDuplicateStop); |
| 94 | |
| 95 | return pSuite; |
| 96 | } |
| 97 |