summaryrefslogtreecommitdiff
path: root/base/threading/thread_restrictions_unittest.cc
blob: 4c67abbfed1f348df02845eed180a52ed3848f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/threading/thread_restrictions.h"

#include <utility>

#include "base/compiler_specific.h"
#include "base/dcheck_is_on.h"
#include "base/debug/stack_trace.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

class ThreadRestrictionsTest : public testing::Test {
 public:
  ThreadRestrictionsTest() = default;

  ThreadRestrictionsTest(const ThreadRestrictionsTest&) = delete;
  ThreadRestrictionsTest& operator=(const ThreadRestrictionsTest&) = delete;

  ~ThreadRestrictionsTest() override {
    internal::ResetThreadRestrictionsForTesting();
  }
};

}  // namespace

TEST_F(ThreadRestrictionsTest, BlockingAllowedByDefault) {
  internal::AssertBlockingAllowed();
}

TEST_F(ThreadRestrictionsTest, ScopedDisallowBlocking) {
  {
    ScopedDisallowBlocking scoped_disallow_blocking;
    EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
  }
  internal::AssertBlockingAllowed();
}

TEST_F(ThreadRestrictionsTest, ScopedAllowBlocking) {
  ScopedDisallowBlocking scoped_disallow_blocking;
  {
    ScopedAllowBlocking scoped_allow_blocking;
    internal::AssertBlockingAllowed();
  }
  EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
}

TEST_F(ThreadRestrictionsTest, ScopedAllowBlockingForTesting) {
  ScopedDisallowBlocking scoped_disallow_blocking;
  {
    ScopedAllowBlockingForTesting scoped_allow_blocking_for_testing;
    internal::AssertBlockingAllowed();
  }
  EXPECT_DCHECK_DEATH({ internal::AssertBlockingAllowed(); });
}

TEST_F(ThreadRestrictionsTest, BaseSyncPrimitivesAllowedByDefault) {
  internal::AssertBaseSyncPrimitivesAllowed();
}

TEST_F(ThreadRestrictionsTest, DisallowBaseSyncPrimitives) {
  DisallowBaseSyncPrimitives();
  EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
}

TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitives) {
  DisallowBaseSyncPrimitives();
  ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives;
  internal::AssertBaseSyncPrimitivesAllowed();
}

TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesResetsState) {
  DisallowBaseSyncPrimitives();
  { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; }
  EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
}

TEST_F(ThreadRestrictionsTest,
       ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed) {
  ScopedDisallowBlocking scoped_disallow_blocking;
  DisallowBaseSyncPrimitives();

  // This should DCHECK because blocking is not allowed in this scope
  // and OutsideBlockingScope is not passed to the constructor.
  EXPECT_DCHECK_DEATH(
      { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; });
}

TEST_F(ThreadRestrictionsTest,
       ScopedAllowBaseSyncPrimitivesOutsideBlockingScope) {
  ScopedDisallowBlocking scoped_disallow_blocking;
  DisallowBaseSyncPrimitives();
  ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
      scoped_allow_base_sync_primitives;
  internal::AssertBaseSyncPrimitivesAllowed();
}

TEST_F(ThreadRestrictionsTest,
       ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState) {
  DisallowBaseSyncPrimitives();
  {
    ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
        scoped_allow_base_sync_primitives;
  }
  EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
}

TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesForTesting) {
  DisallowBaseSyncPrimitives();
  ScopedAllowBaseSyncPrimitivesForTesting
      scoped_allow_base_sync_primitives_for_testing;
  internal::AssertBaseSyncPrimitivesAllowed();
}

TEST_F(ThreadRestrictionsTest,
       ScopedAllowBaseSyncPrimitivesForTestingResetsState) {
  DisallowBaseSyncPrimitives();
  {
    ScopedAllowBaseSyncPrimitivesForTesting
        scoped_allow_base_sync_primitives_for_testing;
  }
  EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
}

TEST_F(ThreadRestrictionsTest,
       ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed) {
  ScopedDisallowBlocking scoped_disallow_blocking;
  DisallowBaseSyncPrimitives();
  // This should not DCHECK.
  ScopedAllowBaseSyncPrimitivesForTesting
      scoped_allow_base_sync_primitives_for_testing;
}

