-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from ethereum-optimism/refcell/channel-bank-tests
fix(derive): Channel Bank Tests and Cleanup
- Loading branch information
Showing
6 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,41 @@ | ||
//! Test Utilities for Telemetry | ||
use crate::traits::{LogLevel, TelemetryProvider}; | ||
use alloc::{rc::Rc, vec::Vec}; | ||
use alloc::{sync::Arc, vec::Vec}; | ||
use alloy_primitives::Bytes; | ||
use core::cell::RefCell; | ||
use spin::mutex::Mutex; | ||
|
||
/// Mock telemetry provider | ||
#[derive(Debug, Default)] | ||
pub struct TestTelemetry { | ||
/// Holds telemetry data with log levels for assertions. | ||
pub(crate) telemetry_calls: Rc<RefCell<Vec<(Bytes, LogLevel)>>>, | ||
pub(crate) telemetry_calls: Arc<Mutex<Vec<(Bytes, LogLevel)>>>, | ||
} | ||
|
||
impl TestTelemetry { | ||
/// Creates a new [TestTelemetry] instance. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Checks the existance of a given ([Bytes], [LogLevel]) call. | ||
pub fn exists(&self, data: Bytes, level: LogLevel) -> bool { | ||
let guard = self.telemetry_calls.lock(); | ||
guard.iter().filter(|(d, l)| *d == data && *l == level).count() > 0 | ||
} | ||
|
||
/// Counts the number of telemetry calls with the given [LogLevel]. | ||
pub fn count_calls(&self, level: LogLevel) -> usize { | ||
let guard = self.telemetry_calls.lock(); | ||
guard.iter().filter(|(_, l)| *l == level).count() | ||
} | ||
} | ||
|
||
impl TelemetryProvider for TestTelemetry { | ||
fn write<I: Into<alloy_primitives::Bytes>>(&self, data: I, level: LogLevel) { | ||
let data = (data.into(), level); | ||
{ | ||
let mut calls = self.telemetry_calls.borrow_mut(); | ||
(*calls).push(data); | ||
} | ||
let binding = self.telemetry_calls.clone(); | ||
let mut guard = binding.lock(); | ||
guard.push(data); | ||
} | ||
} |
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