Skip to content

Commit

Permalink
test: add tests for lease client in xline-client
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds authored and Phoenix500526 committed Jul 19, 2023
1 parent cb6e1de commit 45bf7c1
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions xline-client/tests/lease.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 45bf7c1

Please sign in to comment.