TEST_F(ThreadRestrictionsTest, ScopedDisallowBaseSyncPrimitives) {
  {
    ScopedDisallowBaseSyncPrimitives disallow_sync_primitives;
    EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
  }
  internal::AssertBaseSyncPrimitivesAllowed();
}

TEST_F(ThreadRestrictionsTest, SingletonAllowedByDefault) {
  internal::AssertSingletonAllowed();
}

TEST_F(ThreadRestrictionsTest, DisallowSingleton) {
  DisallowSingleton();
  EXPECT_DCHECK_DEATH({ internal::AssertSingletonAllowed(); });
}

TEST_F(ThreadRestrictionsTest, ScopedDisallowSingleton) {
  {
    ScopedDisallowSingleton disallow_sync_primitives;
    EXPECT_DCHECK_DEATH({ internal::AssertSingletonAllowed(); });
  }
  internal::AssertSingletonAllowed();
}

TEST_F(ThreadRestrictionsTest, LongCPUWorkAllowedByDefault) {
  AssertLongCPUWorkAllowed();
}

TEST_F(ThreadRestrictionsTest, DisallowUnresponsiveTasks) {
  DisallowUnresponsiveTasks();
  EXPECT_DCHECK_DEATH(internal::AssertBlockingAllowed());
  EXPECT_DCHECK_DEATH(internal::AssertBaseSyncPrimitivesAllowed());
  EXPECT_DCHECK_DEATH(AssertLongCPUWorkAllowed());
}

// thread_restriction_checks_and_has_death_tests
#if !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_ANDROID) && DCHECK_IS_ON() && \
    defined(GTEST_HAS_DEATH_TEST)

TEST_F(ThreadRestrictionsTest, BlockingCheckEmitsStack) {
  debug::OverrideStackTraceOutputForTesting enable_stacks_in_death_tests(
      debug::OverrideStackTraceOutputForTesting::Mode::kForceOutput);
  ScopedDisallowBlocking scoped_disallow_blocking;
  // The above ScopedDisallowBlocking should be on the blame list for who set
  // the ban.
  EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
               EXPENSIVE_DCHECKS_ARE_ON() &&
                       debug::StackTrace::WillSymbolizeToStreamForTesting()
                   ? "ScopedDisallowBlocking"
                   : "");
  // And the stack should mention this test body as source.
  EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
               EXPENSIVE_DCHECKS_ARE_ON() &&
                       debug::StackTrace::WillSymbolizeToStreamForTesting()
                   ? "BlockingCheckEmitsStack"
                   : "");
}

class TestCustomDisallow {
 public:
  NOINLINE TestCustomDisallow() { DisallowBlocking(); }
  NOINLINE ~TestCustomDisallow() { PermanentThreadAllowance::AllowBlocking(); }
};

TEST_F(ThreadRestrictionsTest, NestedAllowRestoresPreviousStack) {
  debug::OverrideStackTraceOutputForTesting enable_stacks_in_death_tests(
      debug::OverrideStackTraceOutputForTesting::Mode::kForceOutput);
  TestCustomDisallow custom_disallow;
  {
    ScopedAllowBlocking scoped_allow;
    internal::AssertBlockingAllowed();
  }
  // TestCustomDisallow should be back on the blame list (as opposed to
  // ~ScopedAllowBlocking which is the last one to have changed the state but is
  // no longer relevant).
  EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
               EXPENSIVE_DCHECKS_ARE_ON() &&
                       debug::StackTrace::WillSymbolizeToStreamForTesting()
                   ? "TestCustomDisallow"
                   : "");
  // And the stack should mention this test body as source.
  EXPECT_DEATH({ internal::AssertBlockingAllowed(); },
               EXPENSIVE_DCHECKS_ARE_ON() &&
                       debug::StackTrace::WillSymbolizeToStreamForTesting()
                   ? "NestedAllowRestoresPreviousStack"
                   : "");
}

#endif  // thread_restriction_checks_and_has_death_tests

}  // namespace base