From 45bf7c1c338644550dc387e1cdc91cad04facabc Mon Sep 17 00:00:00 2001 From: bsbds <69835502+bsbds@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:24:16 +0800 Subject: [PATCH] test: add tests for lease client in xline-client Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com> --- xline-client/tests/lease.rs | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 xline-client/tests/lease.rs diff --git a/xline-client/tests/lease.rs b/xline-client/tests/lease.rs new file mode 100644 index 000000000..d761b0c90 --- /dev/null +++ b/xline-client/tests/lease.rs @@ -0,0 +1,102 @@ +use common::get_cluster_client; +use xline_client::{ + error::Result, + types::lease::{ + LeaseGrantRequest, LeaseKeepAliveRequest, LeaseRevokeRequest, LeaseTimeToLiveRequest, + }, +}; + +mod common; + +#[tokio::test] +async fn grant_revoke_should_success_in_normal_path() -> Result<()> { + let (_cluster, client) = get_cluster_client().await?; + let mut client = client.lease_client(); + + let resp = client.grant(LeaseGrantRequest::new(123)).await?; + assert_eq!(resp.ttl, 123); + let id = resp.id; + client.revoke(LeaseRevokeRequest::new(id)).await?; + Ok(()) +} + +#[tokio::test] +async fn keep_alive_should_success_in_normal_path() -> Result<()> { + let (_cluster, client) = get_cluster_client().await?; + let mut client = client.lease_client(); + + let resp = client.grant(LeaseGrantRequest::new(60)).await?; + assert_eq!(resp.ttl, 60); + let id = resp.id; + + let (mut keeper, mut stream) = client.keep_alive(LeaseKeepAliveRequest::new(id)).await?; + keeper.keep_alive()?; + + let resp = stream.message().await?.unwrap(); + assert_eq!(resp.id, keeper.id()); + assert_eq!(resp.ttl, 60); + + client.revoke(LeaseRevokeRequest::new(id)).await?; + Ok(()) +} + +#[tokio::test] +async fn time_to_live_ttl_is_consistent_in_normal_path() -> Result<()> { + let (_cluster, client) = get_cluster_client().await?; + let mut client = client.lease_client(); + + let leaseid = 200; + let resp = client + .grant(LeaseGrantRequest::new(60).with_id(leaseid)) + .await?; + assert_eq!(resp.ttl, 60); + assert_eq!(resp.id, leaseid); + + let resp = client + .time_to_live(LeaseTimeToLiveRequest::new(leaseid)) + .await?; + assert_eq!(resp.id, leaseid); + assert_eq!(resp.granted_ttl, 60); + + client.revoke(LeaseRevokeRequest::new(leaseid)).await?; + Ok(()) +} + +#[tokio::test] +async fn leases_should_include_granted_in_normal_path() -> Result<()> { + let lease1 = 100; + let lease2 = 101; + let lease3 = 102; + + let (_cluster, client) = get_cluster_client().await?; + let mut client = client.lease_client(); + + let resp = client + .grant(LeaseGrantRequest::new(60).with_id(lease1)) + .await?; + assert_eq!(resp.ttl, 60); + assert_eq!(resp.id, lease1); + + let resp = client + .grant(LeaseGrantRequest::new(60).with_id(lease2)) + .await?; + assert_eq!(resp.ttl, 60); + assert_eq!(resp.id, lease2); + + let resp = client + .grant(LeaseGrantRequest::new(60).with_id(lease3)) + .await?; + assert_eq!(resp.ttl, 60); + assert_eq!(resp.id, lease3); + + let resp = client.leases().await?; + let leases: Vec<_> = resp.leases.iter().map(|status| status.id).collect(); + assert!(leases.contains(&lease1)); + assert!(leases.contains(&lease2)); + assert!(leases.contains(&lease3)); + + client.revoke(LeaseRevokeRequest::new(lease1)).await?; + client.revoke(LeaseRevokeRequest::new(lease2)).await?; + client.revoke(LeaseRevokeRequest::new(lease3)).await?; + Ok(()) +}