OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // NOTE(vtl): These tests are inherently flaky (e.g., if run on a heavily-loaded | 5 // NOTE(vtl): These tests are inherently flaky (e.g., if run on a heavily-loaded |
6 // system). Sorry. |kEpsilonMicros| may be increased to increase tolerance and | 6 // system). Sorry. |kEpsilonMicros| may be increased to increase tolerance and |
7 // reduce observed flakiness. | 7 // reduce observed flakiness. |
8 | 8 |
9 #include "mojo/system/waiter_list.h" | 9 #include "mojo/system/waiter_list.h" |
10 | 10 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 base::PlatformThread::Sleep( | 139 base::PlatformThread::Sleep( |
140 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); | 140 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); |
141 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_WRITABLE); | 141 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_WRITABLE); |
142 waiter_list.RemoveWaiter(thread.waiter()); | 142 waiter_list.RemoveWaiter(thread.waiter()); |
143 waiter_list.RemoveWaiter(thread.waiter()); // Double-remove okay. | 143 waiter_list.RemoveWaiter(thread.waiter()); // Double-remove okay. |
144 } // Join |thread|. | 144 } // Join |thread|. |
145 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | 145 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
146 } | 146 } |
147 | 147 |
148 TEST(WaiterListTest, MultipleWaiters) { | 148 TEST(WaiterListTest, MultipleWaiters) { |
149 MojoResult result_1; | 149 MojoResult result1; |
150 MojoResult result_2; | 150 MojoResult result2; |
151 MojoResult result_3; | 151 MojoResult result3; |
152 MojoResult result_4; | 152 MojoResult result4; |
153 | 153 |
154 // Cancel two waiters. | 154 // Cancel two waiters. |
155 { | 155 { |
156 WaiterList waiter_list; | 156 WaiterList waiter_list; |
157 test::SimpleWaiterThread thread_1(&result_1); | 157 test::SimpleWaiterThread thread1(&result1); |
158 waiter_list.AddWaiter(thread_1.waiter(), MOJO_WAIT_FLAG_READABLE, 0); | 158 waiter_list.AddWaiter(thread1.waiter(), MOJO_WAIT_FLAG_READABLE, 0); |
159 thread_1.Start(); | 159 thread1.Start(); |
160 test::SimpleWaiterThread thread_2(&result_2); | 160 test::SimpleWaiterThread thread2(&result2); |
161 waiter_list.AddWaiter(thread_2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 1); | 161 waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 1); |
162 thread_2.Start(); | 162 thread2.Start(); |
163 base::PlatformThread::Sleep( | 163 base::PlatformThread::Sleep( |
164 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); | 164 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); |
165 waiter_list.CancelAllWaiters(); | 165 waiter_list.CancelAllWaiters(); |
166 } // Join threads. | 166 } // Join threads. |
167 EXPECT_EQ(MOJO_RESULT_CANCELLED, result_1); | 167 EXPECT_EQ(MOJO_RESULT_CANCELLED, result1); |
168 EXPECT_EQ(MOJO_RESULT_CANCELLED, result_2); | 168 EXPECT_EQ(MOJO_RESULT_CANCELLED, result2); |
169 | 169 |
170 // Awake one waiter, cancel other. | 170 // Awake one waiter, cancel other. |
171 { | 171 { |
172 WaiterList waiter_list; | 172 WaiterList waiter_list; |
173 test::SimpleWaiterThread thread_1(&result_1); | 173 test::SimpleWaiterThread thread1(&result1); |
174 waiter_list.AddWaiter(thread_1.waiter(), MOJO_WAIT_FLAG_READABLE, 2); | 174 waiter_list.AddWaiter(thread1.waiter(), MOJO_WAIT_FLAG_READABLE, 2); |
175 thread_1.Start(); | 175 thread1.Start(); |
176 test::SimpleWaiterThread thread_2(&result_2); | 176 test::SimpleWaiterThread thread2(&result2); |
177 waiter_list.AddWaiter(thread_2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 3); | 177 waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 3); |
178 thread_2.Start(); | 178 thread2.Start(); |
179 base::PlatformThread::Sleep( | 179 base::PlatformThread::Sleep( |
180 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); | 180 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); |
181 waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, | 181 waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, |
182 MOJO_WAIT_FLAG_READABLE | | 182 MOJO_WAIT_FLAG_READABLE | |
183 MOJO_WAIT_FLAG_WRITABLE); | 183 MOJO_WAIT_FLAG_WRITABLE); |
184 waiter_list.RemoveWaiter(thread_1.waiter()); | 184 waiter_list.RemoveWaiter(thread1.waiter()); |
185 waiter_list.CancelAllWaiters(); | 185 waiter_list.CancelAllWaiters(); |
186 } // Join threads. | 186 } // Join threads. |
187 EXPECT_EQ(2, result_1); | 187 EXPECT_EQ(2, result1); |
188 EXPECT_EQ(MOJO_RESULT_CANCELLED, result_2); | 188 EXPECT_EQ(MOJO_RESULT_CANCELLED, result2); |
189 | 189 |
190 // Cancel one waiter, awake other for unsatisfiability. | 190 // Cancel one waiter, awake other for unsatisfiability. |
191 { | 191 { |
192 WaiterList waiter_list; | 192 WaiterList waiter_list; |
193 test::SimpleWaiterThread thread_1(&result_1); | 193 test::SimpleWaiterThread thread1(&result1); |
194 waiter_list.AddWaiter(thread_1.waiter(), MOJO_WAIT_FLAG_READABLE, 4); | 194 waiter_list.AddWaiter(thread1.waiter(), MOJO_WAIT_FLAG_READABLE, 4); |
195 thread_1.Start(); | 195 thread1.Start(); |
196 test::SimpleWaiterThread thread_2(&result_2); | 196 test::SimpleWaiterThread thread2(&result2); |
197 waiter_list.AddWaiter(thread_2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 5); | 197 waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 5); |
198 thread_2.Start(); | 198 thread2.Start(); |
199 base::PlatformThread::Sleep( | 199 base::PlatformThread::Sleep( |
200 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); | 200 base::TimeDelta::FromMicroseconds(2 * kEpsilonMicros)); |
201 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); | 201 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); |
202 waiter_list.RemoveWaiter(thread_2.waiter()); | 202 waiter_list.RemoveWaiter(thread2.waiter()); |
203 waiter_list.CancelAllWaiters(); | 203 waiter_list.CancelAllWaiters(); |
204 } // Join threads. | 204 } // Join threads. |
205 EXPECT_EQ(MOJO_RESULT_CANCELLED, result_1); | 205 EXPECT_EQ(MOJO_RESULT_CANCELLED, result1); |
206 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result_2); | 206 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result2); |
207 | 207 |
208 // Cancel one waiter, awake other for unsatisfiability. | 208 // Cancel one waiter, awake other for unsatisfiability. |
209 { | 209 { |
210 WaiterList waiter_list; | 210 WaiterList waiter_list; |
211 test::SimpleWaiterThread thread_1(&result_1); | 211 test::SimpleWaiterThread thread1(&result1); |
212 waiter_list.AddWaiter(thread_1.waiter(), MOJO_WAIT_FLAG_READABLE, 6); | 212 waiter_list.AddWaiter(thread1.waiter(), MOJO_WAIT_FLAG_READABLE, 6); |
213 thread_1.Start(); | 213 thread1.Start(); |
214 | 214 |
215 base::PlatformThread::Sleep( | 215 base::PlatformThread::Sleep( |
216 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); | 216 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); |
217 | 217 |
218 // Should do nothing. | 218 // Should do nothing. |
219 waiter_list.AwakeWaitersForStateChange(0, | 219 waiter_list.AwakeWaitersForStateChange(0, |
220 MOJO_WAIT_FLAG_READABLE | | 220 MOJO_WAIT_FLAG_READABLE | |
221 MOJO_WAIT_FLAG_WRITABLE); | 221 MOJO_WAIT_FLAG_WRITABLE); |
222 | 222 |
223 test::SimpleWaiterThread thread_2(&result_2); | 223 test::SimpleWaiterThread thread2(&result2); |
224 waiter_list.AddWaiter(thread_2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 7); | 224 waiter_list.AddWaiter(thread2.waiter(), MOJO_WAIT_FLAG_WRITABLE, 7); |
225 thread_2.Start(); | 225 thread2.Start(); |
226 | 226 |
227 base::PlatformThread::Sleep( | 227 base::PlatformThread::Sleep( |
228 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); | 228 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); |
229 | 229 |
230 // Awake #1. | 230 // Awake #1. |
231 waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, | 231 waiter_list.AwakeWaitersForStateChange(MOJO_WAIT_FLAG_READABLE, |
232 MOJO_WAIT_FLAG_READABLE | | 232 MOJO_WAIT_FLAG_READABLE | |
233 MOJO_WAIT_FLAG_WRITABLE); | 233 MOJO_WAIT_FLAG_WRITABLE); |
234 waiter_list.RemoveWaiter(thread_1.waiter()); | 234 waiter_list.RemoveWaiter(thread1.waiter()); |
235 | 235 |
236 base::PlatformThread::Sleep( | 236 base::PlatformThread::Sleep( |
237 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); | 237 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); |
238 | 238 |
239 test::SimpleWaiterThread thread_3(&result_3); | 239 test::SimpleWaiterThread thread3(&result3); |
240 waiter_list.AddWaiter(thread_3.waiter(), MOJO_WAIT_FLAG_WRITABLE, 8); | 240 waiter_list.AddWaiter(thread3.waiter(), MOJO_WAIT_FLAG_WRITABLE, 8); |
241 thread_3.Start(); | 241 thread3.Start(); |
242 | 242 |
243 test::SimpleWaiterThread thread_4(&result_4); | 243 test::SimpleWaiterThread thread4(&result4); |
244 waiter_list.AddWaiter(thread_4.waiter(), MOJO_WAIT_FLAG_READABLE, 9); | 244 waiter_list.AddWaiter(thread4.waiter(), MOJO_WAIT_FLAG_READABLE, 9); |
245 thread_4.Start(); | 245 thread4.Start(); |
246 | 246 |
247 base::PlatformThread::Sleep( | 247 base::PlatformThread::Sleep( |
248 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); | 248 base::TimeDelta::FromMicroseconds(1 * kEpsilonMicros)); |
249 | 249 |
250 // Awake #2 and #3 for unsatisfiability. | 250 // Awake #2 and #3 for unsatisfiability. |
251 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); | 251 waiter_list.AwakeWaitersForStateChange(0, MOJO_WAIT_FLAG_READABLE); |
252 waiter_list.RemoveWaiter(thread_2.waiter()); | 252 waiter_list.RemoveWaiter(thread2.waiter()); |
253 waiter_list.RemoveWaiter(thread_3.waiter()); | 253 waiter_list.RemoveWaiter(thread3.waiter()); |
254 | 254 |
255 // Cancel #4. | 255 // Cancel #4. |
256 waiter_list.CancelAllWaiters(); | 256 waiter_list.CancelAllWaiters(); |
257 } // Join threads. | 257 } // Join threads. |
258 EXPECT_EQ(6, result_1); | 258 EXPECT_EQ(6, result1); |
259 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result_2); | 259 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result2); |
260 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result_3); | 260 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result3); |
261 EXPECT_EQ(MOJO_RESULT_CANCELLED, result_4); | 261 EXPECT_EQ(MOJO_RESULT_CANCELLED, result4); |
262 } | 262 } |
263 | 263 |
264 } // namespace | 264 } // namespace |
265 } // namespace system | 265 } // namespace system |
266 } // namespace mojo | 266 } // namespace mojo |
OLD | NEW |