failure_injector_ut.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "failure_injector.h"
  2. #include <yql/essentials/utils/log/log.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/datetime/base.h>
  5. #include <chrono>
  6. using namespace NYql;
  7. using namespace NYql::NLog;
  8. using namespace std::chrono;
  9. // do nothing
  10. void OnReach(std::atomic<bool>& called) {
  11. called.store(true);
  12. }
  13. void SetUpLogger() {
  14. TString logType = "cout";
  15. NLog::InitLogger(logType, false);
  16. NLog::EComponentHelpers::ForEach([](NLog::EComponent component) {
  17. NLog::YqlLogger().SetComponentLevel(component, ELevel::DEBUG);
  18. });
  19. }
  20. Y_UNIT_TEST_SUITE(TFailureInjectorTests) {
  21. Y_UNIT_TEST(BasicFailureTest) {
  22. SetUpLogger();
  23. std::atomic<bool> called;
  24. called.store(false);
  25. auto behavior = [&called] { OnReach(called); };
  26. TFailureInjector::Reach("misc_failure", behavior);
  27. UNIT_ASSERT_EQUAL(false, called.load());
  28. TFailureInjector::Activate();
  29. TFailureInjector::Set("misc_failure", 0, 1);
  30. TFailureInjector::Reach("misc_failure", behavior);
  31. UNIT_ASSERT_EQUAL(true, called.load());
  32. }
  33. Y_UNIT_TEST(CheckSkipTest) {
  34. SetUpLogger();
  35. std::atomic<bool> called;
  36. called.store(false);
  37. auto behavior = [&called] { OnReach(called); };
  38. TFailureInjector::Activate();
  39. TFailureInjector::Set("misc_failure", 1, 1);
  40. TFailureInjector::Reach("misc_failure", behavior);
  41. UNIT_ASSERT_EQUAL(false, called.load());
  42. TFailureInjector::Reach("misc_failure", behavior);
  43. UNIT_ASSERT_EQUAL(true, called.load());
  44. }
  45. Y_UNIT_TEST(CheckFailCountTest) {
  46. SetUpLogger();
  47. int called = 0;
  48. auto behavior = [&called] { ++called; };
  49. TFailureInjector::Activate();
  50. TFailureInjector::Set("misc_failure", 1, 2);
  51. TFailureInjector::Reach("misc_failure", behavior);
  52. UNIT_ASSERT_EQUAL(0, called);
  53. TFailureInjector::Reach("misc_failure", behavior);
  54. UNIT_ASSERT_EQUAL(1, called);
  55. TFailureInjector::Reach("misc_failure", behavior);
  56. UNIT_ASSERT_EQUAL(2, called);
  57. TFailureInjector::Reach("misc_failure", behavior);
  58. UNIT_ASSERT_EQUAL(2, called);
  59. TFailureInjector::Reach("misc_failure", behavior);
  60. UNIT_ASSERT_EQUAL(2, called);
  61. }
  62. Y_UNIT_TEST(SlowDownTest) {
  63. SetUpLogger();
  64. TFailureInjector::Activate();
  65. TFailureInjector::Set("misc_failure", 0, 1);
  66. auto start = system_clock::now();
  67. TFailureInjector::Reach("misc_failure", [] { ::Sleep(TDuration::Seconds(5)); });
  68. auto finish = system_clock::now();
  69. auto duration = duration_cast<std::chrono::seconds>(finish - start);
  70. YQL_LOG(DEBUG) << "Duration :" << duration.count();
  71. UNIT_ASSERT_GE(duration.count(), 5);
  72. }
  73. }