ActivityTest.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // ActivityTest.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. #include "ActivityTest.h"
  10. #include "CppUnit/TestCaller.h"
  11. #include "CppUnit/TestSuite.h"
  12. #include "Poco/Activity.h"
  13. #include "Poco/Thread.h"
  14. using Poco::Activity;
  15. using Poco::Thread;
  16. namespace
  17. {
  18. class ActiveObject
  19. {
  20. public:
  21. ActiveObject():
  22. _activity(this, &ActiveObject::run),
  23. _count(0)
  24. {
  25. }
  26. ~ActiveObject()
  27. {
  28. }
  29. Activity<ActiveObject>& activity()
  30. {
  31. return _activity;
  32. }
  33. Poco::UInt64 count() const
  34. {
  35. return _count;
  36. }
  37. protected:
  38. void run()
  39. {
  40. while (!_activity.isStopped())
  41. ++_count;
  42. }
  43. private:
  44. Activity<ActiveObject> _activity;
  45. Poco::UInt64 _count;
  46. };
  47. }
  48. ActivityTest::ActivityTest(const std::string& name): CppUnit::TestCase(name)
  49. {
  50. }
  51. ActivityTest::~ActivityTest()
  52. {
  53. }
  54. void ActivityTest::testActivity()
  55. {
  56. ActiveObject activeObj;
  57. assertTrue (activeObj.activity().isStopped());
  58. activeObj.activity().start();
  59. assertTrue (!activeObj.activity().isStopped());
  60. Thread::sleep(1000);
  61. assertTrue (activeObj.activity().isRunning());
  62. activeObj.activity().stop();
  63. activeObj.activity().wait();
  64. assertTrue (activeObj.count() > 0);
  65. }
  66. void ActivityTest::setUp()
  67. {
  68. }
  69. void ActivityTest::tearDown()
  70. {
  71. }
  72. CppUnit::Test* ActivityTest::suite()
  73. {
  74. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ActivityTest");
  75. CppUnit_addTest(pSuite, ActivityTest, testActivity);
  76. return pSuite;
  77. }