Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(829)

Side by Side Diff: sandbox/linux/services/thread_helpers_unittests.cc

Issue 935333002: Update from https://crrev.com/316786 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sandbox/linux/services/thread_helpers.h" 5 #include "sandbox/linux/services/thread_helpers.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
11 #include <unistd.h> 11 #include <unistd.h>
12 12
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
17 #include "base/process/process_metrics.h" 17 #include "base/process/process_metrics.h"
18 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "sandbox/linux/tests/unit_tests.h" 21 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
23 23
24 using base::PlatformThread; 24 using base::PlatformThread;
25 25
26 namespace sandbox { 26 namespace sandbox {
27 27
28 namespace { 28 namespace {
29 29
30 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
31 #if !defined(THREAD_SANITIZER)
32
30 int GetRaceTestIterations() { 33 int GetRaceTestIterations() {
31 if (IsRunningOnValgrind()) { 34 if (IsRunningOnValgrind()) {
32 return 2; 35 return 2;
33 } else { 36 } else {
34 return 1000; 37 return 1000;
35 } 38 }
36 } 39 }
37 40
38 class ScopedProcSelfTask { 41 class ScopedProcSelfTask {
39 public: 42 public:
40 ScopedProcSelfTask() : fd_(-1) { 43 ScopedProcSelfTask() : fd_(-1) {
41 fd_ = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); 44 fd_ = open("/proc/self/task/", O_RDONLY | O_DIRECTORY);
42 CHECK_LE(0, fd_); 45 CHECK_LE(0, fd_);
43 } 46 }
44 47
45 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); } 48 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); }
46 49
47 int fd() { return fd_; } 50 int fd() { return fd_; }
48 51
49 private: 52 private:
50 int fd_; 53 int fd_;
51 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask); 54 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask);
52 }; 55 };
53 56
54 #if defined(THREAD_SANITIZER) 57 TEST(ThreadHelpers, IsSingleThreadedBasic) {
55 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
56 #define MAYBE_IsSingleThreadedBasic DISABLED_IsSingleThreadedBasic
57 #define MAYBE_IsSingleThreadedIterated DISABLED_IsSingleThreadedIterated
58 #define MAYBE_IsSingleThreadedStartAndStop DISABLED_IsSingleThreadedStartAndStop
59 #else
60 #define MAYBE_IsSingleThreadedBasic IsSingleThreadedBasic
61 #define MAYBE_IsSingleThreadedIterated IsSingleThreadedIterated
62 #define MAYBE_IsSingleThreadedStartAndStop IsSingleThreadedStartAndStop
63 #endif
64
65 TEST(ThreadHelpers, MAYBE_IsSingleThreadedBasic) {
66 ScopedProcSelfTask task; 58 ScopedProcSelfTask task;
67 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
68 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(-1)); 60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded());
69 61
70 base::Thread thread("sandbox_tests"); 62 base::Thread thread("sandbox_tests");
71 ASSERT_TRUE(thread.Start()); 63 ASSERT_TRUE(thread.Start());
72 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd()));
73 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(-1)); 65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded());
74 // Explicitly stop the thread here to not pollute the next test. 66 // Explicitly stop the thread here to not pollute the next test.
75 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); 67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
76 } 68 }
77 69
78 TEST(ThreadHelpers, MAYBE_IsSingleThreadedIterated) { 70 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) {
71 ScopedProcSelfTask task;
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(task.fd()));
73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
74
75 ThreadHelpers::AssertSingleThreaded(task.fd());
76 ThreadHelpers::AssertSingleThreaded();
77 }
78
79 TEST(ThreadHelpers, IsSingleThreadedIterated) {
79 ScopedProcSelfTask task; 80 ScopedProcSelfTask task;
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
81 82
82 // Iterate to check for race conditions. 83 // Iterate to check for race conditions.
83 for (int i = 0; i < GetRaceTestIterations(); ++i) { 84 for (int i = 0; i < GetRaceTestIterations(); ++i) {
84 base::Thread thread("sandbox_tests"); 85 base::Thread thread("sandbox_tests");
85 ASSERT_TRUE(thread.Start()); 86 ASSERT_TRUE(thread.Start());
86 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd()));
87 // Explicitly stop the thread here to not pollute the next test. 88 // Explicitly stop the thread here to not pollute the next test.
88 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); 89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
89 } 90 }
90 } 91 }
91 92
92 TEST(ThreadHelpers, MAYBE_IsSingleThreadedStartAndStop) { 93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) {
93 ScopedProcSelfTask task; 94 ScopedProcSelfTask task;
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
95 96
96 base::Thread thread("sandbox_tests"); 97 base::Thread thread("sandbox_tests");
97 // This is testing for a race condition, so iterate. 98 // This is testing for a race condition, so iterate.
98 // Manually, this has been tested with more that 1M iterations. 99 // Manually, this has been tested with more that 1M iterations.
99 for (int i = 0; i < GetRaceTestIterations(); ++i) { 100 for (int i = 0; i < GetRaceTestIterations(); ++i) {
100 ASSERT_TRUE(thread.Start()); 101 ASSERT_TRUE(thread.Start());
101 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd()));
102 103
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); 104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); 106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
106 } 107 }
107 } 108 }
108 109
110 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) {
111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
112
113 base::Thread thread1("sandbox_tests");
114 base::Thread thread2("sandbox_tests");
115
116 for (int i = 0; i < GetRaceTestIterations(); ++i) {
117 SANDBOX_ASSERT(thread1.Start());
118 SANDBOX_ASSERT(thread2.Start());
119 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
120
121 thread1.Stop();
122 thread2.Stop();
123 // This will wait on /proc/ to reflect the state of threads in the
124 // process.
125 ThreadHelpers::AssertSingleThreaded();
126 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
127 }
128 }
129
130 // Only run this test in Debug mode, where AssertSingleThreaded() will return
131 // in less than 64ms.
132 #if !defined(NDEBUG)
133 SANDBOX_DEATH_TEST(
134 ThreadHelpers,
135 AssertSingleThreadedDies,
136 DEATH_MESSAGE(
137 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
138 base::Thread thread1("sandbox_tests");
139 SANDBOX_ASSERT(thread1.Start());
140 ThreadHelpers::AssertSingleThreaded();
141 }
142 #endif // !defined(NDEBUG)
143
144 #endif // !defined(THREAD_SANITIZER)
145
109 } // namespace 146 } // namespace
110 147
111 } // namespace sandbox 148 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/services/thread_helpers.cc ('k') | sandbox/linux/services/unix_domain_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698