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
5 #include "base/threading/thread_restrictions.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/test/gtest_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16
17 namespace {
18
19 class ThreadRestrictionsTest : public testing::Test {
20 public:
21 ThreadRestrictionsTest() = default;
~ThreadRestrictionsTest()22 ~ThreadRestrictionsTest() override {
23 internal::ResetThreadRestrictionsForTesting();
24 }
25
26 private:
27 DISALLOW_COPY_AND_ASSIGN(ThreadRestrictionsTest);
28 };
29
30 } // namespace
31
TEST_F(ThreadRestrictionsTest,BlockingAllowedByDefault)32 TEST_F(ThreadRestrictionsTest, BlockingAllowedByDefault) {
33 AssertBlockingAllowed();
34 }
35
TEST_F(ThreadRestrictionsTest,ScopedDisallowBlocking)36 TEST_F(ThreadRestrictionsTest, ScopedDisallowBlocking) {
37 {
38 ScopedDisallowBlocking scoped_disallow_blocking;
39 EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
40 }
41 AssertBlockingAllowed();
42 }
43
TEST_F(ThreadRestrictionsTest,ScopedAllowBlocking)44 TEST_F(ThreadRestrictionsTest, ScopedAllowBlocking) {
45 ScopedDisallowBlocking scoped_disallow_blocking;
46 {
47 ScopedAllowBlocking scoped_allow_blocking;
48 AssertBlockingAllowed();
49 }
50 EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
51 }
52
TEST_F(ThreadRestrictionsTest,ScopedAllowBlockingForTesting)53 TEST_F(ThreadRestrictionsTest, ScopedAllowBlockingForTesting) {
54 ScopedDisallowBlocking scoped_disallow_blocking;
55 {
56 ScopedAllowBlockingForTesting scoped_allow_blocking_for_testing;
57 AssertBlockingAllowed();
58 }
59 EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
60 }
61
TEST_F(ThreadRestrictionsTest,BaseSyncPrimitivesAllowedByDefault)62 TEST_F(ThreadRestrictionsTest, BaseSyncPrimitivesAllowedByDefault) {}
63
TEST_F(ThreadRestrictionsTest,DisallowBaseSyncPrimitives)64 TEST_F(ThreadRestrictionsTest, DisallowBaseSyncPrimitives) {
65 DisallowBaseSyncPrimitives();
66 EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
67 }
68
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitives)69 TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitives) {
70 DisallowBaseSyncPrimitives();
71 ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives;
72 internal::AssertBaseSyncPrimitivesAllowed();
73 }
74
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesResetsState)75 TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesResetsState) {
76 DisallowBaseSyncPrimitives();
77 { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; }
78 EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
79 }
80
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed)81 TEST_F(ThreadRestrictionsTest,
82 ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed) {
83 ScopedDisallowBlocking scoped_disallow_blocking;
84 DisallowBaseSyncPrimitives();
85
86 // This should DCHECK because blocking is not allowed in this scope
87 // and OutsideBlockingScope is not passed to the constructor.
88 EXPECT_DCHECK_DEATH(
89 { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; });
90 }
91
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesOutsideBlockingScope)92 TEST_F(ThreadRestrictionsTest,
93 ScopedAllowBaseSyncPrimitivesOutsideBlockingScope) {
94 ScopedDisallowBlocking scoped_disallow_blocking;
95 DisallowBaseSyncPrimitives();
96 ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
97 scoped_allow_base_sync_primitives;
98 internal::AssertBaseSyncPrimitivesAllowed();
99 }
100
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState)101 TEST_F(ThreadRestrictionsTest,
102 ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState) {
103 DisallowBaseSyncPrimitives();
104 {
105 ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
106 scoped_allow_base_sync_primitives;
107 }
108 EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
109 }
110
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTesting)111 TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesForTesting) {
112 DisallowBaseSyncPrimitives();
113 ScopedAllowBaseSyncPrimitivesForTesting
114 scoped_allow_base_sync_primitives_for_testing;
115 internal::AssertBaseSyncPrimitivesAllowed();
116 }
117
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTestingResetsState)118 TEST_F(ThreadRestrictionsTest,
119 ScopedAllowBaseSyncPrimitivesForTestingResetsState) {
120 DisallowBaseSyncPrimitives();
121 {
122 ScopedAllowBaseSyncPrimitivesForTesting
123 scoped_allow_base_sync_primitives_for_testing;
124 }
125 EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
126 }
127
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed)128 TEST_F(ThreadRestrictionsTest,
129 ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed) {
130 ScopedDisallowBlocking scoped_disallow_blocking;
131 DisallowBaseSyncPrimitives();
132 // This should not DCHECK.
133 ScopedAllowBaseSyncPrimitivesForTesting
134 scoped_allow_base_sync_primitives_for_testing;
135 }
136
137 } // namespace base
138