Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: mojo/public/rust/tests/run_loop.rs

Issue 2244463002: Rust: Support delayed tasks in RunLoop (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Oops, forgot to fix build, should work now Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 //! Tests all functionality in the system package 5 //! Tests all functionality in the system package
6 //! 6 //!
7 //! Test failure is defined as the function returning via panicking 7 //! Test failure is defined as the function returning via panicking
8 //! and the result being caught in the test! macro. If a test function 8 //! and the result being caught in the test! macro. If a test function
9 //! returns without panicking, it is assumed to pass. 9 //! returns without panicking, it is assumed to pass.
10 10
11 #[macro_use] 11 #[macro_use]
12 extern crate mojo; 12 extern crate mojo;
13 13
14 #[macro_use] 14 #[macro_use]
15 mod util; 15 mod util;
16 16
17 use mojo::bindings::run_loop; 17 use mojo::bindings::run_loop;
18 use mojo::bindings::run_loop::{RunLoop, Token, Handler, WaitError}; 18 use mojo::bindings::run_loop::{RunLoop, Token, Handler, WaitError};
19 use mojo::system::MOJO_INDEFINITE; 19 use mojo::system::MOJO_INDEFINITE;
20 use mojo::system::message_pipe; 20 use mojo::system::message_pipe;
21 21
22 use std::cell::Cell;
23 use std::rc::Rc;
24
22 struct HandlerExpectReady {} 25 struct HandlerExpectReady {}
23 26
24 impl Handler for HandlerExpectReady { 27 impl Handler for HandlerExpectReady {
25 fn on_ready(&mut self, runloop: &mut RunLoop, token: Token) { 28 fn on_ready(&mut self, runloop: &mut RunLoop, token: Token) {
26 runloop.deregister(token); 29 runloop.deregister(token);
27 } 30 }
28 fn on_timeout(&mut self, _runloop: &mut RunLoop, _token: Token) { 31 fn on_timeout(&mut self, _runloop: &mut RunLoop, _token: Token) {
29 panic!("Timed-out when expected ready"); 32 panic!("Timed-out when expected ready");
30 } 33 }
31 fn on_error(&mut self, _runloop: &mut RunLoop, _token: Token, _error: WaitEr ror) { 34 fn on_error(&mut self, _runloop: &mut RunLoop, _token: Token, _error: WaitEr ror) {
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 runloop.quit(); 170 runloop.quit();
168 } 171 }
169 fn on_timeout(&mut self, runloop: &mut RunLoop, _token: Token) { 172 fn on_timeout(&mut self, runloop: &mut RunLoop, _token: Token) {
170 runloop.run(); 173 runloop.run();
171 } 174 }
172 fn on_error(&mut self, runloop: &mut RunLoop, _token: Token, _error: WaitErr or) { 175 fn on_error(&mut self, runloop: &mut RunLoop, _token: Token, _error: WaitErr or) {
173 runloop.quit(); 176 runloop.quit();
174 } 177 }
175 } 178 }
176 179
180 struct HandlerTasks {
181 count: Rc<Cell<u64>>,
182 }
183
184 impl Handler for HandlerTasks {
185 fn on_ready(&mut self, runloop: &mut RunLoop, token: Token) {
186 let r = self.count.clone();
187 let _ = runloop.post_task(move |_runloop| {
188 let val = (*r).get();
189 (*r).set(val+1);
190 }, 10);
191 if (*self.count).get() > 10 {
192 runloop.deregister(token);
193 }
194 }
195 fn on_timeout(&mut self, _runloop: &mut RunLoop, _token: Token) {
196 panic!("Timed-out when expected error");
197 }
198 fn on_error(&mut self, _runloop: &mut RunLoop, _token: Token, _error: WaitEr ror) {
199 panic!("Error when expected ready");
200 }
201 }
202
203 struct NestedTasks {
204 count: Rc<Cell<u64>>,
205 quitter: bool,
206 }
207
208 impl Handler for NestedTasks {
209 fn on_ready(&mut self, runloop: &mut RunLoop, token: Token) {
210 let r = self.count.clone();
211 let quit = self.quitter;
212 let _ = runloop.post_task(move |runloop| {
213 let r2 = r.clone();
214 let tk = token.clone();
215 if (*r).get() < 10 {
216 let _ = runloop.post_task(move |_runloop| {
217 let val = (*r2).get();
218 (*r2).set(val+1);
219 }, 0);
220 } else {
221 if quit {
222 runloop.quit();
223 } else {
224 runloop.deregister(tk);
225 }
226 }
227 }, 0);
228 }
229 fn on_timeout(&mut self, _runloop: &mut RunLoop, _token: Token) {
230 panic!("Timed-out when expected error");
231 }
232 fn on_error(&mut self, _runloop: &mut RunLoop, _token: Token, _error: WaitEr ror) {
233 panic!("Error when expected ready");
234 }
235 }
236
177 tests! { 237 tests! {
178 // Verifies that after adding and removing, we can run, exit and be 238 // Verifies that after adding and removing, we can run, exit and be
179 // left in a consistent state. 239 // left in a consistent state.
180 fn add_remove() { 240 fn add_remove() {
181 run_loop::with_current(|runloop| { 241 run_loop::with_current(|runloop| {
182 let (endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)). unwrap(); 242 let (endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)). unwrap();
183 let token0 = runloop.register(&endpt0, signals!(Signals::Writable), 0, HandlerExpectReady {}); 243 let token0 = runloop.register(&endpt0, signals!(Signals::Writable), 0, HandlerExpectReady {});
184 let token1 = runloop.register(&endpt1, signals!(Signals::Writable), 0, HandlerExpectReady {}); 244 let token1 = runloop.register(&endpt1, signals!(Signals::Writable), 0, HandlerExpectReady {});
185 runloop.deregister(token1); 245 runloop.deregister(token1);
186 runloop.deregister(token0); 246 runloop.deregister(token0);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 332
273 // Tests to make sure nesting with the SAME runloop fails. 333 // Tests to make sure nesting with the SAME runloop fails.
274 #[should_panic] 334 #[should_panic]
275 fn bad_nesting() { 335 fn bad_nesting() {
276 let (_endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)).unw rap(); 336 let (_endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)).unw rap();
277 run_loop::with_current(|runloop| { 337 run_loop::with_current(|runloop| {
278 let _ = runloop.register(&endpt1, signals!(Signals::Readable), 0, Ha ndlerBadNesting {}); 338 let _ = runloop.register(&endpt1, signals!(Signals::Readable), 0, Ha ndlerBadNesting {});
279 runloop.run(); 339 runloop.run();
280 }); 340 });
281 } 341 }
342
343 // Tests adding a simple task that adds a handler.
344 fn simple_task() {
345 run_loop::with_current(|runloop| {
346 let _ = runloop.post_task(|runloop| {
347 let (_, endpt1) = message_pipe::create(mpflags!(Create::None)).u nwrap();
348 let _ = runloop.register(&endpt1, signals!(Signals::Readable), 0 , HandlerExpectError {});
349 }, 0);
350 runloop.run();
351 });
352 }
353
354 // Tests using a handler that adds a bunch of tasks.
355 fn handler_tasks() {
356 let (_endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)).unw rap();
357 let r = Rc::new(Cell::new(0));
358 run_loop::with_current(|runloop| {
359 let _ = runloop.register(&endpt1, signals!(Signals::Writable), 0, Ha ndlerTasks { count: r.clone() });
360 runloop.run();
361 assert!((*r).get() >= 11);
362 });
363 }
364
365 // Tests using a handler that adds a bunch of tasks.
366 fn nested_tasks() {
367 let (_endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)).unw rap();
368 let r = Rc::new(Cell::new(0));
369 run_loop::with_current(|runloop| {
370 let _ = runloop.register(&endpt1, signals!(Signals::Writable), 0, Ne stedTasks { count: r.clone(), quitter: false });
371 runloop.run();
372 assert!((*r).get() >= 10);
373 });
374 }
375
376 // Tests using a handler that adds a bunch of tasks.
377 fn nested_tasks_quit() {
378 let (_endpt0, endpt1) = message_pipe::create(mpflags!(Create::None)).unw rap();
379 let r = Rc::new(Cell::new(0));
380 run_loop::with_current(|runloop| {
381 let _ = runloop.register(&endpt1, signals!(Signals::Writable), 0, Ne stedTasks { count: r.clone(), quitter: true });
382 runloop.run();
383 assert!((*r).get() >= 10);
384 });
385 }
282 } 386 }
OLDNEW
« mojo/public/rust/src/bindings/run_loop.rs ('K') | « mojo/public/rust/src/bindings/run_loop.rs ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698