thread_restrictions_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/threading/thread_restrictions.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/dcheck_is_on.h"
  10. #include "base/debug/stack_trace.h"
  11. #include "base/test/gtest_util.h"
  12. #include "build/build_config.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace base {
  15. namespace {
  16. class ThreadRestrictionsTest : public testing::Test {
  17. public:
  18. ThreadRestrictionsTest() = default;
  19. ThreadRestrictionsTest(const ThreadRestrictionsTest&) = delete;
  20. ThreadRestrictionsTest& operator=(const ThreadRestrictionsTest&) = delete;
  21. ~ThreadRestrictionsTest() override {
  22. internal::ResetThreadRestrictionsForTesting();
  23. }
  24. };
  25. } // namespace
  26. TEST_F(ThreadRestrictionsTest, BlockingAllowedByDefault) {
  27. internal::AssertBlockingAllowed();
  28. }
  29. TEST_F(ThreadRestrictionsTest, ScopedDisallowBlocking) {
  30. {
  31. ScopedDisallowBlocking scoped_disallow_blocking;
  32. EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
  33. }
  34. internal::AssertBlockingAllowed();
  35. }
  36. TEST_F(ThreadRestrictionsTest, ScopedAllowBlocking) {
  37. ScopedDisallowBlocking scoped_disallow_blocking;
  38. {
  39. ScopedAllowBlocking scoped_allow_blocking;
  40. internal::AssertBlockingAllowed();
  41. }
  42. EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
  43. }
  44. TEST_F(ThreadRestrictionsTest, ScopedAllowBlockingForTesting) {
  45. ScopedDisallowBlocking scoped_disallow_blocking;
  46. {
  47. ScopedAllowBlockingForTesting scoped_allow_blocking_for_testing;
  48. internal::AssertBlockingAllowed();
  49. }
  50. EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
  51. }
  52. TEST_F(ThreadRestrictionsTest, BaseSyncPrimitivesAllowedByDefault) {
  53. internal::AssertBaseSyncPrimitivesAllowed();
  54. }
  55. TEST_F(ThreadRestrictionsTest, DisallowBaseSyncPrimitives) {
  56. DisallowBaseSyncPrimitives();
  57. EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  58. }
  59. TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitives) {
  60. DisallowBaseSyncPrimitives();
  61. ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives;
  62. internal::AssertBaseSyncPrimitivesAllowed();
  63. }
  64. TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesResetsState) {
  65. DisallowBaseSyncPrimitives();
  66. { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; }
  67. EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  68. }
  69. TEST_F(ThreadRestrictionsTest,
  70. ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed) {
  71. ScopedDisallowBlocking scoped_disallow_blocking;
  72. DisallowBaseSyncPrimitives();
  73. // This should DCHECK because blocking is not allowed in this scope
  74. // and OutsideBlockingScope is not passed to the constructor.
  75. EXPECT_DCHECK_DEATH(
  76. { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; });
  77. }
  78. TEST_F(ThreadRestrictionsTest,
  79. ScopedAllowBaseSyncPrimitivesOutsideBlockingScope) {
  80. ScopedDisallowBlocking scoped_disallow_blocking;
  81. DisallowBaseSyncPrimitives();
  82. ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
  83. scoped_allow_base_sync_primitives;
  84. internal::AssertBaseSyncPrimitivesAllowed();
  85. }
  86. TEST_F(ThreadRestrictionsTest,
  87. ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState) {
  88. DisallowBaseSyncPrimitives();
  89. {
  90. ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
  91. scoped_allow_base_sync_primitives;
  92. }
  93. EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  94. }
  95. TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesForTesting) {
  96. DisallowBaseSyncPrimitives();
  97. ScopedAllowBaseSyncPrimitivesForTesting
  98. scoped_allow_base_sync_primitives_for_testing;
  99. internal::AssertBaseSyncPrimitivesAllowed();
  100. }
  101. TEST_F(ThreadRestrictionsTest,
  102. ScopedAllowBaseSyncPrimitivesForTestingResetsState) {
  103. DisallowBaseSyncPrimitives();
  104. {
  105. ScopedAllowBaseSyncPrimitivesForTesting
  106. scoped_allow_base_sync_primitives_for_testing;
  107. }
  108. EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  109. }
  110. TEST_F(ThreadRestrictionsTest,
  111. ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed) {
  112. ScopedDisallowBlocking scoped_disallow_blocking;
  113. DisallowBaseSyncPrimitives();
  114. // This should not DCHECK.
  115. ScopedAllowBaseSyncPrimitivesForTesting
  116. scoped_allow_base_sync_primitives_for_testing;
  117. }
  118. TEST_F(ThreadRestrictionsTest, ScopedDisallowBaseSyncPrimitives) {
  119. {
  120. ScopedDisallowBaseSyncPrimitives disallow_sync_primitives;
  121. EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  122. }
  123. internal::AssertBaseSyncPrimitivesAllowed();
  124. }
  125. TEST_F(ThreadRestrictionsTest, SingletonAllowedByDefault) {
  126. internal::AssertSingletonAllowed();
  127. }
  128. TEST_F(ThreadRestrictionsTest, DisallowSingleton) {
  129. DisallowSingleton();
  130. EXPECT_DCHECK_DEATH({ internal::AssertSingletonAllowed(); });
  131. }
  132. TEST_F(ThreadRestrictionsTest, ScopedDisallowSingleton) {
  133. {
  134. ScopedDisallowSingleton disallow_sync_primitives;
  135. EXPECT_DCHECK_DEATH({ internal::AssertSingletonAllowed(); });
  136. }
  137. internal::AssertSingletonAllowed();
  138. }
  139. TEST_F(ThreadRestrictionsTest, LongCPUWorkAllowedByDefault) {
  140. AssertLongCPUWorkAllowed();
  141. }
  142. TEST_F(ThreadRestrictionsTest, DisallowUnresponsiveTasks) {
  143. DisallowUnresponsiveTasks();
  144. EXPECT_DCHECK_DEATH(internal::AssertBlockingAllowed());
  145. EXPECT_DCHECK_DEATH(internal::AssertBaseSyncPrimitivesAllowed());
  146. EXPECT_DCHECK_DEATH(AssertLongCPUWorkAllowed());
  147. }
  148. // thread_restriction_checks_and_has_death_tests
  149. #if !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_ANDROID) && DCHECK_IS_ON() && \
  150. defined(GTEST_HAS_DEATH_TEST)
  151. TEST_F(ThreadRestrictionsTest, BlockingCheckEmitsStack) {
  152. ScopedDisallowBlocking scoped_disallow_blocking;
  153. // The above ScopedDisallowBlocking should be on the blame list for who set
  154. // the ban.
  155. EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
  156. EXPENSIVE_DCHECKS_ARE_ON() &&
  157. debug::StackTrace::WillSymbolizeToStreamForTesting()
  158. ? "ScopedDisallowBlocking"
  159. : "");
  160. // And the stack should mention this test body as source.
  161. EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
  162. EXPENSIVE_DCHECKS_ARE_ON() &&
  163. debug::StackTrace::WillSymbolizeToStreamForTesting()
  164. ? "BlockingCheckEmitsStack"
  165. : "");
  166. }
  167. class TestCustomDisallow {
  168. public:
  169. NOINLINE TestCustomDisallow() { DisallowBlocking(); }
  170. NOINLINE ~TestCustomDisallow() { PermanentThreadAllowance::AllowBlocking(); }
  171. };
  172. TEST_F(ThreadRestrictionsTest, NestedAllowRestoresPreviousStack) {
  173. TestCustomDisallow custom_disallow;
  174. {
  175. ScopedAllowBlocking scoped_allow;
  176. internal::AssertBlockingAllowed();
  177. }
  178. // TestCustomDisallow should be back on the blame list (as opposed to
  179. // ~ScopedAllowBlocking which is the last one to have changed the state but is
  180. // no longer relevant).
  181. EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
  182. EXPENSIVE_DCHECKS_ARE_ON() &&
  183. debug::StackTrace::WillSymbolizeToStreamForTesting()
  184. ? "TestCustomDisallow"
  185. : "");
  186. // And the stack should mention this test body as source.
  187. EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
  188. EXPENSIVE_DCHECKS_ARE_ON() &&
  189. debug::StackTrace::WillSymbolizeToStreamForTesting()
  190. ? "NestedAllowRestoresPreviousStack"
  191. : "");
  192. }
  193. #endif // thread_restriction_checks_and_has_death_tests
  194. } // namespace base