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