rust/tests/ui/process/core-run-destroy.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.2 KiB
Rust
Raw Normal View History

// run-pass
#![allow(unused_must_use)]
#![allow(stable_features)]
#![allow(deprecated)]
#![allow(unused_imports)]
// compile-flags:--test
// ignore-emscripten no processes
2019-04-24 16:26:33 +00:00
// ignore-sgx no processes
// ignore-vxworks no 'cat' and 'sleep'
// ignore-fuchsia no 'cat'
// N.B., these tests kill child processes. Valgrind sees these children as leaking
// memory, which makes for some *confusing* logs. That's why these are here
// instead of in std.
std: Depend directly on crates.io crates Ever since we added a Cargo-based build system for the compiler the standard library has always been a little special, it's never been able to depend on crates.io crates for runtime dependencies. This has been a result of various limitations, namely that Cargo doesn't understand that crates from crates.io depend on libcore, so Cargo tries to build crates before libcore is finished. I had an idea this afternoon, however, which lifts the strategy from #52919 to directly depend on crates.io crates from the standard library. After all is said and done this removes a whopping three submodules that we need to manage! The basic idea here is that for any crate `std` depends on it adds an *optional* dependency on an empty crate on crates.io, in this case named `rustc-std-workspace-core`. This crate is overridden via `[patch]` in this repository to point to a local crate we write, and *that* has a `path` dependency on libcore. Note that all `no_std` crates also depend on `compiler_builtins`, but if we're not using submodules we can publish `compiler_builtins` to crates.io and all crates can depend on it anyway! The basic strategy then looks like: * The standard library (or some transitive dep) decides to depend on a crate `foo`. * The standard library adds ```toml [dependencies] foo = { version = "0.1", features = ['rustc-dep-of-std'] } ``` * The crate `foo` has an optional dependency on `rustc-std-workspace-core` * The crate `foo` has an optional dependency on `compiler_builtins` * The crate `foo` has a feature `rustc-dep-of-std` which activates these crates and any other necessary infrastructure in the crate. A sample commit for `dlmalloc` [turns out to be quite simple][commit]. After that all `no_std` crates should largely build "as is" and still be publishable on crates.io! Notably they should be able to continue to use stable Rust if necessary, since the `rename-dependency` feature of Cargo is soon stabilizing. As a proof of concept, this commit removes the `dlmalloc`, `libcompiler_builtins`, and `libc` submodules from this repository. Long thorns in our side these are now gone for good and we can directly depend on crates.io! It's hoped that in the long term we can bring in other crates as necessary, but for now this is largely intended to simply make it easier to manage these crates and remove submodules. This should be a transparent non-breaking change for all users, but one possible stickler is that this almost for sure breaks out-of-tree `std`-building tools like `xargo` and `cargo-xbuild`. I think it should be relatively easy to get them working, however, as all that's needed is an entry in the `[patch]` section used to build the standard library. Hopefully we can work with these tools to solve this problem! [commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-20 05:52:50 +00:00
#![feature(rustc_private, duration)]
2014-02-26 17:58:41 +00:00
extern crate libc;
2015-04-10 20:51:53 +00:00
use std::process::{self, Command, Child, Output, Stdio};
2014-10-01 04:09:29 +00:00
use std::str;
std: Second pass stabilization for `comm` This commit is a second pass stabilization for the `std::comm` module, performing the following actions: * The entire `std::comm` module was moved under `std::sync::mpsc`. This movement reflects that channels are just yet another synchronization primitive, and they don't necessarily deserve a special place outside of the other concurrency primitives that the standard library offers. * The `send` and `recv` methods have all been removed. * The `send_opt` and `recv_opt` methods have been renamed to `send` and `recv`. This means that all send/receive operations return a `Result` now indicating whether the operation was successful or not. * The error type of `send` is now a `SendError` to implement a custom error message and allow for `unwrap()`. The error type contains an `into_inner` method to extract the value. * The error type of `recv` is now `RecvError` for the same reasons as `send`. * The `TryRecvError` and `TrySendError` types have had public reexports removed of their variants and the variant names have been tweaked with enum namespacing rules. * The `Messages` iterator is renamed to `Iter` This functionality is now all `#[stable]`: * `Sender` * `SyncSender` * `Receiver` * `std::sync::mpsc` * `channel` * `sync_channel` * `Iter` * `Sender::send` * `Sender::clone` * `SyncSender::send` * `SyncSender::try_send` * `SyncSender::clone` * `Receiver::recv` * `Receiver::try_recv` * `Receiver::iter` * `SendError` * `RecvError` * `TrySendError::{mod, Full, Disconnected}` * `TryRecvError::{mod, Empty, Disconnected}` * `SendError::into_inner` * `TrySendError::into_inner` This is a breaking change due to the modification of where this module is located, as well as the changing of the semantics of `send` and `recv`. Most programs just need to rename imports of `std::comm` to `std::sync::mpsc` and add calls to `unwrap` after a send or a receive operation. [breaking-change]
2014-12-23 19:53:35 +00:00
use std::sync::mpsc::channel;
use std::thread;
2015-04-10 18:12:43 +00:00
use std::time::Duration;
2014-10-01 04:09:29 +00:00
2015-04-10 18:12:43 +00:00
macro_rules! t {
($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
}
#[test]
2014-10-01 04:09:29 +00:00
fn test_destroy_once() {
let mut p = sleeper();
t!(p.kill());
2014-10-01 04:09:29 +00:00
}
#[cfg(unix)]
2015-04-10 18:12:43 +00:00
pub fn sleeper() -> Child {
t!(Command::new("sleep").arg("1000").spawn())
}
#[cfg(windows)]
2015-04-10 18:12:43 +00:00
pub fn sleeper() -> Child {
// There's a `timeout` command on windows, but it doesn't like having
// its output piped, so instead just ping ourselves a few times with
2014-08-01 23:42:13 +00:00
// gaps in between so we're sure this process is alive for awhile
t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
}
#[test]
2014-10-01 04:09:29 +00:00
fn test_destroy_twice() {
let mut p = sleeper();
2015-04-10 18:12:43 +00:00
t!(p.kill()); // this shouldn't crash...
let _ = p.kill(); // ...and nor should this (and nor should the destructor)
2014-10-01 04:09:29 +00:00
}
2015-04-10 18:12:43 +00:00
#[test]
fn test_destroy_actually_kills() {
let cmd = if cfg!(windows) {
"cmd"
} else if cfg!(target_os = "android") {
"/system/bin/cat"
} else {
"cat"
};
// this process will stay alive indefinitely trying to read from stdin
let mut p = t!(Command::new(cmd)
.stdin(Stdio::piped())
.spawn());
t!(p.kill());
// Don't let this test time out, this should be quick
2015-04-10 18:12:43 +00:00
let (tx, rx) = channel();
thread::spawn(move|| {
2015-04-10 18:12:43 +00:00
thread::sleep_ms(1000);
if rx.try_recv().is_err() {
process::exit(1);
}
2015-01-06 05:59:45 +00:00
});
let code = t!(p.wait()).code();
2015-04-10 20:51:53 +00:00
if cfg!(windows) {
assert!(code.is_some());
} else {
assert!(code.is_none());
}
2015-04-10 18:12:43 +00:00
tx.send(());
2014-10-01 04:09:29 +00:00
}