Skip to content

Commit 2a92722

Browse files
committed
Simplify waiting for daemon to be ready.
1 parent 877469f commit 2a92722

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tests/helpers/daemon.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Drop for Daemon {
7373

7474
fn sleep() {
7575
use std::{thread, time};
76-
let ten_millis = time::Duration::from_millis(100);
76+
let ten_millis = time::Duration::from_millis(10);
7777
thread::sleep(ten_millis);
7878
}
7979

@@ -87,20 +87,27 @@ impl Daemon {
8787
.arg(&config.config_path)
8888
.spawn()
8989
.expect("Could not create mpd daemon.");
90-
while !config.sock_path.exists() {}
9190

92-
// FIXME: Wait for mpd to finish updating the database.
93-
sleep();
94-
95-
Daemon {
91+
let daemon = Daemon {
9692
_temp_dir: temp_dir,
9793
config: config,
9894
process: process,
95+
};
96+
97+
// Wait until we can connect to the daemon
98+
while let Err(_) = daemon.maybe_connect() {
99+
sleep()
99100
}
101+
102+
daemon
103+
}
104+
105+
fn maybe_connect(&self) -> Result<mpd::Client<UnixStream>, mpd::error::Error> {
106+
let stream = UnixStream::connect(&self.config.sock_path)?;
107+
mpd::Client::new(stream)
100108
}
101109

102110
pub fn connect(&self) -> mpd::Client<UnixStream> {
103-
let stream = UnixStream::connect(&self.config.sock_path).unwrap();
104-
mpd::Client::new(stream).unwrap()
111+
self.maybe_connect().expect("Could not connect to daemon.")
105112
}
106113
}

0 commit comments

Comments
 (0)