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

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

Issue 24158005: Fix WaitableEvent and ConditionVariable::TimedWait to use monotonic time on non-mac posix (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
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 <time.h> 7 #include <time.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/condition_variable.h" 14 #include "base/synchronization/condition_variable.h"
14 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
15 #include "base/synchronization/spin_wait.h" 16 #include "base/synchronization/spin_wait.h"
16 #include "base/threading/platform_thread.h" 17 #include "base/threading/platform_thread.h"
18 #include "base/threading/thread.h"
17 #include "base/threading/thread_collision_warner.h" 19 #include "base/threading/thread_collision_warner.h"
18 #include "base/time/time.h" 20 #include "base/time/time.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/platform_test.h" 22 #include "testing/platform_test.h"
21 23
22 namespace base { 24 namespace base {
23 25
24 namespace { 26 namespace {
25 //------------------------------------------------------------------------------ 27 //------------------------------------------------------------------------------
26 // Define our test class, with several common variables. 28 // Define our test class, with several common variables.
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 183
182 cv.TimedWait(WAIT_TIME + FUDGE_TIME); 184 cv.TimedWait(WAIT_TIME + FUDGE_TIME);
183 TimeDelta duration = TimeTicks::Now() - start; 185 TimeDelta duration = TimeTicks::Now() - start;
184 // We can't use EXPECT_GE here as the TimeDelta class does not support the 186 // We can't use EXPECT_GE here as the TimeDelta class does not support the
185 // required stream conversion. 187 // required stream conversion.
186 EXPECT_TRUE(duration >= WAIT_TIME); 188 EXPECT_TRUE(duration >= WAIT_TIME);
187 189
188 lock.Release(); 190 lock.Release();
189 } 191 }
190 192
193 #if defined(OS_POSIX)
194 const int kDiscontinuitySeconds = 2;
195
196 void BackInTime(Lock* lock) {
197 AutoLock auto_lock(*lock);
198
199 timeval tv;
200 gettimeofday(&tv, NULL);
201 tv.tv_sec -= kDiscontinuitySeconds;
202 settimeofday(&tv, NULL);
203 }
204
205 // Tests that TimedWait ignores changes to the system clock.
206 // Test is disabled by default, because it needs to run as root to muck with the
207 // system clock.
208 // http://crbug.com/293736
209 TEST_F(ConditionVariableTest, DISABLED_TimeoutAcrossSetTimeOfDay) {
Alexander Potapenko 2013/10/03 09:16:17 A disabled test will rot in no time, because nobod
210 timeval tv;
211 gettimeofday(&tv, NULL);
212 tv.tv_sec += kDiscontinuitySeconds;
213 if (settimeofday(&tv, NULL) < 0) {
214 PLOG(ERROR) << "Could not set time of day. Run as root?";
215 return;
216 }
217
218 Lock lock;
219 ConditionVariable cv(&lock);
220 lock.Acquire();
221
222 Thread thread("Helper");
223 thread.Start();
224 thread.message_loop()->PostTask(FROM_HERE, base::Bind(&BackInTime, &lock));
225
226 TimeTicks start = TimeTicks::Now();
227 const TimeDelta kWaitTime = TimeDelta::FromMilliseconds(300);
228 // Allow for clocking rate granularity.
229 const TimeDelta kFudgeTime = TimeDelta::FromMilliseconds(50);
230
231 cv.TimedWait(kWaitTime + kFudgeTime);
232 TimeDelta duration = TimeTicks::Now() - start;
233
234 thread.Stop();
235 // We can't use EXPECT_GE here as the TimeDelta class does not support the
236 // required stream conversion.
237 EXPECT_TRUE(duration >= kWaitTime);
238 EXPECT_TRUE(duration <= TimeDelta::FromSeconds(kDiscontinuitySeconds));
239
240 lock.Release();
241 }
242 #endif
243
191 244
192 // Suddenly got flaky on Win, see http://crbug.com/10607 (starting at 245 // Suddenly got flaky on Win, see http://crbug.com/10607 (starting at
193 // comment #15) 246 // comment #15)
194 #if defined(OS_WIN) 247 #if defined(OS_WIN)
195 #define MAYBE_MultiThreadConsumerTest DISABLED_MultiThreadConsumerTest 248 #define MAYBE_MultiThreadConsumerTest DISABLED_MultiThreadConsumerTest
196 #else 249 #else
197 #define MAYBE_MultiThreadConsumerTest MultiThreadConsumerTest 250 #define MAYBE_MultiThreadConsumerTest MultiThreadConsumerTest
198 #endif 251 #endif
199 // Test serial task servicing, as well as two parallel task servicing methods. 252 // Test serial task servicing, as well as two parallel task servicing methods.
200 TEST_F(ConditionVariableTest, MAYBE_MultiThreadConsumerTest) { 253 TEST_F(ConditionVariableTest, MAYBE_MultiThreadConsumerTest) {
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 base::AutoLock auto_lock(lock_); 759 base::AutoLock auto_lock(lock_);
707 // Send notification that we completed our "work." 760 // Send notification that we completed our "work."
708 WorkIsCompleted(thread_id); 761 WorkIsCompleted(thread_id);
709 } 762 }
710 } 763 }
711 } 764 }
712 765
713 } // namespace 766 } // namespace
714 767
715 } // namespace base 768 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/condition_variable_posix.cc ('k') | base/synchronization/waitable_event_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698