forked from xline-kv/Xline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for lease client in xline-client
Signed-off-by: bsbds <[email protected]>
- Loading branch information
1 parent
cb6e1de
commit 45bf7c1
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |