PeriodicSinkTests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifdef _USE_CPPUNIT
  14. #include <cppunit/TestFixture.h>
  15. #include "unittests.hpp"
  16. #include <algorithm>
  17. #include "jptree.hpp"
  18. #include "jmetrics.hpp"
  19. using namespace hpccMetrics;
  20. class PeriodicTestSink : public PeriodicMetricSink
  21. {
  22. public:
  23. explicit PeriodicTestSink(const char *name, const IPropertyTree *pSettingsTree) :
  24. PeriodicMetricSink(name, "test", pSettingsTree) { }
  25. ~PeriodicTestSink() = default;
  26. protected:
  27. virtual void prepareToStartCollecting() override
  28. {
  29. prepareCalled = true;
  30. numCollections = 0;
  31. }
  32. virtual void collectingHasStopped() override
  33. {
  34. stopCollectionNotificationCalled = true;
  35. }
  36. void doCollection() override
  37. {
  38. numCollections++;
  39. }
  40. public:
  41. bool prepareCalled = false;
  42. bool stopCollectionNotificationCalled = false;
  43. unsigned numCollections = 0;
  44. };
  45. const char *periodicSinkSettingsTestYml = R"!!(period: 2
  46. )!!";
  47. static const unsigned period = 2;
  48. class PeriodicSinkTests : public CppUnit::TestFixture
  49. {
  50. public:
  51. PeriodicSinkTests()
  52. {
  53. //
  54. // Load the settings then set the period using the global var
  55. Owned<IPropertyTree> pSettings = createPTreeFromYAMLString(periodicSinkSettingsTestYml, ipt_none, ptr_ignoreWhiteSpace, nullptr);
  56. pSettings->setPropInt("@period", period);
  57. pPeriodicTestSink = new PeriodicTestSink("periodic_test_sink", pSettings);
  58. periodicSinkTestReporter.addSink(pPeriodicTestSink, "periodic_test_sink");
  59. }
  60. ~PeriodicSinkTests() = default;
  61. CPPUNIT_TEST_SUITE(PeriodicSinkTests);
  62. CPPUNIT_TEST(Test_sets_period_correctly);
  63. CPPUNIT_TEST_SUITE_END();
  64. protected:
  65. void Test_sets_period_correctly()
  66. {
  67. //
  68. // To test setting the period correctly, start collection and delay a multiple of that period.
  69. // Stop collection and ask the test sink how many collections were done. If the count is +/- 1
  70. // from the wait period multiple used, then we are close enough
  71. unsigned multiple = 3;
  72. periodicSinkTestReporter.startCollecting();
  73. //
  74. // Check that the sink called to prepare for collection
  75. CPPUNIT_ASSERT(pPeriodicTestSink->prepareCalled);
  76. // wait... then stop collecting
  77. sleep(multiple * period);
  78. periodicSinkTestReporter.stopCollecting();
  79. unsigned numCollections = pPeriodicTestSink->numCollections;
  80. bool numReportsCorrect = (numCollections >= multiple-1) && (numCollections <= multiple+1);
  81. CPPUNIT_ASSERT_EQUAL(true, numReportsCorrect);
  82. //
  83. // Verify collection was stopped
  84. CPPUNIT_ASSERT(pPeriodicTestSink->stopCollectionNotificationCalled);
  85. }
  86. protected:
  87. MetricsReporter periodicSinkTestReporter;
  88. PeriodicTestSink *pPeriodicTestSink = nullptr;
  89. };
  90. CPPUNIT_TEST_SUITE_REGISTRATION( PeriodicSinkTests );
  91. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PeriodicSinkTests, "PeriodicSinkTests" );
  92. #endif