-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract TestTransport to web3::transports (#402)
* Extract TestTransport to web3::transports * Enable the module for web3 tests or when the test feature is enabled Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Disable test feature by default Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Reorder use declarations Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
86 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! Test Transport | ||
|
||
use crate::error::{self, Error}; | ||
use crate::helpers; | ||
use crate::rpc; | ||
use crate::{RequestId, Transport}; | ||
use futures::future::{self, BoxFuture, FutureExt}; | ||
use std::cell::RefCell; | ||
use std::collections::VecDeque; | ||
use std::rc::Rc; | ||
|
||
type Result<T> = BoxFuture<'static, error::Result<T>>; | ||
|
||
/// Test Transport | ||
#[derive(Debug, Default, Clone)] | ||
pub struct TestTransport { | ||
asserted: usize, | ||
requests: Rc<RefCell<Vec<(String, Vec<rpc::Value>)>>>, | ||
responses: Rc<RefCell<VecDeque<rpc::Value>>>, | ||
} | ||
|
||
impl Transport for TestTransport { | ||
type Out = Result<rpc::Value>; | ||
|
||
fn prepare(&self, method: &str, params: Vec<rpc::Value>) -> (RequestId, rpc::Call) { | ||
let request = helpers::build_request(1, method, params.clone()); | ||
self.requests.borrow_mut().push((method.into(), params)); | ||
(self.requests.borrow().len(), request) | ||
} | ||
|
||
fn send(&self, id: RequestId, request: rpc::Call) -> Result<rpc::Value> { | ||
future::ready(match self.responses.borrow_mut().pop_front() { | ||
Some(response) => Ok(response), | ||
None => { | ||
println!("Unexpected request (id: {:?}): {:?}", id, request); | ||
Err(Error::Unreachable) | ||
} | ||
}) | ||
.boxed() | ||
} | ||
} | ||
|
||
impl TestTransport { | ||
/// Set response | ||
pub fn set_response(&mut self, value: rpc::Value) { | ||
*self.responses.borrow_mut() = vec![value].into(); | ||
} | ||
|
||
/// Add response | ||
pub fn add_response(&mut self, value: rpc::Value) { | ||
self.responses.borrow_mut().push_back(value); | ||
} | ||
|
||
/// Assert request | ||
pub fn assert_request(&mut self, method: &str, params: &[String]) { | ||
let idx = self.asserted; | ||
self.asserted += 1; | ||
|
||
let (m, p) = self.requests.borrow().get(idx).expect("Expected result.").clone(); | ||
assert_eq!(&m, method); | ||
let p: Vec<String> = p.into_iter().map(|p| serde_json::to_string(&p).unwrap()).collect(); | ||
assert_eq!(p, params); | ||
} | ||
|
||
/// Assert no more requests | ||
pub fn assert_no_more_requests(&self) { | ||
let requests = self.requests.borrow(); | ||
assert_eq!( | ||
self.asserted, | ||
requests.len(), | ||
"Expected no more requests, got: {:?}", | ||
&requests[self.asserted..] | ||
); | ||
} | ||
} |