Skip to content

Commit fd150f8

Browse files
LeoPatOZ0xNeshi
andauthored
Refactor tests (#19)
Co-authored-by: Nenad <[email protected]>
1 parent c28c5dc commit fd150f8

File tree

2 files changed

+104
-77
lines changed

2 files changed

+104
-77
lines changed

tests/common/mod.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use std::{
2+
sync::{
3+
Arc,
4+
atomic::{AtomicUsize, Ordering},
5+
},
6+
time::Duration,
7+
};
8+
9+
use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::types::Log, sol};
10+
use alloy_node_bindings::{Anvil, AnvilInstance};
11+
use async_trait::async_trait;
12+
use event_scanner::EventCallback;
13+
use tokio::time::sleep;
14+
15+
// Shared test contract used across integration tests
16+
sol! {
17+
#[allow(missing_docs)]
18+
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220b846b706f79f5ae1fc4a4238319e723a092f47ce4051404186424739164ab02264736f6c634300081e0033")]
19+
contract TestCounter {
20+
uint256 public count;
21+
22+
event CountIncreased(uint256 newCount);
23+
event CountDecreased(uint256 newCount);
24+
25+
function increase() public {
26+
count += 1;
27+
emit CountIncreased(count);
28+
}
29+
30+
function decrease() public {
31+
require(count > 0, "Count cannot be negative");
32+
count -= 1;
33+
emit CountDecreased(count);
34+
}
35+
36+
function getCount() public view returns (uint256) {
37+
return count;
38+
}
39+
}
40+
}
41+
42+
pub struct EventCounter {
43+
pub count: Arc<AtomicUsize>,
44+
}
45+
46+
#[async_trait]
47+
impl EventCallback for EventCounter {
48+
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
49+
self.count.fetch_add(1, Ordering::SeqCst);
50+
Ok(())
51+
}
52+
}
53+
54+
pub struct SlowProcessor {
55+
pub delay_ms: u64,
56+
pub processed: Arc<AtomicUsize>,
57+
}
58+
59+
#[async_trait]
60+
impl EventCallback for SlowProcessor {
61+
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
62+
sleep(Duration::from_millis(self.delay_ms)).await;
63+
self.processed.fetch_add(1, Ordering::SeqCst);
64+
Ok(())
65+
}
66+
}
67+
68+
pub fn spawn_anvil(block_time_secs: u64) -> anyhow::Result<AnvilInstance> {
69+
Ok(Anvil::new().block_time(block_time_secs).try_spawn()?)
70+
}
71+
72+
pub async fn build_provider(
73+
anvil: &AnvilInstance,
74+
) -> anyhow::Result<impl alloy::providers::Provider<Ethereum> + Clone> {
75+
let wallet = anvil.wallet().expect("anvil should return a default wallet");
76+
let provider = ProviderBuilder::new().wallet(wallet).connect(anvil.endpoint().as_str()).await?;
77+
Ok(provider)
78+
}
79+
80+
pub async fn deploy_counter<P>(provider: P) -> anyhow::Result<TestCounter::TestCounterInstance<P>>
81+
where
82+
P: alloy::providers::Provider<Ethereum> + Clone,
83+
{
84+
let contract = TestCounter::deploy(provider).await?;
85+
Ok(contract)
86+
}

tests/live_scanning_tests.rs

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,27 @@ use std::{
66
time::Duration,
77
};
88

9-
use alloy::{providers::ProviderBuilder, rpc::types::Log, sol, sol_types::SolEvent};
10-
use alloy_node_bindings::Anvil;
11-
use async_trait::async_trait;
12-
use event_scanner::{EventCallback, EventFilter, ScannerBuilder};
9+
mod common;
10+
use alloy::sol_types::SolEvent;
11+
use common::{TestCounter, build_provider, deploy_counter, spawn_anvil};
12+
use event_scanner::{EventFilter, ScannerBuilder};
1313
use tokio::time::sleep;
1414

