add section on spawn_connected to tutorial and pull test into file
This commit is contained in:
parent
e0cf550527
commit
f3b867fd04
3 changed files with 67 additions and 19 deletions
18
src/test/run-pass/task-spawn-connected.rs
Normal file
18
src/test/run-pass/task-spawn-connected.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fn stringifier(from_par: comm::port<uint>,
|
||||
to_par: comm::chan<str>) {
|
||||
let value: uint;
|
||||
do {
|
||||
value = comm::recv(from_par);
|
||||
comm::send(to_par, uint::to_str(value, 10u));
|
||||
} while value != 0u;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let t = task::spawn_connected(stringifier);
|
||||
comm::send(t.to_child, 22u);
|
||||
assert comm::recv(t.from_child) == "22";
|
||||
comm::send(t.to_child, 23u);
|
||||
assert comm::recv(t.from_child) == "23";
|
||||
comm::send(t.to_child, 0u);
|
||||
assert comm::recv(t.from_child) == "0";
|
||||
}
|
||||
|
|
@ -57,22 +57,3 @@ fn spawn_polymorphic() {
|
|||
task::spawn {|| foo(true);};
|
||||
task::spawn {|| foo(42);};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_connected_stringifier() {
|
||||
fn stringifer(p: comm::port<uint>, ch: comm::chan<str>) {
|
||||
let u = 1u;
|
||||
while u != 0u {
|
||||
u = comm::recv(p);
|
||||
comm::send(ch, uint::to_str(u, 10u));
|
||||
}
|
||||
}
|
||||
|
||||
let ch = task::spawn_connected(stringifer);
|
||||
comm::send(ch.to_child, 22u);
|
||||
assert "22" == comm::recv(ch.from_child);
|
||||
comm::send(ch.to_child, 44u);
|
||||
assert "44" == comm::recv(ch.from_child);
|
||||
comm::send(ch.to_child, 0u);
|
||||
assert "0" == comm::recv(ch.from_child);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue