Skip to content

Commit 4a34776

Browse files
committed
Run test functions in isolated tasks. Issue #428
Each test is run in its own task so that the failure can be trapped and the test runner can continue. The easiest way to get the test functions into tasks currently is by treating them as unsafe pointers.
1 parent 7d47553 commit 4a34776

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/lib/test.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,32 @@ fn filter_tests(&test_opts opts, &test_desc[] tests) -> test_desc[] {
212212

213213
fn run_test(&test_desc test) -> test_result {
214214
if (!test.ignore) {
215-
test.fn();
216-
ret tr_ok;
215+
if (run_test_fn_in_task(test.fn)) {
216+
ret tr_ok;
217+
} else {
218+
ret tr_failed;
219+
}
217220
} else {
218221
ret tr_ignored;
219222
}
220223
}
221224

225+
// We need to run our tests in another task in order to trap test failures.
226+
// But, at least currently, functions can't be used as spawn arguments so
227+
// we've got to treat our test functions as unsafe pointers.
228+
fn run_test_fn_in_task(&fn() f) -> bool {
229+
fn run_task(*mutable fn() fptr) {
230+
task::unsupervise();
231+
(*fptr)()
232+
}
233+
auto fptr = ptr::addr_of(f);
234+
auto test_task = spawn run_task(fptr);
235+
ret alt (task::join(test_task)) {
236+
task::tr_success { true }
237+
task::tr_failure { false }
238+
}
239+
}
240+
222241

223242
// Local Variables:
224243
// mode: rust;

0 commit comments

Comments
 (0)