mirror of https://github.com/tikv/client-rust.git
resolve comments: resume src/mock.rs, improve style
Signed-off-by: ekexium <ekexium@gmail.com>
This commit is contained in:
parent
26e1a566e3
commit
61552961ae
|
@ -31,14 +31,14 @@ impl KvStore {
|
||||||
|
|
||||||
pub fn raw_batch_get(&self, keys: &[Vec<u8>]) -> Vec<KvPair> {
|
pub fn raw_batch_get(&self, keys: &[Vec<u8>]) -> Vec<KvPair> {
|
||||||
let data = self.data.read().unwrap();
|
let data = self.data.read().unwrap();
|
||||||
let mut pairs = vec![];
|
keys.iter()
|
||||||
for key in keys {
|
.map(|key| {
|
||||||
let mut pair = KvPair::default();
|
let mut pair = KvPair::default();
|
||||||
pair.set_value(data.get(key).unwrap_or(&vec![]).to_vec());
|
pair.set_value(data.get(key).unwrap_or(&vec![]).to_vec());
|
||||||
pair.set_key(key.to_vec());
|
pair.set_key(key.to_vec());
|
||||||
pairs.push(pair);
|
pair
|
||||||
}
|
})
|
||||||
pairs
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raw_put(&self, key: &[u8], value: &[u8]) {
|
pub fn raw_put(&self, key: &[u8], value: &[u8]) {
|
||||||
|
@ -62,10 +62,11 @@ impl KvStore {
|
||||||
// if any of the key does not exist, return non-existent keys
|
// if any of the key does not exist, return non-existent keys
|
||||||
pub fn raw_batch_delete<'a>(&self, keys: &'a [Vec<u8>]) -> Result<(), Vec<&'a str>> {
|
pub fn raw_batch_delete<'a>(&self, keys: &'a [Vec<u8>]) -> Result<(), Vec<&'a str>> {
|
||||||
let mut data = self.data.write().unwrap();
|
let mut data = self.data.write().unwrap();
|
||||||
let mut non_exist_keys = vec![];
|
let non_exist_keys = keys
|
||||||
keys.iter()
|
.iter()
|
||||||
.filter(|&key| !data.contains_key(key))
|
.filter(|&key| !data.contains_key(key))
|
||||||
.for_each(|key| non_exist_keys.push(std::str::from_utf8(key).unwrap()));
|
.map(|key| std::str::from_utf8(key).unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
if !non_exist_keys.is_empty() {
|
if !non_exist_keys.is_empty() {
|
||||||
Err(non_exist_keys)
|
Err(non_exist_keys)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
|
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
|
||||||
|
|
||||||
use super::{MockKvClient, MockKvConnect};
|
//! Various mock versions of the various clients and other objects.
|
||||||
|
//!
|
||||||
|
//! The goal is to be able to test functionality independently of the rest of
|
||||||
|
//! the system, in particular without requiring a TiKV or PD server, or RPC layer.
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
pd::{PdClient, PdRpcClient, RetryClient},
|
pd::{PdClient, PdRpcClient, RetryClient},
|
||||||
|
request::DispatchHook,
|
||||||
Config, Error, Key, Result, Timestamp,
|
Config, Error, Key, Result, Timestamp,
|
||||||
};
|
};
|
||||||
|
use fail::fail_point;
|
||||||
use futures::future::{ready, BoxFuture};
|
use futures::future::{ready, BoxFuture, FutureExt};
|
||||||
|
use grpcio::CallOption;
|
||||||
use kvproto::metapb;
|
use kvproto::{errorpb, kvrpcpb, metapb, tikvpb::TikvClient};
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{future::Future, sync::Arc, time::Duration};
|
||||||
|
use tikv_client_store::{HasError, KvClient, KvConnect, Region, RegionId, Store};
|
||||||
use tikv_client_store::{Region, RegionId, Store};
|
|
||||||
|
|
||||||
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
|
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
|
||||||
/// client can be tested without doing any RPC calls.
|
/// client can be tested without doing any RPC calls.
|
||||||
|
@ -32,10 +36,47 @@ pub async fn pd_rpc_client() -> PdRpcClient<MockKvConnect, MockCluster> {
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
pub struct MockPdClient;
|
|
||||||
|
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||||
|
pub struct MockKvClient {
|
||||||
|
addr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MockKvConnect;
|
||||||
|
|
||||||
pub struct MockCluster;
|
pub struct MockCluster;
|
||||||
|
|
||||||
|
pub struct MockPdClient;
|
||||||
|
|
||||||
|
impl KvClient for MockKvClient {
|
||||||
|
fn dispatch<Resp, RpcFuture>(
|
||||||
|
&self,
|
||||||
|
_request_name: &'static str,
|
||||||
|
_fut: grpcio::Result<RpcFuture>,
|
||||||
|
) -> BoxFuture<'static, Result<Resp>>
|
||||||
|
where
|
||||||
|
RpcFuture: Future<Output = std::result::Result<Resp, ::grpcio::Error>>,
|
||||||
|
Resp: HasError + Sized + Clone + Send + 'static,
|
||||||
|
RpcFuture: Send + 'static,
|
||||||
|
{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_rpc_client(&self) -> Arc<TikvClient> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KvConnect for MockKvConnect {
|
||||||
|
type KvClient = MockKvClient;
|
||||||
|
|
||||||
|
fn connect(&self, address: &str) -> Result<Self::KvClient> {
|
||||||
|
Ok(MockKvClient {
|
||||||
|
addr: address.to_owned(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MockPdClient {
|
impl MockPdClient {
|
||||||
pub fn region1() -> Region {
|
pub fn region1() -> Region {
|
||||||
let mut region = Region::default();
|
let mut region = Region::default();
|
||||||
|
@ -105,3 +146,17 @@ impl PdClient for MockPdClient {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DispatchHook for kvrpcpb::ResolveLockRequest {
|
||||||
|
fn dispatch_hook(
|
||||||
|
&self,
|
||||||
|
_opt: CallOption,
|
||||||
|
) -> Option<BoxFuture<'static, Result<kvrpcpb::ResolveLockResponse>>> {
|
||||||
|
fail_point!("region-error", |_| {
|
||||||
|
let mut resp = kvrpcpb::ResolveLockResponse::default();
|
||||||
|
resp.region_error = Some(errorpb::Error::default());
|
||||||
|
Some(ready(Ok(resp)).boxed())
|
||||||
|
});
|
||||||
|
Some(ready(Ok(kvrpcpb::ResolveLockResponse::default())).boxed())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,45 +0,0 @@
|
||||||
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
|
|
||||||
|
|
||||||
use crate::Result;
|
|
||||||
|
|
||||||
use futures::future::BoxFuture;
|
|
||||||
|
|
||||||
use kvproto::tikvpb::TikvClient;
|
|
||||||
use std::{future::Future, sync::Arc};
|
|
||||||
use tikv_client_store::{HasError, KvClient, KvConnect};
|
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
|
||||||
pub struct MockKvClient {
|
|
||||||
pub addr: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MockKvConnect;
|
|
||||||
|
|
||||||
impl KvClient for MockKvClient {
|
|
||||||
fn dispatch<Resp, RpcFuture>(
|
|
||||||
&self,
|
|
||||||
_request_name: &'static str,
|
|
||||||
_fut: grpcio::Result<RpcFuture>,
|
|
||||||
) -> BoxFuture<'static, Result<Resp>>
|
|
||||||
where
|
|
||||||
RpcFuture: Future<Output = std::result::Result<Resp, ::grpcio::Error>>,
|
|
||||||
Resp: HasError + Sized + Clone + Send + 'static,
|
|
||||||
RpcFuture: Send + 'static,
|
|
||||||
{
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_rpc_client(&self) -> Arc<TikvClient> {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl KvConnect for MockKvConnect {
|
|
||||||
type KvClient = MockKvClient;
|
|
||||||
|
|
||||||
fn connect(&self, address: &str) -> Result<Self::KvClient> {
|
|
||||||
Ok(MockKvClient {
|
|
||||||
addr: address.to_owned(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
|
|
||||||
|
|
||||||
//! Various mock versions of the various clients and other objects.
|
|
||||||
//!
|
|
||||||
//! The goal is to be able to test functionality independently of the rest of
|
|
||||||
//! the system, in particular without requiring a TiKV or PD server, or RPC layer.
|
|
||||||
|
|
||||||
mod mock_kv;
|
|
||||||
mod mock_pd;
|
|
||||||
|
|
||||||
pub use mock_kv::{MockKvClient, MockKvConnect};
|
|
||||||
pub use mock_pd::{pd_rpc_client, MockPdClient};
|
|
||||||
|
|
||||||
use crate::{request::DispatchHook, Result};
|
|
||||||
use fail::fail_point;
|
|
||||||
use futures::future::{ready, BoxFuture, FutureExt};
|
|
||||||
use grpcio::CallOption;
|
|
||||||
use kvproto::{errorpb, kvrpcpb};
|
|
||||||
|
|
||||||
impl DispatchHook for kvrpcpb::ResolveLockRequest {
|
|
||||||
fn dispatch_hook(
|
|
||||||
&self,
|
|
||||||
_opt: CallOption,
|
|
||||||
) -> Option<BoxFuture<'static, Result<kvrpcpb::ResolveLockResponse>>> {
|
|
||||||
fail_point!("region-error", |_| {
|
|
||||||
let mut resp = kvrpcpb::ResolveLockResponse::default();
|
|
||||||
resp.region_error = Some(errorpb::Error::default());
|
|
||||||
Some(ready(Ok(resp)).boxed())
|
|
||||||
});
|
|
||||||
Some(ready(Ok(kvrpcpb::ResolveLockResponse::default())).boxed())
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue