basic_ut.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/generic/vector.h>
  3. #include <util/system/thread.h>
  4. #include "ut_helpers.h"
  5. template <typename TQueueType>
  6. class TQueueTestsInSingleThread: public TTestBase {
  7. private:
  8. using TSelf = TQueueTestsInSingleThread<TQueueType>;
  9. using TLink = TIntrusiveLink;
  10. UNIT_TEST_SUITE_DEMANGLE(TSelf);
  11. UNIT_TEST(OnePushOnePop)
  12. UNIT_TEST(OnePushOnePop_Repeat1M)
  13. UNIT_TEST(Threads8_Repeat1M_Push1Pop1)
  14. UNIT_TEST_SUITE_END();
  15. public:
  16. void OnePushOnePop() {
  17. TQueueType queue;
  18. auto popped = queue.Pop();
  19. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  20. TLink msg;
  21. queue.Push(&msg);
  22. popped = queue.Pop();
  23. UNIT_ASSERT_VALUES_EQUAL(&msg, popped);
  24. popped = queue.Pop();
  25. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  26. }
  27. void OnePushOnePop_Repeat1M() {
  28. TQueueType queue;
  29. TLink msg;
  30. auto popped = queue.Pop();
  31. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  32. for (int i = 0; i < 1000000; ++i) {
  33. queue.Push(&msg);
  34. popped = queue.Pop();
  35. UNIT_ASSERT_VALUES_EQUAL(&msg, popped);
  36. popped = queue.Pop();
  37. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  38. }
  39. }
  40. template <size_t NUMBER_OF_THREADS>
  41. void RepeatPush1Pop1_InManyThreads() {
  42. class TCycleThread: public ISimpleThread {
  43. public:
  44. void* ThreadProc() override {
  45. TQueueType queue;
  46. TLink msg;
  47. auto popped = queue.Pop();
  48. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  49. for (size_t i = 0; i < 1000000; ++i) {
  50. queue.Push(&msg);
  51. popped = queue.Pop();
  52. UNIT_ASSERT_VALUES_EQUAL(popped, &msg);
  53. popped = queue.Pop();
  54. UNIT_ASSERT_VALUES_EQUAL(popped, nullptr);
  55. }
  56. return nullptr;
  57. }
  58. };
  59. TVector<TAutoPtr<TCycleThread>> cyclers;
  60. for (size_t i = 0; i < NUMBER_OF_THREADS; ++i) {
  61. cyclers.emplace_back(new TCycleThread);
  62. cyclers.back()->Start();
  63. }
  64. for (size_t i = 0; i < NUMBER_OF_THREADS; ++i) {
  65. cyclers[i]->Join();
  66. }
  67. }
  68. void Threads8_Repeat1M_Push1Pop1() {
  69. RepeatPush1Pop1_InManyThreads<8>();
  70. }
  71. };
  72. REGISTER_TESTS_FOR_ALL_ORDERED_QUEUES(TQueueTestsInSingleThread);
  73. REGISTER_TESTS_FOR_ALL_UNORDERED_QUEUES(TQueueTestsInSingleThread)