PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/run-pass/task-comm.rs

http://github.com/jruderman/rust
Rust | 150 lines | 118 code | 31 blank | 1 comment | 10 complexity | 7670b83da9a06f32a35f43014ba733e1 MD5 | raw file
  1use std;
  2
  3import task;
  4import task::task;
  5import comm;
  6import comm::chan;
  7import comm::port;
  8import comm::send;
  9import comm::recv;
 10
 11fn main() {
 12    test00();
 13    // test01();
 14    test02();
 15    test04();
 16    test05();
 17    test06();
 18}
 19
 20fn test00_start(ch: chan<int>, message: int, count: int) {
 21    debug!{"Starting test00_start"};
 22    let mut i: int = 0;
 23    while i < count {
 24        debug!{"Sending Message"};
 25        send(ch, message + 0);
 26        i = i + 1;
 27    }
 28    debug!{"Ending test00_start"};
 29}
 30
 31fn test00() {
 32    let number_of_tasks: int = 1;
 33    let number_of_messages: int = 4;
 34    debug!{"Creating tasks"};
 35
 36    let po = port();
 37    let ch = chan(po);
 38
 39    let mut i: int = 0;
 40
 41    let mut results = ~[];
 42    while i < number_of_tasks {
 43        i = i + 1;
 44        do task::task().future_result(|-r| {
 45            results += ~[r];
 46        }).spawn |copy i| {
 47            test00_start(ch, i, number_of_messages);
 48        }
 49    }
 50    let mut sum: int = 0;
 51    for results.each |r| {
 52        i = 0;
 53        while i < number_of_messages { sum += recv(po); i = i + 1; }
 54    }
 55
 56    for results.each |r| { future::get(r); }
 57
 58    debug!{"Completed: Final number is: "};
 59    assert (sum ==
 60                number_of_messages *
 61                    (number_of_tasks * number_of_tasks + number_of_tasks) /
 62                    2);
 63}
 64
 65fn test01() {
 66    let p = port();
 67    debug!{"Reading from a port that is never written to."};
 68    let value: int = recv(p);
 69    log(debug, value);
 70}
 71
 72fn test02() {
 73    let p = port();
 74    let c = chan(p);
 75    debug!{"Writing to a local task channel."};
 76    send(c, 42);
 77    debug!{"Reading from a local task port."};
 78    let value: int = recv(p);
 79    log(debug, value);
 80}
 81
 82fn test04_start() {
 83    debug!{"Started task"};
 84    let mut i: int = 1024 * 1024;
 85    while i > 0 { i = i - 1; }
 86    debug!{"Finished task"};
 87}
 88
 89fn test04() {
 90    debug!{"Spawning lots of tasks."};
 91    let mut i: int = 4;
 92    while i > 0 { i = i - 1; task::spawn(|| test04_start() ); }
 93    debug!{"Finishing up."};
 94}
 95
 96fn test05_start(ch: chan<int>) {
 97    send(ch, 10);
 98    send(ch, 20);
 99    send(ch, 30);
100    send(ch, 30);
101    send(ch, 30);
102}
103
104fn test05() {
105    let po = comm::port();
106    let ch = chan(po);
107    task::spawn(|| test05_start(ch) );
108    let mut value: int;
109    value = recv(po);
110    value = recv(po);
111    value = recv(po);
112    log(debug, value);
113}
114
115fn test06_start(&&task_number: int) {
116    debug!{"Started task."};
117    let mut i: int = 0;
118    while i < 1000000 { i = i + 1; }
119    debug!{"Finished task."};
120}
121
122fn test06() {
123    let number_of_tasks: int = 4;
124    debug!{"Creating tasks"};
125
126    let mut i: int = 0;
127
128    let mut results = ~[];
129    while i < number_of_tasks {
130        i = i + 1;
131        do task::task().future_result(|-r| {
132            results += ~[r];
133        }).spawn |copy i| {
134            test06_start(i);
135        };
136    }
137
138
139    for results.each |r| { future::get(r); }
140}
141
142
143
144
145
146
147
148
149
150