File: test_try_clone.rs

package info (click to toggle)
rust-serialport 4.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#![cfg(unix)]
extern crate serialport;

use serialport::{SerialPort, TTYPort};
use std::io::{Read, Write};

// Test that cloning a port works as expected
#[test]
fn test_try_clone() {
    let (master, mut slave) = TTYPort::pair().expect("Unable to create ptty pair");

    // Create the clone in an inner scope to test that dropping a clone doesn't close the original
    // port
    {
        let mut clone = master.try_clone().expect("Failed to clone");
        let bytes = [b'a', b'b', b'c', b'd', b'e', b'f'];
        clone.write_all(&bytes).unwrap();
        let mut buffer = [0; 6];
        slave.read_exact(&mut buffer).unwrap();
        assert_eq!(buffer, [b'a', b'b', b'c', b'd', b'e', b'f']);
    }

    // Second try to check that the serial device is not closed
    {
        let mut clone = master.try_clone().expect("Failed to clone");
        let bytes = [b'g', b'h', b'i', b'j', b'k', b'l'];
        clone.write_all(&bytes).unwrap();
        let mut buffer = [0; 6];
        slave.read_exact(&mut buffer).unwrap();
        assert_eq!(buffer, [b'g', b'h', b'i', b'j', b'k', b'l']);
    }
}

// Test moving a cloned port into a thread
#[test]
fn test_try_clone_move() {
    use std::thread;

    let (master, mut slave) = TTYPort::pair().expect("Unable to create ptty pair");

    let mut clone = master.try_clone().expect("Failed to clone the slave");
    let loopback = thread::spawn(move || {
        let bytes = [b'a', b'b', b'c', b'd', b'e', b'f'];
        clone.write_all(&bytes).unwrap();
    });

    let mut buffer = [0; 6];
    slave.read_exact(&mut buffer).unwrap();
    assert_eq!(buffer, [b'a', b'b', b'c', b'd', b'e', b'f']);

    // The thread should have already ended, but we'll make sure here anyways.
    loopback.join().unwrap();
}