Skip to content

Commit

Permalink
Improve time based integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shiro committed Nov 22, 2023
1 parent 40a957e commit 630f9fe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
12 changes: 10 additions & 2 deletions examples/tests/_setup_integration_tests/setup_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ pub fn writer_read(py: Python, module: &PyModule, name: &str) -> Option<EvdevInp
.and_then(|x| serde_json::from_str(&x).unwrap())
}

pub fn writer_read_all(py: Python, module: &PyModule, name: &str) -> Vec<EvdevInputEvent> {
let mut acc = vec![];
while let Some(ev) = writer_read(py, module, name){
acc.push(ev);
}
acc
}

pub fn reader_send(py: Python, module: &PyModule, name: &str, ev: &EvdevInputEvent) {
let target = module.getattr(name).unwrap().to_object(py);
let ev = serde_json::to_string(ev).unwrap();

target.call_method(py, "send", (ev,), None).unwrap();
}
target.call_method(py, "send", (ev, ), None).unwrap();
}
17 changes: 12 additions & 5 deletions examples/tests/wasd_mouse_control.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::thread;
use std::time::Duration;
use evdev_rs::enums::{EV_REL, EventCode};

use crate::*;
Expand All @@ -8,13 +10,18 @@ async fn wasd_mouse_control() -> PyResult<()> {
let m = pytests::include_python!();

reader_send(py, m, "reader_kbd", &EvdevInputEvent::new(&Default::default(), &KEY_W.event_code, 1));

py.allow_threads(|| { thread::sleep(Duration::from_millis(25)); });

reader_send(py, m, "reader_kbd", &EvdevInputEvent::new(&Default::default(), &KEY_W.event_code, 0));

assert_eq!((
writer_read(py, m, "writer_mouse"),
), (
Some(EvdevInputEvent::new(&Default::default(), &EventCode::EV_REL(EV_REL::REL_Y), -15)),
));
assert_eq!(
writer_read_all(py, m, "writer_mouse"),
vec![
EvdevInputEvent::new(&Default::default(), &EventCode::EV_REL(EV_REL::REL_Y), -15),
EvdevInputEvent::new(&Default::default(), &EventCode::EV_REL(EV_REL::REL_Y), -15),
]
);

Ok(())
})?;
Expand Down
13 changes: 10 additions & 3 deletions src/python.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::ops::Add;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time;
pub use pyo3::exceptions::PyRuntimeError;
pub use pyo3::impl_::wrap::OkWrap;
pub use pyo3::prelude::*;
pub use pyo3::PyClass;
pub use pyo3::types::PyDict;
use signal_hook::{consts::SIGINT, iterator::Signals};
use xkeysym::key::hebrew_ayin;

use crate::*;
use crate::mapper::mapper::KeyMapperSnapshot;
Expand All @@ -23,7 +27,7 @@ struct PyKey {

#[pyfunction]
#[pyo3(signature = (* * options))]
fn default( options: Option<&PyDict>) -> PyResult<()> {
fn default(options: Option<&PyDict>) -> PyResult<()> {
let options: HashMap<&str, &PyAny> = match options {
Some(py_dict) => py_dict.extract().unwrap(),
None => HashMap::new()
Expand Down Expand Up @@ -77,14 +81,17 @@ fn wait(py: Python) {
}

#[pyfunction]
fn exit(exit_code: Option<i32>) { std::process::exit(exit_code.unwrap_or(0)); }
fn exit(exit_code: Option<i32>) {
#[cfg(not(feature = "integration"))]
std::process::exit(exit_code.unwrap_or(0));
}

#[cfg(feature = "integration")]
#[pyfunction]
fn __test() -> PyResult<Vec<String>> {
Ok(global::TEST_PIPE.lock().unwrap()
.iter()
.map(|x|serde_json::to_string(x).unwrap())
.map(|x| serde_json::to_string(x).unwrap())
.collect()
)
}
Expand Down

0 comments on commit 630f9fe

Please sign in to comment.