use crate::common::*;
#[test]
fn core_error_kinds_and_old_texts() {
let m = SerialManager::new();
let e = m.write_core("nope".into(), b"x".to_vec()).unwrap_err();
assert_eq!(e.kind(), ErrorKind::NotFound);
let old = m.write("nope".into(), "x".into()).unwrap_err();
assert_eq!(Error::from(e).to_string(), old.to_string());
let missing = "/dev/serialplugin-does-not-exist".to_string();
let e = m
.open_core(
missing.clone(),
9600,
None,
None,
None,
None,
None,
Default::default(),
)
.unwrap_err();
assert_eq!(e.kind(), ErrorKind::NotFound, "{e}");
let old = m
.open(missing, 9600, None, None, None, None, None)
.unwrap_err();
assert_eq!(e.to_string(), old.to_string());
assert!(matches!(Error::from(e), Error::String(_)));
let fake = Fake::new();
let m = manager_with("kinds", fake.port(true));
for (io_kind, kind) in [
(io::ErrorKind::BrokenPipe, ErrorKind::Disconnected),
(io::ErrorKind::PermissionDenied, ErrorKind::PermissionDenied),
] {
fake.fail_writes(io_kind);
let e = m.write_core("kinds".into(), b"x".to_vec()).unwrap_err();
assert_eq!(e.kind(), kind, "{e}");
let src = std::error::Error::source(&e)
.and_then(|s| s.downcast_ref::<io::Error>())
.expect("io source");
assert_eq!(src.kind(), io_kind);
let old = m.write_binary("kinds".into(), b"x".to_vec()).unwrap_err();
assert_eq!(e.to_string(), old.to_string());
}
*fake.0.write_error.lock().unwrap() = None;
let opts = ExchangeOptions {
timeout_ms: Some(100),
rx_prepare: Some(RxPrepareMode::None),
..Default::default()
};
let e = m
.exchange_core("kinds".into(), b"AT\r".to_vec(), opts)
.unwrap_err();
assert_eq!(e.kind(), ErrorKind::Timeout, "{e}");
assert_eq!(e.to_string(), "exchange timed out after 100 ms");
let (wrote, written) = mpsc::channel();
let wrote = Mutex::new(wrote);
fake.respond(move |_| {
let _ = wrote.lock().unwrap().send(());
Vec::new()
});
let bg = m.clone();
let t = thread::spawn(move || {
bg.exchange_core(
"kinds".into(),
b"AT\r".to_vec(),
ExchangeOptions {
timeout_ms: Some(5000),
rx_prepare: Some(RxPrepareMode::None),
..Default::default()
},
)
});
written
.recv_timeout(Duration::from_secs(3))
.expect("exchange wrote its command");
m.cancel_exchange("kinds".into()).unwrap();
let e = t.join().unwrap().unwrap_err();
assert_eq!(e.kind(), ErrorKind::Cancelled, "{e}");
assert_eq!(
CoreError::from(Error::new("read already in progress")).kind(),
ErrorKind::Busy
);
assert_eq!(
CoreError::from(Error::new("weird")).kind(),
ErrorKind::Other
);
m.close_core("kinds".into()).unwrap();
}
#[test]
fn port_in_use_errors_are_busy() {
for text in [
"Unable to acquire exclusive lock on serial port",
"Access is denied.",
"Device or resource busy",
] {
let e = serialport::Error::new(serialport::ErrorKind::NoDevice, text);
assert_eq!(ErrorKind::from_serialport(&e), ErrorKind::Busy, "{text}");
}
let gone = serialport::Error::new(serialport::ErrorKind::NoDevice, "No such file");
assert_eq!(ErrorKind::from_serialport(&gone), ErrorKind::NotFound);
}