-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a mempool proxy support for concurrent calls
- Loading branch information
1 parent
a268176
commit 33445c0
Showing
2 changed files
with
57 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
mod tests { | ||
use std::sync::Arc; | ||
|
||
use tokio::sync::Mutex; | ||
use tokio::task::JoinSet; | ||
|
||
use crate::{ | ||
mempool::{Mempool, MempoolTrait}, | ||
mempool::{AddTransactionCallType, AddTransactionReturnType, MempoolTrait}, | ||
mempool_proxy::MempoolProxy, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_proxy_add_transaction() { | ||
let mempool = Arc::new(Mutex::new(Mempool::new())); | ||
let mut proxy = MempoolProxy::new(mempool); | ||
assert_eq!(proxy.add_transaction(1).await, 1); | ||
assert_eq!(proxy.add_transaction(1).await, 2); | ||
async fn test_proxy_simple_add_transaction() { | ||
let mut proxy = MempoolProxy::default(); | ||
let tx: AddTransactionCallType = 1; | ||
let expect_result: AddTransactionReturnType = 1; | ||
assert_eq!(proxy.add_transaction(tx).await, expect_result); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_proxy_concurrent_add_transaction() { | ||
let proxy = MempoolProxy::default(); | ||
|
||
let mut tasks: JoinSet<_> = (0..5) | ||
.map(|_| { | ||
let mut proxy = proxy.clone(); | ||
async move { | ||
let tx: AddTransactionCallType = 1; | ||
proxy.add_transaction(tx).await | ||
} | ||
}) | ||
.collect(); | ||
|
||
let mut results: Vec<AddTransactionReturnType> = vec![]; | ||
while let Some(result) = tasks.join_next().await { | ||
results.push(result.unwrap()); | ||
} | ||
|
||
results.sort(); | ||
|
||
let expected_results: Vec<AddTransactionReturnType> = (1..=5).collect(); | ||
assert_eq!(results, expected_results); | ||
} | ||
} |