| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/system_wrappers/interface/condition_variable_wrapper.h" | 11 #include "webrtc/system_wrappers/interface/condition_variable_wrapper.h" |
| 12 | 12 |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/base/scoped_ptr.h" |
| 14 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | 15 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 15 #include "webrtc/system_wrappers/interface/thread_wrapper.h" | 16 #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| 17 #include "webrtc/system_wrappers/interface/tick_util.h" |
| 16 #include "webrtc/system_wrappers/interface/trace.h" | 18 #include "webrtc/system_wrappers/interface/trace.h" |
| 17 | 19 |
| 18 namespace webrtc { | 20 namespace webrtc { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const int kLongWaitMs = 100 * 1000; // A long time in testing terms | 24 const int kLongWaitMs = 100 * 1000; // A long time in testing terms |
| 23 const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen | 25 const int kShortWaitMs = 2 * 1000; // Long enough for process switches to happen |
| 26 const int kVeryShortWaitMs = 20; // Used when we want a timeout |
| 24 | 27 |
| 25 // A Baton is one possible control structure one can build using | 28 // A Baton is one possible control structure one can build using |
| 26 // conditional variables. | 29 // conditional variables. |
| 27 // A Baton is always held by one and only one active thread - unlike | 30 // A Baton is always held by one and only one active thread - unlike |
| 28 // a lock, it can never be free. | 31 // a lock, it can never be free. |
| 29 // One can pass it or grab it - both calls have timeouts. | 32 // One can pass it or grab it - both calls have timeouts. |
| 30 // Note - a production tool would guard against passing it without | 33 // Note - a production tool would guard against passing it without |
| 31 // grabbing it first. This one is for testing, so it doesn't. | 34 // grabbing it first. This one is for testing, so it doesn't. |
| 32 class Baton { | 35 class Baton { |
| 33 public: | 36 public: |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 // This test verifies that one can use the baton multiple times. | 180 // This test verifies that one can use the baton multiple times. |
| 178 TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) { | 181 TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) { |
| 179 const int kNumberOfRounds = 2; | 182 const int kNumberOfRounds = 2; |
| 180 for (int i = 0; i < kNumberOfRounds; ++i) { | 183 for (int i = 0; i < kNumberOfRounds; ++i) { |
| 181 ASSERT_TRUE(baton_.Pass(kShortWaitMs)); | 184 ASSERT_TRUE(baton_.Pass(kShortWaitMs)); |
| 182 ASSERT_TRUE(baton_.Grab(kShortWaitMs)); | 185 ASSERT_TRUE(baton_.Grab(kShortWaitMs)); |
| 183 } | 186 } |
| 184 EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount()); | 187 EXPECT_EQ(2 * kNumberOfRounds, baton_.PassCount()); |
| 185 } | 188 } |
| 186 | 189 |
| 190 TEST(CondVarWaitTest, WaitingWaits) { |
| 191 rtc::scoped_ptr<CriticalSectionWrapper> crit_sect( |
| 192 CriticalSectionWrapper::CreateCriticalSection()); |
| 193 rtc::scoped_ptr<ConditionVariableWrapper> cond_var( |
| 194 ConditionVariableWrapper::CreateConditionVariable()); |
| 195 CriticalSectionScoped cs(crit_sect.get()); |
| 196 int64_t start_ms = TickTime::MillisecondTimestamp(); |
| 197 EXPECT_FALSE(cond_var->SleepCS(*(crit_sect), kVeryShortWaitMs)); |
| 198 int64_t end_ms = TickTime::MillisecondTimestamp(); |
| 199 EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms) |
| 200 << "actual elapsed:" << end_ms - start_ms; |
| 201 } |
| 202 |
| 187 } // anonymous namespace | 203 } // anonymous namespace |
| 188 | 204 |
| 189 } // namespace webrtc | 205 } // namespace webrtc |
| OLD | NEW |