File: basic.rs

package info (click to toggle)
rust-interprocess 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,016 kB
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 952 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use {
    crate::{tests::util::*, unnamed_pipe::pipe},
    std::{
        io::{BufRead, BufReader, Write},
        sync::mpsc,
        thread,
    },
};
static MSG: &str = "Message from sender to receiver\n";

pub(super) fn main() -> TestResult {
    let (mut tx, rx) = pipe().opname("pipe creation")?;

    thread::scope(|scope| {
        let (notify, wait) = mpsc::channel();

        scope.spawn(move || {
            tx.write_all(MSG.as_bytes()).opname("send")?;
            drop(tx);
            // Test buffer retention on drop
            notify.send(()).opname("notify")?;
            TestResult::Ok(())
        });

        wait.recv().opname("wait for drop")?;
        // Sender is guaranteed to be in limbo by this point (Windows only)

        let mut buf = String::with_capacity(MSG.len());
        let mut rx = BufReader::new(rx);

        rx.read_line(&mut buf).opname("receive")?;
        ensure_eq!(buf, MSG);

        Ok(())
    })
}