Skip to content

Commit f91708d

Browse files
committed
test: add a simple test for atomic operations
Signed-off-by: ekexium <[email protected]>
1 parent 9c89895 commit f91708d

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

mock-tikv/src/pd.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ impl Pd for MockPd {
336336
todo!()
337337
}
338338

339-
fn get_dc_locations(
339+
fn get_dc_location_info(
340340
&mut self,
341-
_: ::grpcio::RpcContext<'_>,
342-
_: GetDcLocationsRequest,
343-
_: ::grpcio::UnarySink<GetDcLocationsResponse>,
341+
_: grpcio::RpcContext,
342+
_: GetDcLocationInfoRequest,
343+
_: grpcio::UnarySink<GetDcLocationInfoResponse>,
344344
) {
345345
todo!()
346346
}

mock-tikv/src/server.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use derive_new::new;
55
use futures::{FutureExt, TryFutureExt};
66
use grpcio::{Environment, Server, ServerBuilder};
77
use std::sync::Arc;
8-
use tikv_client_proto::{kvrpcpb::*, tikvpb::*};
8+
use tikv_client_proto::{coprocessor_v2::*, kvrpcpb::*, tikvpb::*};
99

1010
pub const MOCK_TIKV_PORT: u16 = 50019;
1111

@@ -513,4 +513,40 @@ impl Tikv for MockTikv {
513513
) {
514514
todo!()
515515
}
516+
517+
fn raw_get_key_ttl(
518+
&mut self,
519+
_: grpcio::RpcContext,
520+
_: RawGetKeyTtlRequest,
521+
_: grpcio::UnarySink<RawGetKeyTtlResponse>,
522+
) {
523+
todo!()
524+
}
525+
526+
fn raw_compare_and_swap(
527+
&mut self,
528+
_: grpcio::RpcContext,
529+
_: RawCasRequest,
530+
_: grpcio::UnarySink<RawCasResponse>,
531+
) {
532+
todo!()
533+
}
534+
535+
fn coprocessor_v2(
536+
&mut self,
537+
_: grpcio::RpcContext,
538+
_: RawCoprocessorRequest,
539+
_: grpcio::UnarySink<RawCoprocessorResponse>,
540+
) {
541+
todo!()
542+
}
543+
544+
fn get_store_safe_ts(
545+
&mut self,
546+
_: grpcio::RpcContext,
547+
_: StoreSafeTsRequest,
548+
_: grpcio::UnarySink<StoreSafeTsResponse>,
549+
) {
550+
todo!()
551+
}
516552
}

src/raw/client.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,15 @@ impl Client {
449449
pub async fn atomic_compare_and_swap(
450450
&self,
451451
key: impl Into<Key>,
452-
previous_value: Option<Value>,
453-
new_value: Value,
452+
previous_value: impl Into<Option<Value>>,
453+
new_value: impl Into<Value>,
454454
) -> Result<(Option<Value>, bool)> {
455-
let req = new_cas_request(key.into(), new_value, previous_value, self.cf.clone());
455+
let req = new_cas_request(
456+
key.into(),
457+
new_value.into(),
458+
previous_value.into(),
459+
self.cf.clone(),
460+
);
456461
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), req)
457462
.single_region()
458463
.await?

tests/integration_tests.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,52 @@ async fn pessimistic_heartbeat() -> Result<()> {
748748

749749
Ok(())
750750
}
751+
752+
// It tests very basic functionality of atomic operations (put, cas, delete).
753+
#[tokio::test]
754+
#[serial]
755+
async fn raw_cas() -> Result<()> {
756+
clear_tikv().await;
757+
let client = RawClient::new(pd_addrs()).await?;
758+
let key = "key".to_owned();
759+
let value = "value".to_owned();
760+
let new_value = "new value".to_owned();
761+
762+
client.atomic_put(key.clone(), value.clone()).await?;
763+
assert_eq!(
764+
client.get(key.clone()).await?.unwrap(),
765+
value.clone().as_bytes()
766+
);
767+
768+
client
769+
.atomic_compare_and_swap(
770+
key.clone(),
771+
Some("another_value".to_owned()).map(|v| v.into()),
772+
new_value.clone(),
773+
)
774+
.await?;
775+
assert_ne!(
776+
client.get(key.clone()).await?.unwrap(),
777+
new_value.clone().as_bytes()
778+
);
779+
780+
client
781+
.atomic_compare_and_swap(
782+
key.clone(),
783+
Some(value.to_owned()).map(|v| v.into()),
784+
new_value.clone(),
785+
)
786+
.await?;
787+
assert_eq!(
788+
client.get(key.clone()).await?.unwrap(),
789+
new_value.clone().as_bytes()
790+
);
791+
792+
client.atomic_delete(key.clone()).await?;
793+
assert!(client.get(key.clone()).await?.is_none());
794+
Ok(())
795+
}
796+
751797
// helper function
752798
async fn get_u32(client: &RawClient, key: Vec<u8>) -> Result<u32> {
753799
let x = client.get(key).await?.unwrap();

tikv-client-proto/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use protos::*;
4-
pub use protos::{coprocessor, errorpb, kvrpcpb, metapb, mpp, pdpb, raft_serverpb, tikvpb};
4+
pub use protos::{
5+
coprocessor, coprocessor_v2, errorpb, kvrpcpb, metapb, mpp, pdpb, raft_serverpb, tikvpb,
6+
};
57

68
#[allow(dead_code)]
79
#[allow(clippy::all)]

0 commit comments

Comments
 (0)