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 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 TimedWaitUntil. | |
danakj
2016/11/16 22:57:29
this comment isn't adding value
stanisc
2016/11/17 19:42:22
Done.
| |
169 TEST(WaitableEventTest, TimedWaitUntil) { | |
170 WaitableEvent ev(WaitableEvent::ResetPolicy::AUTOMATIC, | |
171 WaitableEvent::InitialState::NOT_SIGNALED); | |
172 | |
173 TimeTicks start_time(TimeTicks::Now()); | |
174 TimeDelta delay = TimeDelta::FromMilliseconds(10); | |
175 | |
176 // Should be OK to wait for the current time or time in the past. | |
177 // That should end promptly and be equivalent to IsSignalled. | |
178 EXPECT_FALSE(ev.TimedWaitUntil(start_time)); | |
179 EXPECT_FALSE(ev.TimedWaitUntil(start_time - delay)); | |
180 | |
181 // Waiting for a time in the future shouldn't end before the deadline | |
182 // if the event isn't signalled. | |
183 EXPECT_FALSE(ev.TimedWaitUntil(start_time + delay)); | |
184 EXPECT_GE(TimeTicks::Now() - start_time, delay); | |
185 | |
186 // A test similr to the one in TimedWait test above. | |
danakj
2016/11/16 22:57:29
similar
But maybe copy the explanation there over
stanisc
2016/11/17 19:42:22
Done.
| |
187 WaitableEventSignaler signaler(delay, &ev); | |
188 PlatformThreadHandle thread; | |
189 start_time = TimeTicks::Now(); | |
190 PlatformThread::Create(0, &signaler, &thread); | |
191 | |
192 ev.TimedWaitUntil(TimeTicks::Max()); | |
193 EXPECT_GE(TimeTicks::Now() - start_time, delay); | |
194 | |
195 PlatformThread::Join(thread); | |
196 } | |
197 | |
163 } // namespace base | 198 } // namespace base |
OLD | NEW |