|
2 | 2 |
|
3 | 3 | use crate::{ |
4 | 4 | backoff::Backoff, |
5 | | - kv::HasKeys, |
6 | 5 | pd::PdClient, |
7 | 6 | request::{KvRequest, Shardable}, |
8 | 7 | stats::tikv_stats, |
@@ -340,67 +339,12 @@ where |
340 | 339 | async fn execute(&self) -> Result<Self::Result> { |
341 | 340 | let keys = self.inner.get_keys(); |
342 | 341 | let res = self.inner.execute().await?; |
343 | | - // TODO: should we check they have the same length? |
344 | 342 | Ok(ResponseAndKeys(res, keys)) |
345 | 343 | } |
346 | 344 | } |
347 | 345 |
|
348 | | -#[cfg(test)] |
349 | | -mod test { |
350 | | - use super::*; |
351 | | - use crate::mock::{mock_store, MockPdClient}; |
352 | | - use futures::stream::BoxStream; |
353 | | - use tikv_client_proto::kvrpcpb::BatchGetResponse; |
354 | | - |
355 | | - #[derive(Clone)] |
356 | | - struct ErrPlan; |
357 | | - |
358 | | - #[async_trait] |
359 | | - impl Plan for ErrPlan { |
360 | | - type Result = BatchGetResponse; |
361 | | - |
362 | | - async fn execute(&self) -> Result<Self::Result> { |
363 | | - Err(Error::Unimplemented) |
364 | | - } |
365 | | - } |
366 | | - |
367 | | - impl Shardable for ErrPlan { |
368 | | - type Shard = u8; |
369 | | - |
370 | | - fn shards( |
371 | | - &self, |
372 | | - _: &Arc<impl crate::pd::PdClient>, |
373 | | - ) -> BoxStream<'static, crate::Result<(Self::Shard, crate::store::Store)>> { |
374 | | - Box::pin(stream::iter(1..=3).map(|_| Err(Error::Unimplemented))) |
375 | | - .map_ok(|_: u8| (42, mock_store())) |
376 | | - .boxed() |
377 | | - } |
378 | | - |
379 | | - fn apply_shard(&mut self, _: Self::Shard, _: &crate::store::Store) -> Result<()> { |
380 | | - Ok(()) |
381 | | - } |
382 | | - } |
383 | | - |
384 | | - #[tokio::test] |
385 | | - async fn test_err() { |
386 | | - let plan = RetryRegion { |
387 | | - inner: MultiRegion { |
388 | | - inner: ResolveLock { |
389 | | - inner: ErrPlan, |
390 | | - backoff: Backoff::no_backoff(), |
391 | | - pd_client: Arc::new(MockPdClient::default()), |
392 | | - }, |
393 | | - pd_client: Arc::new(MockPdClient::default()), |
394 | | - }, |
395 | | - backoff: Backoff::no_backoff(), |
396 | | - pd_client: Arc::new(MockPdClient::default()), |
397 | | - }; |
398 | | - plan.execute() |
399 | | - .await |
400 | | - .unwrap() |
401 | | - .iter() |
402 | | - .for_each(|r| assert!(r.is_err())); |
403 | | - } |
| 346 | +pub trait HasKeys { |
| 347 | + fn get_keys(&self) -> Vec<Key>; |
404 | 348 | } |
405 | 349 |
|
406 | 350 | // contains a response and the corresponding keys |
@@ -459,3 +403,61 @@ impl Merge<ResponseAndKeys<kvrpcpb::PessimisticLockResponse>> for CollectAndMatc |
459 | 403 | .collect() |
460 | 404 | } |
461 | 405 | } |
| 406 | + |
| 407 | +#[cfg(test)] |
| 408 | +mod test { |
| 409 | + use super::*; |
| 410 | + use crate::mock::{mock_store, MockPdClient}; |
| 411 | + use futures::stream::BoxStream; |
| 412 | + use tikv_client_proto::kvrpcpb::BatchGetResponse; |
| 413 | + |
| 414 | + #[derive(Clone)] |
| 415 | + struct ErrPlan; |
| 416 | + |
| 417 | + #[async_trait] |
| 418 | + impl Plan for ErrPlan { |
| 419 | + type Result = BatchGetResponse; |
| 420 | + |
| 421 | + async fn execute(&self) -> Result<Self::Result> { |
| 422 | + Err(Error::Unimplemented) |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + impl Shardable for ErrPlan { |
| 427 | + type Shard = u8; |
| 428 | + |
| 429 | + fn shards( |
| 430 | + &self, |
| 431 | + _: &Arc<impl crate::pd::PdClient>, |
| 432 | + ) -> BoxStream<'static, crate::Result<(Self::Shard, crate::store::Store)>> { |
| 433 | + Box::pin(stream::iter(1..=3).map(|_| Err(Error::Unimplemented))) |
| 434 | + .map_ok(|_: u8| (42, mock_store())) |
| 435 | + .boxed() |
| 436 | + } |
| 437 | + |
| 438 | + fn apply_shard(&mut self, _: Self::Shard, _: &crate::store::Store) -> Result<()> { |
| 439 | + Ok(()) |
| 440 | + } |
| 441 | + } |
| 442 | + |
| 443 | + #[tokio::test] |
| 444 | + async fn test_err() { |
| 445 | + let plan = RetryRegion { |
| 446 | + inner: MultiRegion { |
| 447 | + inner: ResolveLock { |
| 448 | + inner: ErrPlan, |
| 449 | + backoff: Backoff::no_backoff(), |
| 450 | + pd_client: Arc::new(MockPdClient::default()), |
| 451 | + }, |
| 452 | + pd_client: Arc::new(MockPdClient::default()), |
| 453 | + }, |
| 454 | + backoff: Backoff::no_backoff(), |
| 455 | + pd_client: Arc::new(MockPdClient::default()), |
| 456 | + }; |
| 457 | + plan.execute() |
| 458 | + .await |
| 459 | + .unwrap() |
| 460 | + .iter() |
| 461 | + .for_each(|r| assert!(r.is_err())); |
| 462 | + } |
| 463 | +} |
0 commit comments