-
Notifications
You must be signed in to change notification settings - Fork 76
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 kv client in xline-client
- Loading branch information
Showing
1 changed file
with
217 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 |
---|---|---|
@@ -1 +1,218 @@ | ||
use common::cluster::Cluster; | ||
use xline_client::{ | ||
clients::kv::{Compare, DeleteRangeRequest, PutRequest, RangeRequest, Txn, TxnOp}, | ||
error::Result, | ||
Client, ClientOptions, | ||
}; | ||
use xlineapi::CompareResult; | ||
|
||
mod common; | ||
|
||
#[tokio::test] | ||
async fn test_put() -> Result<()> { | ||
let mut cluster = Cluster::new(3).await; | ||
cluster.start().await; | ||
let mut client = Client::connect(cluster.addrs().clone(), ClientOptions::default()) | ||
.await? | ||
.kv_client(); | ||
let request = PutRequest::new("put", "123"); | ||
client.put(request).await?; | ||
|
||
// overwrite with prev key | ||
{ | ||
let request = PutRequest::new("put", "456").with_prev_kv(true); | ||
let resp = client.put(request).await?; | ||
let prev_key = resp.prev_kv; | ||
assert!(prev_key.is_some()); | ||
let prev_key = prev_key.unwrap(); | ||
assert_eq!(prev_key.key, b"put"); | ||
assert_eq!(prev_key.value, b"123"); | ||
} | ||
|
||
// overwrite again with prev key | ||
{ | ||
let request = PutRequest::new("put", "456").with_prev_kv(true); | ||
let resp = client.put(request).await?; | ||
let prev_key = resp.prev_kv; | ||
assert!(prev_key.is_some()); | ||
let prev_key = prev_key.unwrap(); | ||
assert_eq!(prev_key.key, b"put"); | ||
assert_eq!(prev_key.value, b"456"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get() -> Result<()> { | ||
let mut cluster = Cluster::new(3).await; | ||
cluster.start().await; | ||
let mut client = Client::connect(cluster.addrs().clone(), ClientOptions::default()) | ||
.await? | ||
.kv_client(); | ||
client.put(PutRequest::new("get10", "10")).await?; | ||
client.put(PutRequest::new("get11", "11")).await?; | ||
client.put(PutRequest::new("get20", "20")).await?; | ||
client.put(PutRequest::new("get21", "21")).await?; | ||
|
||
// get key | ||
{ | ||
let resp = client.range(RangeRequest::new("get11")).await?; | ||
assert_eq!(resp.count, 1); | ||
assert!(!resp.more); | ||
assert_eq!(resp.kvs.len(), 1); | ||
assert_eq!(resp.kvs[0].key, b"get11"); | ||
assert_eq!(resp.kvs[0].value, b"11"); | ||
} | ||
|
||
// get from key | ||
{ | ||
let resp = client | ||
.range(RangeRequest::new("get11").with_from_key().with_limit(2)) | ||
.await?; | ||
assert!(resp.more); | ||
assert_eq!(resp.kvs.len(), 2); | ||
assert_eq!(resp.kvs[0].key, b"get11"); | ||
assert_eq!(resp.kvs[0].value, b"11"); | ||
assert_eq!(resp.kvs[1].key, b"get20"); | ||
assert_eq!(resp.kvs[1].value, b"20"); | ||
} | ||
|
||
// get prefix keys | ||
{ | ||
let resp = client | ||
.range(RangeRequest::new("get1").with_prefix()) | ||
.await?; | ||
assert_eq!(resp.count, 2); | ||
assert!(!resp.more); | ||
assert_eq!(resp.kvs.len(), 2); | ||
assert_eq!(resp.kvs[0].key, b"get10"); | ||
assert_eq!(resp.kvs[0].value, b"10"); | ||
assert_eq!(resp.kvs[1].key, b"get11"); | ||
assert_eq!(resp.kvs[1].value, b"11"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_delete() -> Result<()> { | ||
let mut cluster = Cluster::new(3).await; | ||
cluster.start().await; | ||
let mut client = Client::connect(cluster.addrs().clone(), ClientOptions::default()) | ||
.await? | ||
.kv_client(); | ||
client.put(PutRequest::new("del10", "10")).await?; | ||
client.put(PutRequest::new("del11", "11")).await?; | ||
client.put(PutRequest::new("del20", "20")).await?; | ||
client.put(PutRequest::new("del21", "21")).await?; | ||
client.put(PutRequest::new("del31", "31")).await?; | ||
client.put(PutRequest::new("del32", "32")).await?; | ||
|
||
// delete key | ||
{ | ||
let resp = client.delete(DeleteRangeRequest::new("del11")).await?; | ||
assert_eq!(resp.deleted, 1); | ||
let resp = client | ||
.range(RangeRequest::new("del11").with_count_only(true)) | ||
.await?; | ||
assert_eq!(resp.count, 0); | ||
} | ||
|
||
// delete a range of keys | ||
{ | ||
let resp = client | ||
.delete(DeleteRangeRequest::new("del11").with_range_end("del22")) | ||
.await?; | ||
assert_eq!(resp.deleted, 2); | ||
let resp = client | ||
.range( | ||
RangeRequest::new("del11") | ||
.with_range_end("del22") | ||
.with_count_only(true), | ||
) | ||
.await?; | ||
assert_eq!(resp.count, 0); | ||
} | ||
|
||
// delete key with prefix | ||
{ | ||
let resp = client | ||
.delete(DeleteRangeRequest::new("del3").with_prefix()) | ||
.await?; | ||
assert_eq!(resp.deleted, 2); | ||
let resp = client | ||
.range(RangeRequest::new("del32").with_count_only(true)) | ||
.await?; | ||
assert_eq!(resp.count, 0); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_txn() -> Result<()> { | ||
let mut cluster = Cluster::new(3).await; | ||
cluster.start().await; | ||
let mut client = Client::connect(cluster.addrs().clone(), ClientOptions::default()) | ||
.await? | ||
.kv_client(); | ||
|
||
client.put(PutRequest::new("txn01", "01")).await?; | ||
|
||
// transaction 1 | ||
{ | ||
let resp = client | ||
.txn( | ||
Txn::new() | ||
.when(&[Compare::value("txn01", CompareResult::Equal, "01")][..]) | ||
.and_then( | ||
&[TxnOp::put( | ||
PutRequest::new("txn01", "02").with_prev_kv(true), | ||
)][..], | ||
) | ||
.or_else(&[TxnOp::range(RangeRequest::new("txn01"))][..]), | ||
) | ||
.await?; | ||
|
||
assert!(resp.succeeded); | ||
let op_responses = resp.responses; | ||
assert_eq!(op_responses.len(), 1); | ||
|
||
match op_responses[0].response.as_ref().unwrap() { | ||
xlineapi::Response::ResponsePut(resp) => { | ||
assert_eq!(resp.prev_kv.as_ref().unwrap().value, b"01") | ||
} | ||
_ => panic!("unexpected response"), | ||
} | ||
|
||
let resp = client.range(RangeRequest::new("txn01")).await?; | ||
assert_eq!(resp.kvs[0].key, b"txn01"); | ||
assert_eq!(resp.kvs[0].value, b"02"); | ||
} | ||
|
||
// transaction 2 | ||
{ | ||
let resp = client | ||
.txn( | ||
Txn::new() | ||
.when(&[Compare::value("txn01", CompareResult::Equal, "01")][..]) | ||
.and_then(&[TxnOp::put(PutRequest::new("txn01", "02"))][..]) | ||
.or_else(&[TxnOp::range(RangeRequest::new("txn01"))][..]), | ||
) | ||
.await?; | ||
|
||
assert!(!resp.succeeded); | ||
let op_responses = resp.responses; | ||
assert_eq!(op_responses.len(), 1); | ||
|
||
match op_responses[0].response.as_ref().unwrap() { | ||
xlineapi::Response::ResponseRange(resp) => { | ||
assert_eq!(resp.kvs[0].value, b"02") | ||
} | ||
_ => panic!("unexpected response"), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |