-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathmock.rs
More file actions
206 lines (175 loc) · 5.41 KB
/
mock.rs
File metadata and controls
206 lines (175 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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.
use crate::{
pd::{PdClient, PdRpcClient, RetryClient},
region::{RegionId, RegionWithLeader},
request::request_codec::RawApiV1,
store::RegionStore,
Config, Error, Key, Result, Timestamp,
};
use async_trait::async_trait;
use derive_new::new;
use slog::{Drain, Logger};
use std::{any::Any, sync::Arc};
use tikv_client_proto::metapb;
use tikv_client_store::{KvClient, KvConnect, Request};
/// Create a `PdRpcClient` with it's internals replaced with mocks so that the
/// client can be tested without doing any RPC calls.
pub async fn pd_rpc_client() -> PdRpcClient<RawApiV1, MockKvConnect, MockCluster> {
let config = Config::default();
let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
let logger = Logger::root(
slog_term::FullFormat::new(plain)
.build()
.filter_level(slog::Level::Info)
.fuse(),
o!(),
);
PdRpcClient::new(
config.clone(),
|_, _| MockKvConnect,
|e, sm| {
futures::future::ok(RetryClient::new_with_cluster(
e,
sm,
config.timeout,
MockCluster,
))
},
RawApiV1,
logger,
)
.await
.unwrap()
}
#[allow(clippy::type_complexity)]
#[derive(new, Default, Clone)]
pub struct MockKvClient {
pub addr: String,
dispatch: Option<Arc<dyn Fn(&dyn Any) -> Result<Box<dyn Any>> + Send + Sync + 'static>>,
}
impl MockKvClient {
pub fn with_dispatch_hook<F>(dispatch: F) -> MockKvClient
where
F: Fn(&dyn Any) -> Result<Box<dyn Any>> + Send + Sync + 'static,
{
MockKvClient {
addr: String::new(),
dispatch: Some(Arc::new(dispatch)),
}
}
}
pub struct MockKvConnect;
pub struct MockCluster;
#[derive(new)]
pub struct MockPdClient {
client: MockKvClient,
}
#[async_trait]
impl KvClient for MockKvClient {
async fn dispatch(&self, req: &dyn Request) -> Result<Box<dyn Any>> {
match &self.dispatch {
Some(f) => f(req.as_any()),
None => panic!("no dispatch hook set"),
}
}
}
impl KvConnect for MockKvConnect {
type KvClient = MockKvClient;
fn connect(&self, address: &str) -> Result<Self::KvClient> {
Ok(MockKvClient {
addr: address.to_owned(),
dispatch: None,
})
}
}
impl MockPdClient {
pub fn default() -> MockPdClient {
MockPdClient {
client: MockKvClient::default(),
}
}
pub fn region1() -> RegionWithLeader {
let mut region = RegionWithLeader::default();
region.region.id = 1;
region.region.set_start_key(vec![]);
region.region.set_end_key(vec![10]);
let leader = metapb::Peer {
store_id: 41,
..Default::default()
};
region.leader = Some(leader);
region
}
pub fn region2() -> RegionWithLeader {
let mut region = RegionWithLeader::default();
region.region.id = 2;
region.region.set_start_key(vec![10]);
region.region.set_end_key(vec![250, 250]);
let leader = metapb::Peer {
store_id: 42,
..Default::default()
};
region.leader = Some(leader);
region
}
pub fn region3() -> RegionWithLeader {
let mut region = RegionWithLeader::default();
region.region.id = 3;
region.region.set_start_key(vec![250, 250]);
region.region.set_end_key(vec![]);
let leader = metapb::Peer {
store_id: 43,
..Default::default()
};
region.leader = Some(leader);
region
}
}
#[async_trait]
impl PdClient for MockPdClient {
type KvClient = MockKvClient;
type RequestCodec = RawApiV1;
async fn map_region_to_store(self: Arc<Self>, region: RegionWithLeader) -> Result<RegionStore> {
Ok(RegionStore::new(region, Arc::new(self.client.clone())))
}
async fn region_for_key(&self, key: &Key) -> Result<RegionWithLeader> {
let bytes: &[_] = key.into();
let region = if bytes.is_empty() || bytes < &[10][..] {
Self::region1()
} else if bytes >= &[10][..] && bytes < &[250, 250][..] {
Self::region2()
} else {
Self::region3()
};
Ok(region)
}
async fn region_for_id(&self, id: RegionId) -> Result<RegionWithLeader> {
match id {
1 => Ok(Self::region1()),
2 => Ok(Self::region2()),
3 => Ok(Self::region3()),
_ => Err(Error::RegionNotFoundInResponse { region_id: id }),
}
}
async fn get_timestamp(self: Arc<Self>) -> Result<Timestamp> {
Ok(Timestamp::default())
}
async fn update_safepoint(self: Arc<Self>, _safepoint: u64) -> Result<bool> {
unimplemented!()
}
async fn update_leader(
&self,
_ver_id: crate::region::RegionVerId,
_leader: metapb::Peer,
) -> Result<()> {
todo!()
}
async fn invalidate_region_cache(&self, _ver_id: crate::region::RegionVerId) {}
fn get_request_codec(&self) -> Self::RequestCodec {
RawApiV1
}
}