Skip to content

Commit 816e46d

Browse files
committed
Fixing some tests, adding some pipes
This adds constructors to pipe streams in the new runtime to take ownership of file descriptors, and also fixes a few tests relating to the std::run changes (new errors are raised on io_error and one test is xfail'd).
1 parent 262b958 commit 816e46d

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/libstd/rt/io/pipe.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,45 @@
1616
use prelude::*;
1717
use super::{Reader, Writer};
1818
use rt::io::{io_error, EndOfFile};
19-
use rt::rtio::RtioPipe;
19+
use rt::io::native::file;
20+
use rt::rtio::{RtioPipe, with_local_io};
2021

2122
pub struct PipeStream {
2223
priv obj: ~RtioPipe,
2324
}
2425

2526
impl PipeStream {
27+
/// Consumes a file descriptor to return a pipe stream that will have
28+
/// synchronous, but non-blocking reads/writes. This is useful if the file
29+
/// descriptor is acquired via means other than the standard methods.
30+
///
31+
/// This operation consumes ownership of the file descriptor and it will be
32+
/// closed once the object is deallocated.
33+
///
34+
/// # Example
35+
///
36+
/// use std::libc;
37+
/// use std::rt::io::pipe;
38+
///
39+
/// let mut pipe = PipeStream::open(libc::STDERR_FILENO);
40+
/// pipe.write(bytes!("Hello, stderr!"));
41+
///
42+
/// # Failure
43+
///
44+
/// If the pipe cannot be created, an error will be raised on the
45+
/// `io_error` condition.
46+
pub fn open(fd: file::fd_t) -> Option<PipeStream> {
47+
do with_local_io |io| {
48+
match io.pipe_open(fd) {
49+
Ok(obj) => Some(PipeStream { obj: obj }),
50+
Err(e) => {
51+
io_error::cond.raise(e);
52+
None
53+
}
54+
}
55+
}
56+
}
57+
2658
pub fn new(inner: ~RtioPipe) -> PipeStream {
2759
PipeStream { obj: inner }
2860
}

src/libstd/run.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ mod tests {
322322
use path::Path;
323323
use run;
324324
use str;
325+
use task::spawn;
325326
use unstable::running_on_valgrind;
326327
use rt::io::native::file;
327328
use rt::io::{Writer, Reader};
@@ -394,6 +395,7 @@ mod tests {
394395
}
395396
396397
#[test]
398+
#[ignore] // FIXME(#10016) cat never sees stdin close
397399
fn test_pipes() {
398400
399401
let pipe_in = os::pipe();
@@ -412,13 +414,14 @@ mod tests {
412414
os::close(pipe_out.out);
413415
os::close(pipe_err.out);
414416
415-
let expected = ~"test";
416-
writeclose(pipe_in.out, expected);
417+
do spawn {
418+
writeclose(pipe_in.out, ~"test");
419+
}
417420
let actual = readclose(pipe_out.input);
418421
readclose(pipe_err.input);
419422
proc.finish();
420423
421-
assert_eq!(expected, actual);
424+
assert_eq!(~"test", actual);
422425
}
423426
424427
fn writeclose(fd: c_int, s: &str) {

src/test/run-pass/core-run-destroy.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use std::libc;
1919
use std::run;
2020
use std::str;
21+
use std::rt::io;
2122

2223
#[test]
2324
fn test_destroy_once() {
@@ -29,7 +30,9 @@ fn test_destroy_once() {
2930
fn test_destroy_twice() {
3031
let mut p = run::Process::new("echo", [], run::ProcessOptions::new());
3132
p.destroy(); // this shouldnt crash...
32-
p.destroy(); // ...and nor should this (and nor should the destructor)
33+
do io::io_error::cond.trap(|_| {}).inside {
34+
p.destroy(); // ...and nor should this (and nor should the destructor)
35+
}
3336
}
3437

3538
fn test_destroy_actually_kills(force: bool) {

0 commit comments

Comments
 (0)