Skip to content

Commit 63ad411

Browse files
committed
feat: allow rpc mock to insert multiple same requests by migrating from Hashmap to Vec
1 parent 98eb135 commit 63ad411

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

rpc-client/src/mock_sender.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,40 @@ use {
4141

4242
pub const PUBKEY: &str = "7RoSF9fUmdphVCpabEoefH81WwrW7orsWonXWqTXkKV8";
4343

44-
pub type Mocks = HashMap<RpcRequest, Value>;
44+
#[derive(Default)]
45+
pub struct Mocks(Vec<(RpcRequest, Value)>);
46+
47+
impl Mocks {
48+
pub fn new() -> Self {
49+
Self(Vec::new())
50+
}
51+
52+
pub fn from<const N: usize>(mocks: [(RpcRequest, Value); N]) -> Self {
53+
Self(mocks.to_vec())
54+
}
55+
56+
pub fn insert(&mut self, request: RpcRequest, response: Value) {
57+
self.0.push((request, response));
58+
}
59+
60+
pub fn remove(&mut self, request: &RpcRequest) -> Option<Value> {
61+
self.0
62+
.iter()
63+
.position(|(r, _)| r == request)
64+
.map(|index| self.0.remove(index).1)
65+
}
66+
}
67+
68+
impl FromIterator<(RpcRequest, Value)> for Mocks {
69+
fn from_iter<T: IntoIterator<Item = (RpcRequest, Value)>>(iter: T) -> Self {
70+
let mut mocks = Mocks::new();
71+
for (request, response) in iter {
72+
mocks.insert(request, response);
73+
}
74+
mocks
75+
}
76+
}
77+
4578
pub struct MockSender {
4679
mocks: RwLock<Mocks>,
4780
url: String,

rpc-client/src/nonblocking/rpc_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4707,7 +4707,7 @@ pub(crate) fn parse_keyed_accounts(
47074707

47084708
#[doc(hidden)]
47094709
pub fn create_rpc_client_mocks() -> crate::mock_sender::Mocks {
4710-
let mut mocks = std::collections::HashMap::new();
4710+
let mut mocks = crate::mock_sender::Mocks::default();
47114711

47124712
let get_account_request = RpcRequest::GetAccountInfo;
47134713
let get_account_response = serde_json::to_value(Response {

rpc-client/src/rpc_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3677,7 +3677,7 @@ impl RpcClient {
36773677
/// Mocks for documentation examples
36783678
#[doc(hidden)]
36793679
pub fn create_rpc_client_mocks() -> crate::mock_sender::Mocks {
3680-
let mut mocks = std::collections::HashMap::new();
3680+
let mut mocks = crate::mock_sender::Mocks::default();
36813681

36823682
let get_account_request = RpcRequest::GetAccountInfo;
36833683
let get_account_response = serde_json::to_value(Response {

0 commit comments

Comments
 (0)