Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test timeout #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,37 @@ mod tests {
assert!(client.is_ok());
assert!(elapsed > Duration::from_secs(2));
}

#[test]
fn test_timeout() {
use std::thread;
use std::time::{Duration, Instant};

let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap(); // 0 means the OS choose a free port
let endpoint = format!("{}", listener.local_addr().unwrap());

let (s, r) = std::sync::mpsc::channel();
thread::spawn(move || {
// emulate an electrum server socket not replying for 30 seconds
s.send(());
let (mut s, _) = listener.accept().unwrap();
thread::sleep(Duration::from_secs(30));
});
// ensure the above thread is started
r.recv_timeout(Duration::from_secs(1)).unwrap();
let client = Client::from_config(
&endpoint,
crate::config::ConfigBuilder::new()
.timeout(Some(1))
.unwrap()
.build(),
)
.unwrap();
let now = Instant::now();
let _ = client.ping();
assert!(
now.elapsed().as_secs() <= 5,
"much more than timeout time passed",
);
}
}