event_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "event.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/thread/pool.h>
  4. #include <atomic>
  5. namespace {
  6. struct TSharedData {
  7. std::atomic<size_t> Counter = 0;
  8. TManualEvent event;
  9. bool failed = false;
  10. };
  11. struct TThreadTask: public IObjectInQueue {
  12. public:
  13. TThreadTask(TSharedData& data, size_t id)
  14. : Data_(data)
  15. , Id_(id)
  16. {
  17. }
  18. void Process(void*) override {
  19. THolder<TThreadTask> This(this);
  20. if (Id_ == 0) {
  21. usleep(100);
  22. bool cond = Data_.Counter.load() == 0;
  23. if (!cond) {
  24. Data_.failed = true;
  25. }
  26. Data_.event.Signal();
  27. } else {
  28. while (!Data_.event.WaitT(TDuration::Seconds(100))) {
  29. }
  30. Data_.Counter += Id_;
  31. }
  32. }
  33. private:
  34. TSharedData& Data_;
  35. size_t Id_;
  36. };
  37. class TSignalTask: public IObjectInQueue {
  38. private:
  39. TManualEvent& Ev_;
  40. public:
  41. TSignalTask(TManualEvent& ev)
  42. : Ev_(ev)
  43. {
  44. }
  45. void Process(void*) override {
  46. Ev_.Signal();
  47. }
  48. };
  49. class TOwnerTask: public IObjectInQueue {
  50. public:
  51. TManualEvent Barrier;
  52. THolder<TManualEvent> Ev;
  53. public:
  54. TOwnerTask()
  55. : Ev(new TManualEvent)
  56. {
  57. }
  58. void Process(void*) override {
  59. Ev->WaitI();
  60. Ev.Destroy();
  61. }
  62. };
  63. } // namespace
  64. Y_UNIT_TEST_SUITE(EventTest) {
  65. Y_UNIT_TEST(WaitAndSignalTest) {
  66. TSharedData data;
  67. TThreadPool queue;
  68. queue.Start(5);
  69. for (size_t i = 0; i < 5; ++i) {
  70. UNIT_ASSERT(queue.Add(new TThreadTask(data, i)));
  71. }
  72. queue.Stop();
  73. UNIT_ASSERT(data.Counter.load() == 10);
  74. UNIT_ASSERT(!data.failed);
  75. }
  76. Y_UNIT_TEST(ConcurrentSignalAndWaitTest) {
  77. // test for problem detected by thread-sanitizer (signal/wait race) SEARCH-2113
  78. const size_t limit = 200;
  79. TManualEvent event[limit];
  80. TThreadPool queue;
  81. queue.Start(limit);
  82. TVector<THolder<IObjectInQueue>> tasks;
  83. for (size_t i = 0; i < limit; ++i) {
  84. tasks.emplace_back(MakeHolder<TSignalTask>(event[i]));
  85. UNIT_ASSERT(queue.Add(tasks.back().Get()));
  86. }
  87. for (size_t i = limit; i != 0; --i) {
  88. UNIT_ASSERT(event[i - 1].WaitT(TDuration::Seconds(90)));
  89. }
  90. queue.Stop();
  91. }
  92. /** Test for a problem: http://nga.at.yandex-team.ru/5772 */
  93. Y_UNIT_TEST(DestructorBeforeSignalFinishTest) {
  94. return;
  95. TVector<THolder<IObjectInQueue>> tasks;
  96. for (size_t i = 0; i < 1000; ++i) {
  97. auto owner = MakeHolder<TOwnerTask>();
  98. tasks.emplace_back(MakeHolder<TSignalTask>(*owner->Ev));
  99. tasks.emplace_back(std::move(owner));
  100. }
  101. TThreadPool queue;
  102. queue.Start(4);
  103. for (auto& task : tasks) {
  104. UNIT_ASSERT(queue.Add(task.Get()));
  105. }
  106. queue.Stop();
  107. }
  108. } // Y_UNIT_TEST_SUITE(EventTest)