rust/src/test/run-pass/pipe-select.rs

133 lines
3.0 KiB
Rust
Raw Normal View History

// xfail-fast
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2012-07-06 01:14:27 -05:00
// xfail-pretty
// xfail-win32
2012-07-06 01:14:27 -05:00
extern mod std;
2012-09-05 14:32:05 -05:00
use std::timer::sleep;
use std::uv;
use core::cell::Cell;
2013-02-02 05:10:12 -06:00
use core::pipes;
use core::pipes::*;
proto! oneshot (
2012-07-06 01:14:27 -05:00
waiting:send {
signal -> !
}
)
proto! stream (
2012-12-11 15:50:04 -06:00
Stream:send<T:Owned> {
send(T) -> Stream<T>
}
)
pub fn spawn_service<T:Owned,Tb:Owned>(
init: extern fn() -> (SendPacketBuffered<T, Tb>,
RecvPacketBuffered<T, Tb>),
service: ~fn(v: RecvPacketBuffered<T, Tb>))
-> SendPacketBuffered<T, Tb> {
let (client, server) = init();
// This is some nasty gymnastics required to safely move the pipe
// into a new task.
let server = Cell(server);
do task::spawn {
service(server.take());
}
client
}
pub fn main() {
use oneshot::client::*;
use stream::client::*;
let iotask = &uv::global_loop::get();
let c = spawn_service(stream::init, |p| {
2012-08-22 19:24:52 -05:00
error!("waiting for pipes");
2013-02-15 04:44:18 -06:00
let stream::send(x, p) = recv(p);
2012-08-22 19:24:52 -05:00
error!("got pipes");
let (left, right) : (oneshot::server::waiting,
oneshot::server::waiting)
2013-02-15 04:44:18 -06:00
= x;
2012-08-22 19:24:52 -05:00
error!("selecting");
2013-02-15 04:44:18 -06:00
let (i, _, _) = select(~[left, right]);
2012-08-22 19:24:52 -05:00
error!("selected");
2013-03-28 20:39:09 -05:00
assert!(i == 0);
2012-08-22 19:24:52 -05:00
error!("waiting for pipes");
2013-02-15 04:44:18 -06:00
let stream::send(x, _) = recv(p);
2012-08-22 19:24:52 -05:00
error!("got pipes");
let (left, right) : (oneshot::server::waiting,
oneshot::server::waiting)
2013-02-15 04:44:18 -06:00
= x;
2012-08-22 19:24:52 -05:00
error!("selecting");
2013-02-15 04:44:18 -06:00
let (i, m, _) = select(~[left, right]);
2012-08-22 19:24:52 -05:00
error!("selected %?", i);
if m.is_some() {
2013-03-28 20:39:09 -05:00
assert!(i == 1);
}
});
let (c1, p1) = oneshot::init();
2012-07-10 18:46:16 -05:00
let (_c2, p2) = oneshot::init();
2013-02-15 04:44:18 -06:00
let c = send(c, (p1, p2));
sleep(iotask, 100);
2013-02-15 04:44:18 -06:00
signal(c1);
2012-07-10 18:46:16 -05:00
let (_c1, p1) = oneshot::init();
let (c2, p2) = oneshot::init();
2013-02-15 04:44:18 -06:00
send(c, (p1, p2));
sleep(iotask, 100);
2013-02-15 04:44:18 -06:00
signal(c2);
2012-07-09 15:53:55 -05:00
test_select2();
}
fn test_select2() {
let (ac, ap) = stream::init();
let (bc, bp) = stream::init();
2013-02-15 04:44:18 -06:00
stream::client::send(ac, 42);
2012-07-09 15:53:55 -05:00
2013-02-15 04:44:18 -06:00
match pipes::select2(ap, bp) {
2012-08-14 18:54:13 -05:00
either::Left(*) => { }
either::Right(*) => { fail!() }
2012-07-09 15:53:55 -05:00
}
2013-02-15 04:44:18 -06:00
stream::client::send(bc, ~"abc");
2012-07-09 15:53:55 -05:00
2012-08-22 19:24:52 -05:00
error!("done with first select2");
2012-07-09 15:53:55 -05:00
let (ac, ap) = stream::init();
let (bc, bp) = stream::init();
2013-02-15 04:44:18 -06:00
stream::client::send(bc, ~"abc");
2012-07-09 15:53:55 -05:00
2013-02-15 04:44:18 -06:00
match pipes::select2(ap, bp) {
either::Left(*) => { fail!() }
2012-08-14 18:54:13 -05:00
either::Right(*) => { }
2012-07-09 15:53:55 -05:00
}
2013-02-15 04:44:18 -06:00
stream::client::send(ac, 42);
2012-07-09 15:53:55 -05:00
}