]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/pipe-select.rs
Imported Upstream version 0.7
[rustc.git] / src / test / run-pass / pipe-select.rs
CommitLineData
223e47cc
LB
1// xfail-fast
2
3// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
4// file at the top-level directory of this distribution and at
5// http://rust-lang.org/COPYRIGHT.
6//
7// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10// option. This file may not be copied, modified, or distributed
11// except according to those terms.
12
13// xfail-pretty
14// xfail-win32
15
970d7e83
LB
16extern mod extra;
17use extra::timer::sleep;
18use extra::uv;
223e47cc 19
970d7e83
LB
20use std::cell::Cell;
21use std::pipes::*;
22use std::pipes;
23use std::task;
223e47cc
LB
24
25proto! oneshot (
26 waiting:send {
27 signal -> !
28 }
29)
30
31proto! stream (
970d7e83 32 Stream:send<T:Send> {
223e47cc
LB
33 send(T) -> Stream<T>
34 }
35)
36
970d7e83
LB
37pub fn spawn_service<T:Send,Tb:Send>(
38 init: extern fn() -> (RecvPacketBuffered<T, Tb>,
39 SendPacketBuffered<T, Tb>),
40 service: ~fn(v: RecvPacketBuffered<T, Tb>))
41 -> SendPacketBuffered<T, Tb> {
42 let (server, client) = init();
43
44 // This is some nasty gymnastics required to safely move the pipe
45 // into a new task.
46 let server = Cell::new(server);
47 do task::spawn {
48 service(server.take());
49 }
50
51 client
52}
53
223e47cc
LB
54pub fn main() {
55 use oneshot::client::*;
56 use stream::client::*;
57
58 let iotask = &uv::global_loop::get();
970d7e83
LB
59
60 let c = spawn_service(stream::init, |p| {
223e47cc
LB
61 error!("waiting for pipes");
62 let stream::send(x, p) = recv(p);
63 error!("got pipes");
64 let (left, right) : (oneshot::server::waiting,
65 oneshot::server::waiting)
66 = x;
67 error!("selecting");
68 let (i, _, _) = select(~[left, right]);
69 error!("selected");
970d7e83 70 assert_eq!(i, 0);
223e47cc
LB
71
72 error!("waiting for pipes");
73 let stream::send(x, _) = recv(p);
74 error!("got pipes");
75 let (left, right) : (oneshot::server::waiting,
76 oneshot::server::waiting)
77 = x;
78 error!("selecting");
79 let (i, m, _) = select(~[left, right]);
80 error!("selected %?", i);
81 if m.is_some() {
970d7e83 82 assert_eq!(i, 1);
223e47cc
LB
83 }
84 });
85
970d7e83
LB
86 let (p1, c1) = oneshot::init();
87 let (p2, _c2) = oneshot::init();
223e47cc
LB
88
89 let c = send(c, (p1, p2));
970d7e83 90
223e47cc
LB
91 sleep(iotask, 100);
92
93 signal(c1);
94
970d7e83
LB
95 let (p1, _c1) = oneshot::init();
96 let (p2, c2) = oneshot::init();
223e47cc
LB
97
98 send(c, (p1, p2));
99
100 sleep(iotask, 100);
101
102 signal(c2);
103
104 test_select2();
105}
106
107fn test_select2() {
970d7e83
LB
108 let (ap, ac) = stream::init();
109 let (bp, bc) = stream::init();
223e47cc
LB
110
111 stream::client::send(ac, 42);
112
113 match pipes::select2(ap, bp) {
970d7e83
LB
114 Left(*) => { }
115 Right(*) => { fail!() }
223e47cc
LB
116 }
117
118 stream::client::send(bc, ~"abc");
119
120 error!("done with first select2");
121
970d7e83
LB
122 let (ap, ac) = stream::init();
123 let (bp, bc) = stream::init();
223e47cc
LB
124
125 stream::client::send(bc, ~"abc");
126
127 match pipes::select2(ap, bp) {
970d7e83
LB
128 Left(*) => { fail!() }
129 Right(*) => { }
223e47cc
LB
130 }
131
132 stream::client::send(ac, 42);
133}