Skip to content

Commit 8c7dcfc

Browse files
committed
test: add tests for ClientBuilder::connect
1 parent 0539c0c commit 8c7dcfc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/client/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,10 @@ impl ClientBuilder {
16051605
}
16061606

16071607
/// Connects to ZooKeeper cluster.
1608+
///
1609+
/// # Notable errors
1610+
/// * [Error::NoHosts] if no host is available
1611+
/// * [Error::SessionExpired] if specified session expired
16081612
pub async fn connect(&mut self, cluster: &str) -> Result<Client> {
16091613
let (hosts, chroot) = util::parse_connect_string(cluster)?;
16101614
let mut buf = Vec::with_capacity(4096);

tests/zookeeper.rs

+22
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ async fn connect(cluster: &str, chroot: &str) -> zk::Client {
145145
client.chroot(chroot).unwrap()
146146
}
147147

148+
#[test_log::test(tokio::test)]
149+
async fn test_connect_nohosts() {
150+
assert_that!(zk::Client::connect("127.0.0.1:100,127.0.0.1:101").await.unwrap_err()).is_equal_to(zk::Error::NoHosts);
151+
}
152+
153+
#[test_log::test(tokio::test)]
154+
async fn test_connect_session_expired() {
155+
let docker = DockerCli::default();
156+
let zookeeper = docker.run(zookeeper_image());
157+
let zk_port = zookeeper.get_host_port(2181);
158+
159+
let cluster = format!("127.0.0.1:{}", zk_port);
160+
let client = zk::Client::builder().detach().connect(&cluster).await.unwrap();
161+
let timeout = client.session_timeout();
162+
let (id, password) = client.into_session();
163+
164+
tokio::time::sleep(timeout * 2).await;
165+
166+
assert_that!(zk::Client::builder().with_session(id, password).connect(&cluster).await.unwrap_err())
167+
.is_equal_to(zk::Error::SessionExpired);
168+
}
169+
148170
#[test_case("/"; "no_chroot")]
149171
#[test_case("/x"; "chroot_x")]
150172
#[test_case("/x/y"; "chroot_x_y")]

0 commit comments

Comments
 (0)