Skip to content

Commit 928e558

Browse files
committed
libcore: Do less blocking in the test runner
1 parent 7a66303 commit 928e558

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

src/libstd/test.rs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export tr_ok;
1717
export tr_failed;
1818
export tr_ignored;
1919
export run_tests_console;
20-
export configure_test_task;
2120

2221
#[abi = "cdecl"]
2322
native mod rustrt {
@@ -192,6 +191,8 @@ enum testevent {
192191
te_result(test_desc, test_result);
193192
}
194193

194+
type monitor_msg = (test_desc, test_result);
195+
195196
fn run_tests(opts: test_opts, tests: [test_desc],
196197
callback: fn@(testevent)) {
197198

@@ -202,23 +203,27 @@ fn run_tests(opts: test_opts, tests: [test_desc],
202203
// many tests that run in other processes we would be making a big mess.
203204
let concurrency = get_concurrency();
204205
#debug("using %u test tasks", concurrency);
206+
205207
let total = vec::len(filtered_tests);
206208
let run_idx = 0u;
207209
let wait_idx = 0u;
208-
let futures = [];
210+
let done_idx = 0u;
209211

210-
while wait_idx < total {
211-
while vec::len(futures) < concurrency && run_idx < total {
212-
futures += [run_test(filtered_tests[run_idx])];
212+
let p = comm::port();
213+
let ch = comm::chan(p);
214+
215+
while done_idx < total {
216+
while wait_idx < concurrency && run_idx < total {
217+
run_test(vec::shift(filtered_tests), ch);
218+
wait_idx += 1u;
213219
run_idx += 1u;
214220
}
215221

216-
let future = futures[0];
217-
callback(te_wait(future.test));
218-
let result = future.wait();
219-
callback(te_result(future.test, result));
220-
futures = vec::slice(futures, 1u, vec::len(futures));
221-
wait_idx += 1u;
222+
let (test, result) = comm::recv(p);
223+
callback(te_wait(test));
224+
callback(te_result(test, result));
225+
wait_idx -= 1u;
226+
done_idx += 1u;
222227
}
223228
}
224229

@@ -280,35 +285,34 @@ fn filter_tests(opts: test_opts,
280285

281286
type test_future = {test: test_desc, wait: fn@() -> test_result};
282287

283-
fn run_test(test: test_desc) -> test_future {
288+
fn run_test(+test: test_desc, monitor_ch: comm::chan<monitor_msg>) {
284289
if test.ignore {
285-
ret {test: test, wait: fn@() -> test_result { tr_ignored }};
290+
comm::send(monitor_ch, (test, tr_ignored));
291+
ret;
286292
}
287293

288-
let test_task = test_to_task(test.fn);
289-
ret {test: test,
290-
wait: fn@() -> test_result {
291-
alt task::join(test_task) {
292-
task::tr_success {
293-
if test.should_fail { tr_failed }
294-
else { tr_ok }
295-
}
296-
task::tr_failure {
297-
if test.should_fail { tr_ok }
298-
else { tr_failed }
299-
}
300-
}
301-
}
294+
task::spawn {||
295+
296+
let testfn = test.fn;
297+
let test_task = task::spawn_joinable {||
298+
configure_test_task();
299+
testfn();
302300
};
301+
302+
let task_result = task::join(test_task);
303+
let test_result = calc_result(test, task_result == task::tr_success);
304+
comm::send(monitor_ch, (test, test_result));
305+
};
303306
}
304307

305-
// We need to run our tests in another task in order to trap test failures.
306-
// This function only works with functions that don't contain closures.
307-
fn test_to_task(&&f: test_fn) -> task::joinable_task {
308-
ret task::spawn_joinable(fn~[copy f]() {
309-
configure_test_task();
310-
f();
311-
});
308+
fn calc_result(test: test_desc, task_succeeded: bool) -> test_result {
309+
if task_succeeded {
310+
if test.should_fail { tr_failed }
311+
else { tr_ok }
312+
} else {
313+
if test.should_fail { tr_ok }
314+
else { tr_failed }
315+
}
312316
}
313317

314318
// Call from within a test task to make sure it's set up correctly
@@ -330,9 +334,11 @@ mod tests {
330334
ignore: true,
331335
should_fail: false
332336
};
333-
let future = run_test(desc);
334-
let result = future.wait();
335-
assert result != tr_ok;
337+
let p = comm::port();
338+
let ch = comm::chan(p);
339+
run_test(desc, ch);
340+
let (_, res) = comm::recv(p);
341+
assert res != tr_ok;
336342
}
337343

338344
#[test]
@@ -344,8 +350,11 @@ mod tests {
344350
ignore: true,
345351
should_fail: false
346352
};
347-
let res = run_test(desc).wait();
348-
assert (res == tr_ignored);
353+
let p = comm::port();
354+
let ch = comm::chan(p);
355+
run_test(desc, ch);
356+
let (_, res) = comm::recv(p);
357+
assert res == tr_ignored;
349358
}
350359

351360
#[test]
@@ -358,7 +367,10 @@ mod tests {
358367
ignore: false,
359368
should_fail: true
360369
};
361-
let res = run_test(desc).wait();
370+
let p = comm::port();
371+
let ch = comm::chan(p);
372+
run_test(desc, ch);
373+
let (_, res) = comm::recv(p);
362374
assert res == tr_ok;
363375
}
364376

@@ -371,7 +383,10 @@ mod tests {
371383
ignore: false,
372384
should_fail: true
373385
};
374-
let res = run_test(desc).wait();
386+
let p = comm::port();
387+
let ch = comm::chan(p);
388+
run_test(desc, ch);
389+
let (_, res) = comm::recv(p);
375390
assert res == tr_failed;
376391
}
377392

0 commit comments

Comments
 (0)