OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/edk/util/waitable_event.h" | 5 #include "mojo/edk/system/waitable_event.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <atomic> | 10 #include <atomic> |
11 #include <thread> | 11 #include <thread> |
12 #include <type_traits> | 12 #include <type_traits> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "mojo/edk/system/test/sleep.h" | 15 #include "mojo/edk/system/test/sleep.h" |
16 #include "mojo/edk/system/test/stopwatch.h" | 16 #include "mojo/edk/system/test/stopwatch.h" |
17 #include "mojo/edk/system/test/timeouts.h" | 17 #include "mojo/edk/system/test/timeouts.h" |
18 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 using mojo::system::test::ActionTimeout; | |
22 using mojo::system::test::DeadlineFromMilliseconds; | |
23 using mojo::system::test::EpsilonTimeout; | |
24 using mojo::system::test::Sleep; | |
25 using mojo::system::test::SleepMilliseconds; | |
26 using mojo::system::test::Stopwatch; | |
27 using mojo::system::test::TinyTimeout; | |
28 | |
29 namespace mojo { | 21 namespace mojo { |
30 namespace util { | 22 namespace system { |
31 namespace { | 23 namespace { |
32 | 24 |
33 // Sleeps for a "very small" amount of time. | 25 // Sleeps for a "very small" amount of time. |
34 void EpsilonRandomSleep() { | 26 void EpsilonRandomSleep() { |
35 SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); | 27 test::SleepMilliseconds(static_cast<unsigned>(rand()) % 20u); |
36 } | 28 } |
37 | 29 |
38 // We'll use |MojoDeadline| with |uint64_t| (for |WaitWithTimeout()|'s timeout | 30 // We'll use |MojoDeadline| with |uint64_t| (for |WaitWithTimeout()|'s timeout |
39 // argument), though note that |WaitWithTimeout()| doesn't support | 31 // argument), though note that |WaitWithTimeout()| doesn't support |
40 // |MOJO_DEADLINE_INDEFINITE|. | 32 // |MOJO_DEADLINE_INDEFINITE|. |
41 static_assert(std::is_same<uint64_t, MojoDeadline>::value, | 33 static_assert(std::is_same<uint64_t, MojoDeadline>::value, |
42 "MojoDeadline isn't uint64_t!"); | 34 "MojoDeadline isn't uint64_t!"); |
43 | 35 |
44 // AutoResetWaitableEvent ------------------------------------------------------ | 36 // AutoResetWaitableEvent ------------------------------------------------------ |
45 | 37 |
46 TEST(AutoResetWaitableEventTest, Basic) { | 38 TEST(AutoResetWaitableEventTest, Basic) { |
47 AutoResetWaitableEvent ev; | 39 AutoResetWaitableEvent ev; |
48 EXPECT_FALSE(ev.IsSignaledForTest()); | 40 EXPECT_FALSE(ev.IsSignaledForTest()); |
49 ev.Signal(); | 41 ev.Signal(); |
50 EXPECT_TRUE(ev.IsSignaledForTest()); | 42 EXPECT_TRUE(ev.IsSignaledForTest()); |
51 ev.Wait(); | 43 ev.Wait(); |
52 EXPECT_FALSE(ev.IsSignaledForTest()); | 44 EXPECT_FALSE(ev.IsSignaledForTest()); |
53 ev.Reset(); | 45 ev.Reset(); |
54 EXPECT_FALSE(ev.IsSignaledForTest()); | 46 EXPECT_FALSE(ev.IsSignaledForTest()); |
55 ev.Signal(); | 47 ev.Signal(); |
56 EXPECT_TRUE(ev.IsSignaledForTest()); | 48 EXPECT_TRUE(ev.IsSignaledForTest()); |
57 ev.Reset(); | 49 ev.Reset(); |
58 EXPECT_FALSE(ev.IsSignaledForTest()); | 50 EXPECT_FALSE(ev.IsSignaledForTest()); |
59 EXPECT_TRUE(ev.WaitWithTimeout(0)); | 51 EXPECT_TRUE(ev.WaitWithTimeout(0)); |
60 EXPECT_FALSE(ev.IsSignaledForTest()); | 52 EXPECT_FALSE(ev.IsSignaledForTest()); |
61 EXPECT_TRUE(ev.WaitWithTimeout(DeadlineFromMilliseconds(1))); | 53 EXPECT_TRUE(ev.WaitWithTimeout(test::DeadlineFromMilliseconds(1))); |
62 EXPECT_FALSE(ev.IsSignaledForTest()); | 54 EXPECT_FALSE(ev.IsSignaledForTest()); |
63 ev.Signal(); | 55 ev.Signal(); |
64 EXPECT_TRUE(ev.IsSignaledForTest()); | 56 EXPECT_TRUE(ev.IsSignaledForTest()); |
65 EXPECT_FALSE(ev.WaitWithTimeout(0)); | 57 EXPECT_FALSE(ev.WaitWithTimeout(0)); |
66 EXPECT_FALSE(ev.IsSignaledForTest()); | 58 EXPECT_FALSE(ev.IsSignaledForTest()); |
67 EXPECT_TRUE(ev.WaitWithTimeout(DeadlineFromMilliseconds(1))); | 59 EXPECT_TRUE(ev.WaitWithTimeout(test::DeadlineFromMilliseconds(1))); |
68 EXPECT_FALSE(ev.IsSignaledForTest()); | 60 EXPECT_FALSE(ev.IsSignaledForTest()); |
69 ev.Signal(); | 61 ev.Signal(); |
70 EXPECT_FALSE(ev.WaitWithTimeout(DeadlineFromMilliseconds(1))); | 62 EXPECT_FALSE(ev.WaitWithTimeout(test::DeadlineFromMilliseconds(1))); |
71 EXPECT_FALSE(ev.IsSignaledForTest()); | 63 EXPECT_FALSE(ev.IsSignaledForTest()); |
72 } | 64 } |
73 | 65 |
74 TEST(AutoResetWaitableEventTest, MultipleWaiters) { | 66 TEST(AutoResetWaitableEventTest, MultipleWaiters) { |
75 AutoResetWaitableEvent ev; | 67 AutoResetWaitableEvent ev; |
76 | 68 |
77 for (size_t i = 0u; i < 5u; i++) { | 69 for (size_t i = 0u; i < 5u; i++) { |
78 std::atomic_uint wake_count(0u); | 70 std::atomic_uint wake_count(0u); |
79 std::vector<std::thread> threads; | 71 std::vector<std::thread> threads; |
80 for (size_t j = 0u; j < 4u; j++) { | 72 for (size_t j = 0u; j < 4u; j++) { |
81 threads.push_back(std::thread([&ev, &wake_count]() { | 73 threads.push_back(std::thread([&ev, &wake_count]() { |
82 if (rand() % 2 == 0) | 74 if (rand() % 2 == 0) |
83 ev.Wait(); | 75 ev.Wait(); |
84 else | 76 else |
85 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); | 77 EXPECT_FALSE(ev.WaitWithTimeout(test::ActionTimeout())); |
86 wake_count.fetch_add(1u); | 78 wake_count.fetch_add(1u); |
87 // Note: We can't say anything about the signaled state of |ev| here, | 79 // Note: We can't say anything about the signaled state of |ev| here, |
88 // since the main thread may have already signaled it again. | 80 // since the main thread may have already signaled it again. |
89 })); | 81 })); |
90 } | 82 } |
91 | 83 |
92 // Unfortunately, we can't really wait for the threads to be waiting, so we | 84 // Unfortunately, we can't really wait for the threads to be waiting, so we |
93 // just sleep for a bit, and count on them having started and advanced to | 85 // just sleep for a bit, and count on them having started and advanced to |
94 // waiting. | 86 // waiting. |
95 Sleep(2 * TinyTimeout()); | 87 test::Sleep(2 * test::TinyTimeout()); |
96 | 88 |
97 for (size_t j = 0u; j < threads.size(); j++) { | 89 for (size_t j = 0u; j < threads.size(); j++) { |
98 unsigned old_wake_count = wake_count.load(); | 90 unsigned old_wake_count = wake_count.load(); |
99 EXPECT_EQ(j, old_wake_count); | 91 EXPECT_EQ(j, old_wake_count); |
100 | 92 |
101 // Each |Signal()| should wake exactly one thread. | 93 // Each |Signal()| should wake exactly one thread. |
102 ev.Signal(); | 94 ev.Signal(); |
103 | 95 |
104 // Poll for |wake_count| to change. | 96 // Poll for |wake_count| to change. |
105 while (wake_count.load() == old_wake_count) | 97 while (wake_count.load() == old_wake_count) |
106 Sleep(EpsilonTimeout()); | 98 test::Sleep(test::EpsilonTimeout()); |
107 | 99 |
108 EXPECT_FALSE(ev.IsSignaledForTest()); | 100 EXPECT_FALSE(ev.IsSignaledForTest()); |
109 | 101 |
110 // And once it's changed, wait a little longer, to see if any other | 102 // And once it's changed, wait a little longer, to see if any other |
111 // threads are awoken (they shouldn't be). | 103 // threads are awoken (they shouldn't be). |
112 Sleep(EpsilonTimeout()); | 104 test::Sleep(test::EpsilonTimeout()); |
113 | 105 |
114 EXPECT_EQ(old_wake_count + 1u, wake_count.load()); | 106 EXPECT_EQ(old_wake_count + 1u, wake_count.load()); |
115 | 107 |
116 EXPECT_FALSE(ev.IsSignaledForTest()); | 108 EXPECT_FALSE(ev.IsSignaledForTest()); |
117 } | 109 } |
118 | 110 |
119 // Having done that, if we signal |ev| now, it should stay signaled. | 111 // Having done that, if we signal |ev| now, it should stay signaled. |
120 ev.Signal(); | 112 ev.Signal(); |
121 Sleep(EpsilonTimeout()); | 113 test::Sleep(test::EpsilonTimeout()); |
122 EXPECT_TRUE(ev.IsSignaledForTest()); | 114 EXPECT_TRUE(ev.IsSignaledForTest()); |
123 | 115 |
124 for (auto& thread : threads) | 116 for (auto& thread : threads) |
125 thread.join(); | 117 thread.join(); |
126 | 118 |
127 ev.Reset(); | 119 ev.Reset(); |
128 } | 120 } |
129 } | 121 } |
130 | 122 |
131 TEST(AutoResetWaitableEventTest, Timeouts) { | 123 TEST(AutoResetWaitableEventTest, Timeouts) { |
132 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80}; | 124 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80}; |
133 | 125 |
134 Stopwatch stopwatch; | 126 test::Stopwatch stopwatch; |
135 | 127 |
136 AutoResetWaitableEvent ev; | 128 AutoResetWaitableEvent ev; |
137 | 129 |
138 for (size_t i = 0u; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { | 130 for (size_t i = 0u; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { |
139 uint64_t timeout = DeadlineFromMilliseconds(kTestTimeoutsMs[i]); | 131 uint64_t timeout = test::DeadlineFromMilliseconds(kTestTimeoutsMs[i]); |
140 | 132 |
141 stopwatch.Start(); | 133 stopwatch.Start(); |
142 EXPECT_TRUE(ev.WaitWithTimeout(timeout)); | 134 EXPECT_TRUE(ev.WaitWithTimeout(timeout)); |
143 MojoDeadline elapsed = stopwatch.Elapsed(); | 135 MojoDeadline elapsed = stopwatch.Elapsed(); |
144 | 136 |
145 // It should time out after *at least* the specified amount of time. | 137 // It should time out after *at least* the specified amount of time. |
146 EXPECT_GE(elapsed, timeout); | 138 EXPECT_GE(elapsed, timeout); |
147 // But we expect that it should time out soon after that amount of time. | 139 // But we expect that it should time out soon after that amount of time. |
148 EXPECT_LT(elapsed, timeout + EpsilonTimeout()); | 140 EXPECT_LT(elapsed, timeout + test::EpsilonTimeout()); |
149 } | 141 } |
150 } | 142 } |
151 | 143 |
152 // ManualResetWaitableEvent ---------------------------------------------------- | 144 // ManualResetWaitableEvent ---------------------------------------------------- |
153 | 145 |
154 TEST(ManualResetWaitableEventTest, Basic) { | 146 TEST(ManualResetWaitableEventTest, Basic) { |
155 ManualResetWaitableEvent ev; | 147 ManualResetWaitableEvent ev; |
156 EXPECT_FALSE(ev.IsSignaledForTest()); | 148 EXPECT_FALSE(ev.IsSignaledForTest()); |
157 ev.Signal(); | 149 ev.Signal(); |
158 EXPECT_TRUE(ev.IsSignaledForTest()); | 150 EXPECT_TRUE(ev.IsSignaledForTest()); |
159 ev.Wait(); | 151 ev.Wait(); |
160 EXPECT_TRUE(ev.IsSignaledForTest()); | 152 EXPECT_TRUE(ev.IsSignaledForTest()); |
161 ev.Reset(); | 153 ev.Reset(); |
162 EXPECT_FALSE(ev.IsSignaledForTest()); | 154 EXPECT_FALSE(ev.IsSignaledForTest()); |
163 EXPECT_TRUE(ev.WaitWithTimeout(0)); | 155 EXPECT_TRUE(ev.WaitWithTimeout(0)); |
164 EXPECT_FALSE(ev.IsSignaledForTest()); | 156 EXPECT_FALSE(ev.IsSignaledForTest()); |
165 EXPECT_TRUE(ev.WaitWithTimeout(DeadlineFromMilliseconds(1))); | 157 EXPECT_TRUE(ev.WaitWithTimeout(test::DeadlineFromMilliseconds(1))); |
166 EXPECT_FALSE(ev.IsSignaledForTest()); | 158 EXPECT_FALSE(ev.IsSignaledForTest()); |
167 ev.Signal(); | 159 ev.Signal(); |
168 EXPECT_TRUE(ev.IsSignaledForTest()); | 160 EXPECT_TRUE(ev.IsSignaledForTest()); |
169 EXPECT_FALSE(ev.WaitWithTimeout(0)); | 161 EXPECT_FALSE(ev.WaitWithTimeout(0)); |
170 EXPECT_TRUE(ev.IsSignaledForTest()); | 162 EXPECT_TRUE(ev.IsSignaledForTest()); |
171 EXPECT_FALSE(ev.WaitWithTimeout(DeadlineFromMilliseconds(1))); | 163 EXPECT_FALSE(ev.WaitWithTimeout(test::DeadlineFromMilliseconds(1))); |
172 EXPECT_TRUE(ev.IsSignaledForTest()); | 164 EXPECT_TRUE(ev.IsSignaledForTest()); |
173 } | 165 } |
174 | 166 |
175 TEST(ManualResetWaitableEventTest, SignalMultiple) { | 167 TEST(ManualResetWaitableEventTest, SignalMultiple) { |
176 ManualResetWaitableEvent ev; | 168 ManualResetWaitableEvent ev; |
177 | 169 |
178 for (size_t i = 0u; i < 10u; i++) { | 170 for (size_t i = 0u; i < 10u; i++) { |
179 for (size_t num_waiters = 1u; num_waiters < 5u; num_waiters++) { | 171 for (size_t num_waiters = 1u; num_waiters < 5u; num_waiters++) { |
180 std::vector<std::thread> threads; | 172 std::vector<std::thread> threads; |
181 for (size_t j = 0u; j < num_waiters; j++) { | 173 for (size_t j = 0u; j < num_waiters; j++) { |
182 threads.push_back(std::thread([&ev]() { | 174 threads.push_back(std::thread([&ev]() { |
183 EpsilonRandomSleep(); | 175 EpsilonRandomSleep(); |
184 | 176 |
185 if (rand() % 2 == 0) | 177 if (rand() % 2 == 0) |
186 ev.Wait(); | 178 ev.Wait(); |
187 else | 179 else |
188 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); | 180 EXPECT_FALSE(ev.WaitWithTimeout(test::ActionTimeout())); |
189 })); | 181 })); |
190 } | 182 } |
191 | 183 |
192 EpsilonRandomSleep(); | 184 EpsilonRandomSleep(); |
193 | 185 |
194 ev.Signal(); | 186 ev.Signal(); |
195 | 187 |
196 // The threads will only terminate once they've successfully waited (or | 188 // The threads will only terminate once they've successfully waited (or |
197 // timed out). | 189 // timed out). |
198 for (auto& thread : threads) | 190 for (auto& thread : threads) |
199 thread.join(); | 191 thread.join(); |
200 | 192 |
201 ev.Reset(); | 193 ev.Reset(); |
202 } | 194 } |
203 } | 195 } |
204 } | 196 } |
205 | 197 |
206 // Tries to test that threads that are awoken may immediately call |Reset()| | 198 // Tries to test that threads that are awoken may immediately call |Reset()| |
207 // without affecting other threads that are awoken. | 199 // without affecting other threads that are awoken. |
208 TEST(ManualResetWaitableEventTest, SignalMultipleWaitReset) { | 200 TEST(ManualResetWaitableEventTest, SignalMultipleWaitReset) { |
209 ManualResetWaitableEvent ev; | 201 ManualResetWaitableEvent ev; |
210 | 202 |
211 for (size_t i = 0u; i < 5u; i++) { | 203 for (size_t i = 0u; i < 5u; i++) { |
212 std::vector<std::thread> threads; | 204 std::vector<std::thread> threads; |
213 for (size_t j = 0u; j < 4u; j++) { | 205 for (size_t j = 0u; j < 4u; j++) { |
214 threads.push_back(std::thread([&ev]() { | 206 threads.push_back(std::thread([&ev]() { |
215 if (rand() % 2 == 0) | 207 if (rand() % 2 == 0) |
216 ev.Wait(); | 208 ev.Wait(); |
217 else | 209 else |
218 EXPECT_FALSE(ev.WaitWithTimeout(ActionTimeout())); | 210 EXPECT_FALSE(ev.WaitWithTimeout(test::ActionTimeout())); |
219 ev.Reset(); | 211 ev.Reset(); |
220 })); | 212 })); |
221 } | 213 } |
222 | 214 |
223 // Unfortunately, we can't really wait for the threads to be waiting, so we | 215 // Unfortunately, we can't really wait for the threads to be waiting, so we |
224 // just sleep for a bit, and count on them having started and advanced to | 216 // just sleep for a bit, and count on them having started and advanced to |
225 // waiting. | 217 // waiting. |
226 Sleep(2 * TinyTimeout()); | 218 test::Sleep(2 * test::TinyTimeout()); |
227 | 219 |
228 ev.Signal(); | 220 ev.Signal(); |
229 | 221 |
230 // In fact, we may ourselves call |Reset()| immediately. | 222 // In fact, we may ourselves call |Reset()| immediately. |
231 ev.Reset(); | 223 ev.Reset(); |
232 | 224 |
233 // The threads will only terminate once they've successfully waited (or | 225 // The threads will only terminate once they've successfully waited (or |
234 // timed out). | 226 // timed out). |
235 for (auto& thread : threads) | 227 for (auto& thread : threads) |
236 thread.join(); | 228 thread.join(); |
237 | 229 |
238 ASSERT_FALSE(ev.IsSignaledForTest()); | 230 ASSERT_FALSE(ev.IsSignaledForTest()); |
239 } | 231 } |
240 } | 232 } |
241 | 233 |
242 TEST(ManualResetWaitableEventTest, Timeouts) { | 234 TEST(ManualResetWaitableEventTest, Timeouts) { |
243 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80}; | 235 static const unsigned kTestTimeoutsMs[] = {0, 10, 20, 40, 80}; |
244 | 236 |
245 Stopwatch stopwatch; | 237 test::Stopwatch stopwatch; |
246 | 238 |
247 ManualResetWaitableEvent ev; | 239 ManualResetWaitableEvent ev; |
248 | 240 |
249 for (size_t i = 0u; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { | 241 for (size_t i = 0u; i < MOJO_ARRAYSIZE(kTestTimeoutsMs); i++) { |
250 uint64_t timeout = DeadlineFromMilliseconds(kTestTimeoutsMs[i]); | 242 uint64_t timeout = test::DeadlineFromMilliseconds(kTestTimeoutsMs[i]); |
251 | 243 |
252 stopwatch.Start(); | 244 stopwatch.Start(); |
253 EXPECT_TRUE(ev.WaitWithTimeout(timeout)); | 245 EXPECT_TRUE(ev.WaitWithTimeout(timeout)); |
254 MojoDeadline elapsed = stopwatch.Elapsed(); | 246 MojoDeadline elapsed = stopwatch.Elapsed(); |
255 | 247 |
256 // It should time out after *at least* the specified amount of time. | 248 // It should time out after *at least* the specified amount of time. |
257 EXPECT_GE(elapsed, timeout); | 249 EXPECT_GE(elapsed, timeout); |
258 // But we expect that it should time out soon after that amount of time. | 250 // But we expect that it should time out soon after that amount of time. |
259 EXPECT_LT(elapsed, timeout + EpsilonTimeout()); | 251 EXPECT_LT(elapsed, timeout + test::EpsilonTimeout()); |
260 } | 252 } |
261 } | 253 } |
262 | 254 |
263 } // namespace | 255 } // namespace |
264 } // namespace util | 256 } // namespace system |
265 } // namespace mojo | 257 } // namespace mojo |
OLD | NEW |