Skip to content

Commit

Permalink
Enable graceful shutdown on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Jun 4, 2024
1 parent 5f79713 commit a3bbe21
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
os: [macos-latest, windows-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Install Rust Unix
if: runner.os != 'Windows'
Expand Down
4 changes: 3 additions & 1 deletion src/master/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ impl<H: ClipboardHandler> Master<H> {
};

loop {
let res = clipboard.load_wait(
let res = clipboard.load(
clipboard.getter.atoms.clipboard,
clipboard.getter.atoms.incr,
clipboard.getter.atoms.property,
self.handler.sleep_interval(),
);
match res {
Ok(_) => {
Expand All @@ -81,6 +82,7 @@ impl<H: ClipboardHandler> Master<H> {
}
}
},
Err(x11_clipboard::error::Error::Timeout) => (),
Err(error) => {
let error = io::Error::new(
io::ErrorKind::Other,
Expand Down
10 changes: 6 additions & 4 deletions tests/shutdown.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time;
use clipboard_master::{Master, ClipboardHandler, CallbackResult};

pub struct Handler;
Expand All @@ -8,19 +9,20 @@ impl ClipboardHandler for Handler {
}
}

//TODO: Make shutdown work on Linux
//This is currently difficult due to buggy x11-clipboard lib
#[cfg(not(target_arch = "linux"))]
#[test]
fn should_shutdown_successfully() {
const TIMEOUT: time::Duration = time::Duration::from_secs(5);
let mut master = Master::new(Handler).expect("To create master");
let shutdown = master.shutdown_channel();
std::thread::spawn(move || {
std::thread::sleep(core::time::Duration::from_secs(5));
std::thread::sleep(TIMEOUT);
println!("signal");
shutdown.signal();
});

println!("RUN");
let now = time::Instant::now();
master.run().expect("to finish");
assert!(now.elapsed() >= (TIMEOUT - time::Duration::from_millis(500)));
assert!(now.elapsed() <= (TIMEOUT + time::Duration::from_millis(500)));
}

0 comments on commit a3bbe21

Please sign in to comment.