resolve comments: resume src/mock.rs, improve style

Signed-off-by: ekexium <ekexium@gmail.com>
This commit is contained in:
ekexium 2020-09-09 15:28:52 +08:00
parent 26e1a566e3
commit 61552961ae
4 changed files with 76 additions and 97 deletions

View File

@ -31,14 +31,14 @@ impl KvStore {
pub fn raw_batch_get(&self, keys: &[Vec<u8>]) -> Vec<KvPair> {
let data = self.data.read().unwrap();
let mut pairs = vec![];
for key in keys {
let mut pair = KvPair::default();
pair.set_value(data.get(key).unwrap_or(&vec![]).to_vec());
pair.set_key(key.to_vec());
pairs.push(pair);
}
pairs
keys.iter()
.map(|key| {
let mut pair = KvPair::default();
pair.set_value(data.get(key).unwrap_or(&vec![]).to_vec());
pair.set_key(key.to_vec());
pair
})
.collect::<Vec<_>>()
}
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
pub fn raw_batch_delete<'a>(&self, keys: &'a [Vec<u8>]) -> Result<(), Vec<&'a str>> {
let mut data = self.data.write().unwrap();
let mut non_exist_keys = vec![];
keys.iter()
let non_exist_keys = keys
.iter()
.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() {
Err(non_exist_keys)
} else {

View File

@ -1,17 +1,21 @@
// 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::{
pd::{PdClient, PdRpcClient, RetryClient},
request::DispatchHook,
Config, Error, Key, Result, Timestamp,
};
use futures::future::{ready, BoxFuture};
use kvproto::metapb;
use std::{sync::Arc, time::Duration};
use tikv_client_store::{Region, RegionId, Store};
use fail::fail_point;
use futures::future::{ready, BoxFuture, FutureExt};
use grpcio::CallOption;
use kvproto::{errorpb, kvrpcpb, metapb, tikvpb::TikvClient};
use std::{future::Future, sync::Arc, time::Duration};
use tikv_client_store::{HasError, KvClient, KvConnect, Region, RegionId, Store};
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
/// client can be tested without doing any RPC calls.
@ -32,10 +36,47 @@ pub async fn pd_rpc_client() -> PdRpcClient<MockKvConnect, MockCluster> {
.await
.unwrap()
}
pub struct MockPdClient;
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct MockKvClient {
addr: String,
}
pub struct MockKvConnect;
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 {
pub fn region1() -> Region {
let mut region = Region::default();
@ -105,3 +146,17 @@ impl PdClient for MockPdClient {
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())
}
}

View File

@ -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(),
})
}
}

View File

@ -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())
}
}