OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Multi-threaded tests of ConditionVariable class. | 5 // Multi-threaded tests of ConditionVariable class. |
6 | 6 |
7 #include "base/synchronization/condition_variable.h" | 7 #include "base/synchronization/condition_variable.h" |
8 | 8 |
9 #include <time.h> | 9 #include <time.h> |
10 | 10 |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 187 |
188 cv.TimedWait(WAIT_TIME + FUDGE_TIME); | 188 cv.TimedWait(WAIT_TIME + FUDGE_TIME); |
189 TimeDelta duration = TimeTicks::Now() - start; | 189 TimeDelta duration = TimeTicks::Now() - start; |
190 // We can't use EXPECT_GE here as the TimeDelta class does not support the | 190 // We can't use EXPECT_GE here as the TimeDelta class does not support the |
191 // required stream conversion. | 191 // required stream conversion. |
192 EXPECT_TRUE(duration >= WAIT_TIME); | 192 EXPECT_TRUE(duration >= WAIT_TIME); |
193 | 193 |
194 lock.Release(); | 194 lock.Release(); |
195 } | 195 } |
196 | 196 |
| 197 void SignalConditionVariable(ConditionVariable* cv) { |
| 198 cv->Signal(); |
| 199 } |
| 200 |
| 201 TEST_F(ConditionVariableTest, TimedWaitTimeoutTest) { |
| 202 Lock lock; |
| 203 ConditionVariable cv(&lock); |
| 204 lock.Acquire(); |
| 205 |
| 206 bool timed_out = cv.TimedWait(TimeDelta::FromMilliseconds(50)); |
| 207 EXPECT_TRUE(timed_out); |
| 208 |
| 209 Thread thread("Helper"); |
| 210 thread.Start(); |
| 211 thread.task_runner()->PostTask(FROM_HERE, |
| 212 base::Bind(&SignalConditionVariable, &cv)); |
| 213 timed_out = cv.TimedWait(TimeDelta::FromMilliseconds(300)); |
| 214 EXPECT_FALSE(timed_out); |
| 215 |
| 216 lock.Release(); |
| 217 } |
| 218 |
197 #if defined(OS_POSIX) | 219 #if defined(OS_POSIX) |
198 const int kDiscontinuitySeconds = 2; | 220 const int kDiscontinuitySeconds = 2; |
199 | 221 |
200 void BackInTime(Lock* lock) { | 222 void BackInTime(Lock* lock) { |
201 AutoLock auto_lock(*lock); | 223 AutoLock auto_lock(*lock); |
202 | 224 |
203 timeval tv; | 225 timeval tv; |
204 gettimeofday(&tv, NULL); | 226 gettimeofday(&tv, NULL); |
205 tv.tv_sec -= kDiscontinuitySeconds; | 227 tv.tv_sec -= kDiscontinuitySeconds; |
206 settimeofday(&tv, NULL); | 228 settimeofday(&tv, NULL); |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 base::AutoLock auto_lock(lock_); | 781 base::AutoLock auto_lock(lock_); |
760 // Send notification that we completed our "work." | 782 // Send notification that we completed our "work." |
761 WorkIsCompleted(thread_id); | 783 WorkIsCompleted(thread_id); |
762 } | 784 } |
763 } | 785 } |
764 } | 786 } |
765 | 787 |
766 } // namespace | 788 } // namespace |
767 | 789 |
768 } // namespace base | 790 } // namespace base |
OLD | NEW |