thread_ut.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "thread.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <atomic>
  4. Y_UNIT_TEST_SUITE(TSysThreadTest) {
  5. struct TIdTester {
  6. inline TIdTester()
  7. : Thr(nullptr)
  8. , Cur(0)
  9. , Real(0)
  10. {
  11. }
  12. static inline void* DoRun(void* ptr) {
  13. ((TIdTester*)ptr)->Run();
  14. return nullptr;
  15. }
  16. inline void Run() {
  17. Cur = TThread::CurrentThreadId();
  18. Real = Thr->Id();
  19. Numeric = TThread::CurrentThreadNumericId();
  20. }
  21. TThread* Thr;
  22. TThread::TId Cur;
  23. TThread::TId Real;
  24. TThread::TId Numeric;
  25. };
  26. Y_UNIT_TEST(TestThreadId) {
  27. TIdTester tst;
  28. TThread thr(tst.DoRun, &tst);
  29. tst.Thr = &thr;
  30. thr.Start();
  31. thr.Join();
  32. UNIT_ASSERT_EQUAL(tst.Cur, tst.Real);
  33. UNIT_ASSERT(tst.Cur != 0);
  34. UNIT_ASSERT(tst.Numeric != 0);
  35. UNIT_ASSERT(tst.Numeric != tst.Real);
  36. }
  37. void* ThreadProc(void*) {
  38. TThread::SetCurrentThreadName("CurrentThreadSetNameTest");
  39. return nullptr;
  40. }
  41. void* ThreadProc2(void*) {
  42. return nullptr;
  43. }
  44. void* ThreadProc3(void*) {
  45. const auto name = TThread::CurrentThreadName();
  46. Y_FAKE_READ(name);
  47. return nullptr;
  48. }
  49. void* ThreadProc4(void*) {
  50. const TString setName = "ThreadName";
  51. TThread::SetCurrentThreadName(setName.data());
  52. const auto getName = TThread::CurrentThreadName();
  53. if (TThread::CanGetCurrentThreadName()) {
  54. UNIT_ASSERT_VALUES_EQUAL(setName, getName);
  55. } else {
  56. UNIT_ASSERT_VALUES_EQUAL("", getName);
  57. }
  58. return nullptr;
  59. }
  60. void* ThreadProcChild(void*) {
  61. const auto name = TThread::CurrentThreadName();
  62. const auto defaultName = GetProgramName();
  63. (void)name;
  64. (void)defaultName;
  65. #if defined(_darwin_) || defined(_linux_)
  66. UNIT_ASSERT_VALUES_EQUAL(name, defaultName);
  67. #endif
  68. return nullptr;
  69. }
  70. void* ThreadProcParent(void*) {
  71. const TString setName = "Parent";
  72. TThread::SetCurrentThreadName(setName.data());
  73. TThread thread(&ThreadProcChild, nullptr);
  74. thread.Start();
  75. thread.Join();
  76. const auto getName = TThread::CurrentThreadName();
  77. if (TThread::CanGetCurrentThreadName()) {
  78. UNIT_ASSERT_VALUES_EQUAL(setName, getName);
  79. } else {
  80. UNIT_ASSERT_VALUES_EQUAL("", getName);
  81. }
  82. return nullptr;
  83. }
  84. Y_UNIT_TEST(TestSetThreadName) {
  85. TThread thread(&ThreadProc, nullptr);
  86. // just check it doesn't crash
  87. thread.Start();
  88. thread.Join();
  89. }
  90. Y_UNIT_TEST(TestSetThreadName2) {
  91. TThread thread(TThread::TParams(&ThreadProc, nullptr, 0).SetName("XXX"));
  92. thread.Start();
  93. thread.Join();
  94. }
  95. Y_UNIT_TEST(TestGetThreadName) {
  96. TThread thread(&ThreadProc3, nullptr);
  97. thread.Start();
  98. thread.Join();
  99. }
  100. Y_UNIT_TEST(TestSetGetThreadName) {
  101. TThread thread(&ThreadProc4, nullptr);
  102. thread.Start();
  103. thread.Join();
  104. }
  105. Y_UNIT_TEST(TestSetGetThreadNameInChildThread) {
  106. TThread thread(&ThreadProcParent, nullptr);
  107. thread.Start();
  108. thread.Join();
  109. }
  110. Y_UNIT_TEST(TestDoubleJoin) {
  111. TThread thread(&ThreadProc, nullptr);
  112. thread.Start();
  113. thread.Join();
  114. UNIT_ASSERT_EQUAL(thread.Join(), nullptr);
  115. }
  116. Y_UNIT_TEST(TestDoubleStart) {
  117. TThread thread(&ThreadProc, nullptr);
  118. thread.Start();
  119. UNIT_ASSERT_EXCEPTION(thread.Start(), yexception);
  120. thread.Join();
  121. }
  122. Y_UNIT_TEST(TestNoStart) {
  123. TThread thread(&ThreadProc, nullptr);
  124. }
  125. Y_UNIT_TEST(TestNoStartJoin) {
  126. TThread thread(&ThreadProc, nullptr);
  127. UNIT_ASSERT_EQUAL(thread.Join(), nullptr);
  128. }
  129. Y_UNIT_TEST(TestStackPointer) {
  130. TArrayHolder<char> buf(new char[64000]);
  131. TThread thr(TThread::TParams(ThreadProc2, nullptr).SetStackPointer(buf.Get()).SetStackSize(64000));
  132. thr.Start();
  133. UNIT_ASSERT_VALUES_EQUAL(thr.Join(), nullptr);
  134. }
  135. Y_UNIT_TEST(TestStackLimits) {
  136. TCurrentThreadLimits sl;
  137. UNIT_ASSERT(sl.StackBegin);
  138. UNIT_ASSERT(sl.StackLength > 0);
  139. }
  140. Y_UNIT_TEST(TestFunc) {
  141. std::atomic_bool flag = {false};
  142. TThread thread([&flag]() { flag = true; });
  143. thread.Start();
  144. UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);
  145. UNIT_ASSERT(flag);
  146. }
  147. Y_UNIT_TEST(TestCopyFunc) {
  148. std::atomic_bool flag = {false};
  149. auto func = [&flag]() { flag = true; };
  150. TThread thread(func);
  151. thread.Start();
  152. UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);
  153. TThread thread2(func);
  154. thread2.Start();
  155. UNIT_ASSERT_VALUES_EQUAL(thread2.Join(), nullptr);
  156. UNIT_ASSERT(flag);
  157. }
  158. Y_UNIT_TEST(TestCallable) {
  159. std::atomic_bool flag = {false};
  160. struct TCallable: TMoveOnly {
  161. std::atomic_bool* Flag_;
  162. TCallable(std::atomic_bool* flag)
  163. : Flag_(flag)
  164. {
  165. }
  166. void operator()() {
  167. *Flag_ = true;
  168. }
  169. };
  170. TCallable foo(&flag);
  171. TThread thread(std::move(foo));
  172. thread.Start();
  173. UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);
  174. UNIT_ASSERT(flag);
  175. }
  176. } // Y_UNIT_TEST_SUITE(TSysThreadTest)