std: Make timer tests more reliable under valgrind

This commit is contained in:
Brian Anderson 2012-05-21 17:29:00 -07:00
parent 248e439638
commit 6d8cffa991

View File

@ -182,40 +182,58 @@ mod test {
} }
} }
// Because valgrind serializes multithreaded programs it can
// make timing-sensitive tests fail in wierd ways. In these
// next test we run them many times and expect them to pass
// the majority of tries.
#[test] #[test]
fn test_gl_timer_recv_timeout_before_time_passes() { fn test_gl_timer_recv_timeout_before_time_passes() {
let expected = rand::rng().gen_str(16u); let times = 100;
let test_po = comm::port::<str>(); let mut successes = 0;
let test_ch = comm::chan(test_po); let mut failures = 0;
task::spawn() {|| iter::repeat(times as uint) {||
delayed_send(1u, test_ch, expected); task::yield();
};
let actual = alt recv_timeout(1000u, test_po) { let expected = rand::rng().gen_str(16u);
some(val) { val } let test_po = comm::port::<str>();
_ { fail "test_timer_recv_timeout_before_time_passes:"+ let test_ch = comm::chan(test_po);
" didn't receive result before timeout"; }
}; task::spawn() {||
assert actual == expected; delayed_send(1u, test_ch, expected);
};
alt recv_timeout(10u, test_po) {
some(val) { assert val == expected; successes += 1; }
_ { failures += 1; }
};
}
assert successes > times / 2;
} }
#[test] #[test]
fn test_gl_timer_recv_timeout_after_time_passes() { fn test_gl_timer_recv_timeout_after_time_passes() {
let expected = rand::rng().gen_str(16u); let times = 100;
let fail_msg = rand::rng().gen_str(16u); let mut successes = 0;
let test_po = comm::port::<str>(); let mut failures = 0;
let test_ch = comm::chan(test_po);
task::spawn() {|| iter::repeat(times as uint) {||
delayed_send(1000u, test_ch, expected); let expected = rand::rng().gen_str(16u);
}; let test_po = comm::port::<str>();
let test_ch = comm::chan(test_po);
let actual = alt recv_timeout(1u, test_po) { task::spawn() {||
none { fail_msg } delayed_send(1000u, test_ch, expected);
_ { fail "test_timer_recv_timeout_before_time_passes:"+ };
" didn't receive result before timeout"; }
}; let actual = alt recv_timeout(1u, test_po) {
assert actual == fail_msg; none { successes += 1; }
_ { failures += 1; }
};
}
assert successes > times / 2;
} }
} }