Skip to content

Commit 5aee2ee

Browse files
olsonjefferybrson
authored andcommitted
---
yaml --- r: 15588 b: refs/heads/try c: 707391e h: refs/heads/master v: v3
1 parent 61841e4 commit 5aee2ee

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 7ac8c3081c5d4d7b1c79942e426770c1c3e1c0b3
5+
refs/heads/try: 707391edbce75b717779d074bf5f5e4b57901e98
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libstd/timer.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Utilities that leverage libuv's `uv_timer_*` API
33
"];
44

55
import uv = uv;
6-
export delayed_send, sleep;
6+
export delayed_send, sleep, recv_timeout;
77

88
#[doc = "
99
Wait for timeout period then send provided value over a channel
@@ -16,9 +16,9 @@ for *at least* that period of time.
1616
1717
# Arguments
1818
19-
msecs - a timeout period, in milliseconds, to wait
20-
ch - a channel of type T to send a `val` on
21-
val - a value of type T to send over the provided `ch`
19+
* msecs - a timeout period, in milliseconds, to wait
20+
* ch - a channel of type T to send a `val` on
21+
* val - a value of type T to send over the provided `ch`
2222
"]
2323
fn delayed_send<T: send>(msecs: uint, ch: comm::chan<T>, val: T) {
2424
task::spawn() {||
@@ -80,6 +80,39 @@ fn sleep(msecs: uint) {
8080
comm::recv(exit_po);
8181
}
8282

83+
#[doc = "
84+
Receive on a port for (up to) a specified time, then return an `option<T>`
85+
86+
This call will block to receive on the provided port for up to the specified
87+
timeout. Depending on whether the provided port receives in that time period,
88+
`recv_timeout` will return an `option<T>` representing the result.
89+
90+
# Arguments
91+
92+
* msecs - an mount of time, in milliseconds, to wait to receive
93+
* wait_port - a `comm::port<T>` to receive on
94+
95+
# Returns
96+
97+
An `option<T>` representing the outcome of the call. If the call `recv`'d on
98+
the provided port in the allotted timeout period, then the result will be a
99+
`some(T)`. If not, then `none` will be returned.
100+
"]
101+
fn recv_timeout<T: send>(msecs: uint, wait_po: comm::port<T>) -> option<T> {
102+
let timeout_po = comm::port::<()>();
103+
let timeout_ch = comm::chan(timeout_po);
104+
delayed_send(msecs, timeout_ch, ());
105+
either::either(
106+
{|left_val|
107+
log(debug, #fmt("recv_time .. left_val %?",
108+
left_val));
109+
none
110+
}, {|right_val|
111+
some(right_val)
112+
}, comm::select2(timeout_po, wait_po)
113+
)
114+
}
115+
83116
// INTERNAL API
84117
crust fn delayed_send_cb(handle: *uv::ll::uv_timer_t,
85118
status: libc::c_int) unsafe {
@@ -108,6 +141,43 @@ crust fn delayed_send_close_cb(handle: *uv::ll::uv_timer_t) unsafe {
108141
mod test {
109142
#[test]
110143
fn test_timer_simple_sleep_test() {
111-
sleep(2000u);
144+
sleep(1u);
145+
}
146+
147+
#[test]
148+
fn test_timer_recv_timeout_before_time_passes() {
149+
let expected = rand::rng().gen_str(16u);
150+
let test_po = comm::port::<str>();
151+
let test_ch = comm::chan(test_po);
152+
153+
task::spawn() {||
154+
delayed_send(1u, test_ch, expected);
155+
};
156+
157+
let actual = alt recv_timeout(1000u, test_po) {
158+
some(val) { val }
159+
_ { fail "test_timer_recv_timeout_before_time_passes:"+
160+
" didn't receive result before timeout"; }
161+
};
162+
assert actual == expected;
163+
}
164+
165+
#[test]
166+
fn test_timer_recv_timeout_after_time_passes() {
167+
let expected = rand::rng().gen_str(16u);
168+
let fail_msg = rand::rng().gen_str(16u);
169+
let test_po = comm::port::<str>();
170+
let test_ch = comm::chan(test_po);
171+
172+
task::spawn() {||
173+
delayed_send(1000u, test_ch, expected);
174+
};
175+
176+
let actual = alt recv_timeout(1u, test_po) {
177+
none { fail_msg }
178+
_ { fail "test_timer_recv_timeout_before_time_passes:"+
179+
" didn't receive result before timeout"; }
180+
};
181+
assert actual == fail_msg;
112182
}
113183
}

branches/try/src/libstd/uv_ll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ mod test {
841841
// we have data
842842
log(debug, #fmt("CLIENT read: data! nread: %d", nread));
843843
read_stop(stream);
844-
let client_data =
844+
let client_data =
845845
get_data_for_uv_handle(stream as *libc::c_void)
846846
as *request_wrapper;
847847
let buf_base = get_base_from_buf(buf);

0 commit comments

Comments
 (0)