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

Side by Side Diff: base/synchronization/lock_unittest.cc

Issue 9055001: Change code in base (primarily unit tests) to use Sleep(TimeDelta). (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Qualify windows Sleep calls to go through PlatformThread. Created 8 years, 11 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "base/synchronization/lock.h" 7 #include "base/synchronization/lock.h"
8 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 // Basic test to make sure that Acquire()/Release()/Try() don't crash ---------- 13 // Basic test to make sure that Acquire()/Release()/Try() don't crash ----------
14 14
15 class BasicLockTestThread : public PlatformThread::Delegate { 15 class BasicLockTestThread : public PlatformThread::Delegate {
16 public: 16 public:
17 BasicLockTestThread(Lock* lock) : lock_(lock), acquired_(0) {} 17 BasicLockTestThread(Lock* lock) : lock_(lock), acquired_(0) {}
18 18
19 virtual void ThreadMain() { 19 virtual void ThreadMain() {
20 for (int i = 0; i < 10; i++) { 20 for (int i = 0; i < 10; i++) {
21 lock_->Acquire(); 21 lock_->Acquire();
22 acquired_++; 22 acquired_++;
23 lock_->Release(); 23 lock_->Release();
24 } 24 }
25 for (int i = 0; i < 10; i++) { 25 for (int i = 0; i < 10; i++) {
26 lock_->Acquire(); 26 lock_->Acquire();
27 acquired_++; 27 acquired_++;
28 PlatformThread::Sleep(rand() % 20); 28 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
29 lock_->Release(); 29 lock_->Release();
30 } 30 }
31 for (int i = 0; i < 10; i++) { 31 for (int i = 0; i < 10; i++) {
32 if (lock_->Try()) { 32 if (lock_->Try()) {
33 acquired_++; 33 acquired_++;
34 PlatformThread::Sleep(rand() % 20); 34 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
35 lock_->Release(); 35 lock_->Release();
36 } 36 }
37 } 37 }
38 } 38 }
39 39
40 int acquired() const { return acquired_; } 40 int acquired() const { return acquired_; }
41 41
42 private: 42 private:
43 Lock* lock_; 43 Lock* lock_;
44 int acquired_; 44 int acquired_;
(...skipping 10 matching lines...) Expand all
55 55
56 int acquired = 0; 56 int acquired = 0;
57 for (int i = 0; i < 5; i++) { 57 for (int i = 0; i < 5; i++) {
58 lock.Acquire(); 58 lock.Acquire();
59 acquired++; 59 acquired++;
60 lock.Release(); 60 lock.Release();
61 } 61 }
62 for (int i = 0; i < 10; i++) { 62 for (int i = 0; i < 10; i++) {
63 lock.Acquire(); 63 lock.Acquire();
64 acquired++; 64 acquired++;
65 PlatformThread::Sleep(rand() % 20); 65 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
66 lock.Release(); 66 lock.Release();
67 } 67 }
68 for (int i = 0; i < 10; i++) { 68 for (int i = 0; i < 10; i++) {
69 if (lock.Try()) { 69 if (lock.Try()) {
70 acquired++; 70 acquired++;
71 PlatformThread::Sleep(rand() % 20); 71 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
72 lock.Release(); 72 lock.Release();
73 } 73 }
74 } 74 }
75 for (int i = 0; i < 5; i++) { 75 for (int i = 0; i < 5; i++) {
76 lock.Acquire(); 76 lock.Acquire();
77 acquired++; 77 acquired++;
78 PlatformThread::Sleep(rand() % 20); 78 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20));
79 lock.Release(); 79 lock.Release();
80 } 80 }
81 81
82 PlatformThread::Join(handle); 82 PlatformThread::Join(handle);
83 83
84 EXPECT_GE(acquired, 20); 84 EXPECT_GE(acquired, 20);
85 EXPECT_GE(thread.acquired(), 20); 85 EXPECT_GE(thread.acquired(), 20);
86 } 86 }
87 87
88 // Test that Try() works as expected ------------------------------------------- 88 // Test that Try() works as expected -------------------------------------------
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 147
148 class MutexLockTestThread : public PlatformThread::Delegate { 148 class MutexLockTestThread : public PlatformThread::Delegate {
149 public: 149 public:
150 MutexLockTestThread(Lock* lock, int* value) : lock_(lock), value_(value) {} 150 MutexLockTestThread(Lock* lock, int* value) : lock_(lock), value_(value) {}
151 151
152 // Static helper which can also be called from the main thread. 152 // Static helper which can also be called from the main thread.
153 static void DoStuff(Lock* lock, int* value) { 153 static void DoStuff(Lock* lock, int* value) {
154 for (int i = 0; i < 40; i++) { 154 for (int i = 0; i < 40; i++) {
155 lock->Acquire(); 155 lock->Acquire();
156 int v = *value; 156 int v = *value;
157 PlatformThread::Sleep(rand() % 10); 157 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 10));
158 *value = v + 1; 158 *value = v + 1;
159 lock->Release(); 159 lock->Release();
160 } 160 }
161 } 161 }
162 162
163 virtual void ThreadMain() { 163 virtual void ThreadMain() {
164 DoStuff(lock_, value_); 164 DoStuff(lock_, value_);
165 } 165 }
166 166
167 private: 167 private:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 MutexLockTestThread::DoStuff(&lock, &value); 205 MutexLockTestThread::DoStuff(&lock, &value);
206 206
207 PlatformThread::Join(handle1); 207 PlatformThread::Join(handle1);
208 PlatformThread::Join(handle2); 208 PlatformThread::Join(handle2);
209 PlatformThread::Join(handle3); 209 PlatformThread::Join(handle3);
210 210
211 EXPECT_EQ(4 * 40, value); 211 EXPECT_EQ(4 * 40, value);
212 } 212 }
213 213
214 } // namespace base 214 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/condition_variable_unittest.cc ('k') | base/synchronization/waitable_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698