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 #include "base/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 129 |
130 for (unsigned i = 0; i < 5; ++i) | 130 for (unsigned i = 0; i < 5; ++i) |
131 delete ev[i]; | 131 delete ev[i]; |
132 | 132 |
133 PlatformThread::Join(thread); | 133 PlatformThread::Join(thread); |
134 EXPECT_EQ(2u, index); | 134 EXPECT_EQ(2u, index); |
135 } | 135 } |
136 | 136 |
137 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing | 137 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing |
138 // a timeout of 0. (crbug.com/465948) | 138 // a timeout of 0. (crbug.com/465948) |
139 #if defined(OS_POSIX) | 139 TEST(WaitableEventTest, TimedWait) { |
140 // crbug.com/465948 not fixed yet. | |
141 #define MAYBE_TimedWait DISABLED_TimedWait | |
142 #else | |
143 #define MAYBE_TimedWait TimedWait | |
144 #endif | |
145 TEST(WaitableEventTest, MAYBE_TimedWait) { | |
146 WaitableEvent* ev = | 140 WaitableEvent* ev = |
147 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, | 141 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
148 WaitableEvent::InitialState::NOT_SIGNALED); | 142 WaitableEvent::InitialState::NOT_SIGNALED); |
149 | 143 |
150 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10); | 144 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10); |
151 WaitableEventSignaler signaler(thread_delay, ev); | 145 WaitableEventSignaler signaler(thread_delay, ev); |
152 PlatformThreadHandle thread; | 146 PlatformThreadHandle thread; |
153 TimeTicks start = TimeTicks::Now(); | 147 TimeTicks start = TimeTicks::Now(); |
154 PlatformThread::Create(0, &signaler, &thread); | 148 PlatformThread::Create(0, &signaler, &thread); |
155 | 149 |
156 ev->TimedWait(TimeDelta::Max()); | 150 EXPECT_TRUE(ev->TimedWait(TimeDelta::Max())); |
157 EXPECT_GE(TimeTicks::Now() - start, thread_delay); | 151 EXPECT_GE(TimeTicks::Now() - start, thread_delay); |
158 delete ev; | 152 delete ev; |
159 | 153 |
160 PlatformThread::Join(thread); | 154 PlatformThread::Join(thread); |
161 } | 155 } |
162 | 156 |
| 157 // Tests that a sub-ms TimedWait doesn't time out promptly. |
| 158 TEST(WaitableEventTest, SubMsTimedWait) { |
| 159 WaitableEvent ev(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 160 WaitableEvent::InitialState::NOT_SIGNALED); |
| 161 |
| 162 TimeDelta delay = TimeDelta::FromMicroseconds(900); |
| 163 TimeTicks start_time = TimeTicks::Now(); |
| 164 ev.TimedWait(delay); |
| 165 EXPECT_GE(TimeTicks::Now() - start_time, delay); |
| 166 } |
| 167 |
| 168 // Tests that TimedWaitUntil can be safely used with various end_time deadline |
| 169 // values. |
| 170 TEST(WaitableEventTest, TimedWaitUntil) { |
| 171 WaitableEvent ev(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 172 WaitableEvent::InitialState::NOT_SIGNALED); |
| 173 |
| 174 TimeTicks start_time(TimeTicks::Now()); |
| 175 TimeDelta delay = TimeDelta::FromMilliseconds(10); |
| 176 |
| 177 // Should be OK to wait for the current time or time in the past. |
| 178 // That should end promptly and be equivalent to IsSignalled. |
| 179 EXPECT_FALSE(ev.TimedWaitUntil(start_time)); |
| 180 EXPECT_FALSE(ev.TimedWaitUntil(start_time - delay)); |
| 181 |
| 182 // Should be OK to wait for zero TimeTicks(). |
| 183 EXPECT_FALSE(ev.TimedWaitUntil(TimeTicks())); |
| 184 |
| 185 // Waiting for a time in the future shouldn't end before the deadline |
| 186 // if the event isn't signalled. |
| 187 EXPECT_FALSE(ev.TimedWaitUntil(start_time + delay)); |
| 188 EXPECT_GE(TimeTicks::Now() - start_time, delay); |
| 189 |
| 190 // Test that passing TimeTicks::Max to TimedWaitUntil is valid and isn't |
| 191 // the same as passing TimeTicks(). Also verifies that signaling event |
| 192 // ends the wait promptly. |
| 193 WaitableEventSignaler signaler(delay, &ev); |
| 194 PlatformThreadHandle thread; |
| 195 start_time = TimeTicks::Now(); |
| 196 PlatformThread::Create(0, &signaler, &thread); |
| 197 |
| 198 EXPECT_TRUE(ev.TimedWaitUntil(TimeTicks::Max())); |
| 199 EXPECT_GE(TimeTicks::Now() - start_time, delay); |
| 200 |
| 201 PlatformThread::Join(thread); |
| 202 } |
| 203 |
163 } // namespace base | 204 } // namespace base |
OLD | NEW |