15-
sol! {
16-
#[allow(missing_docs)]
17-
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220b846b706f79f5ae1fc4a4238319e723a092f47ce4051404186424739164ab02264736f6c634300081e0033")]
18-
contract LiveTestCounter {
19-
uint256 public count;
20-
21-
event CountIncreased(uint256 newCount);
22-
event CountDecreased(uint256 newCount);
23-
24-
function increase() public {
25-
count += 1;
26-
emit CountIncreased(count);
27-
}
28-
29-
function decrease() public {
30-
require(count > 0, "Count cannot be negative");
31-
count -= 1;
32-
emit CountDecreased(count);
33-
}
34-
35-
function getCount() public view returns (uint256) {
36-
return count;
37-
}
38-
}
39-
}
40-
41-
struct EventCounter {
42-
pub count: Arc<AtomicUsize>,
43-
}
44-
45-
#[async_trait]
46-
impl EventCallback for EventCounter {
47-
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
48-
self.count.fetch_add(1, Ordering::SeqCst);
49-
Ok(())
50-
}
51-
}
52-
53-
struct SlowProcessor {
54-
pub delay_ms: u64,
55-
pub processed: Arc<AtomicUsize>,
56-
}
57-
58-
#[async_trait]
59-
impl EventCallback for SlowProcessor {
60-
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
61-
sleep(Duration::from_millis(self.delay_ms)).await;
62-
self.processed.fetch_add(1, Ordering::SeqCst);
63-
Ok(())
64-
}
65-
}
15+
use crate::common::{EventCounter, SlowProcessor};
6616

6717
#[tokio::test]
6818
async fn test_live_scanning_basic() -> anyhow::Result<()> {
69-
let anvil = Anvil::new().block_time(1).try_spawn()?;
70-
let wallet = anvil.wallet();
71-
let provider =
72-
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;
73-
74-
let contract = LiveTestCounter::deploy(provider.clone()).await?;
19+
let anvil = spawn_anvil(1)?;
20+
let provider = build_provider(&anvil).await?;
21+
let contract = deploy_counter(provider.clone()).await?;
7522
let contract_address = *contract.address();
7623

7724
let event_count = Arc::new(AtomicUsize::new(0));
7825
let callback = Arc::new(EventCounter { count: event_count.clone() });
7926

8027
let filter = EventFilter {
8128
contract_address,
82-
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
29+
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
8330
callback,
8431
};
8532

@@ -103,12 +50,9 @@ async fn test_live_scanning_basic() -> anyhow::Result<()> {
10350

10451
#[tokio::test]
10552
async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {
106-
let anvil = Anvil::new().block_time(1).try_spawn()?;
107-
let wallet = anvil.wallet();
108-
let provider =
109-
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;
110-
111-
let contract = LiveTestCounter::deploy(provider.clone()).await?;
53+
let anvil = spawn_anvil(1)?;
54+
let provider = build_provider(&anvil).await?;
55+
let contract = deploy_counter(provider).await?;
11256
let contract_address = *contract.address();
11357

11458
let increase_count = Arc::new(AtomicUsize::new(0));
@@ -120,13 +64,13 @@ async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {
12064

12165
let increase_filter = EventFilter {
12266
contract_address,
123-
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
67+
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
12468
callback: increase_callback,
12569
};
12670

12771
let decrease_filter = EventFilter {
12872
contract_address,
129-
event: LiveTestCounter::CountDecreased::SIGNATURE.to_owned(),
73+
event: TestCounter::CountDecreased::SIGNATURE.to_owned(),
13074
callback: decrease_callback,
13175
};
13276

@@ -157,20 +101,17 @@ async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {
157101

158102
#[tokio::test]
159103
async fn test_live_scanning_with_slow_processor() -> anyhow::Result<()> {
160-
let anvil = Anvil::new().block_time(1).try_spawn()?;
161-
let wallet = anvil.wallet();
162-
let provider =
163-
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;
164-
165-
let contract = LiveTestCounter::deploy(provider.clone()).await?;
104+
let anvil = spawn_anvil(1)?;
105+
let provider = build_provider(&anvil).await?;
106+
let contract = deploy_counter(provider.clone()).await?;
166107
let contract_address = *contract.address();
167108

168109
let processed = Arc::new(AtomicUsize::new(0));
169110
let callback = Arc::new(SlowProcessor { delay_ms: 100, processed: processed.clone() });
170111

171112
let filter = EventFilter {
172113
contract_address,
173-
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
114+
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
174115
callback,
175116
};
176117

0 commit comments

Comments
 (0)