-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for api_client slow API call logging (#127)
- Loading branch information
1 parent
f42ef08
commit 82e276d
Showing
9 changed files
with
294 additions
and
59 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
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,2 +1,7 @@ | ||
pub mod mock_git_repo; | ||
mod mock_logger; | ||
mod mock_sentry; | ||
pub mod mock_server; | ||
|
||
pub use mock_logger::mock_logger; | ||
pub use mock_sentry::mock_sentry; |
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,48 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
struct MockLogger { | ||
pub logs: Arc<Mutex<Vec<(log::Level, String)>>>, | ||
} | ||
|
||
impl log::Log for MockLogger { | ||
fn enabled(&self, _: &log::Metadata) -> bool { | ||
true | ||
} | ||
fn flush(&self) {} | ||
fn log(&self, record: &log::Record) { | ||
self.logs | ||
.lock() | ||
.unwrap() | ||
.push((record.level(), record.args().to_string())); | ||
} | ||
} | ||
|
||
pub fn mock_logger(max_level: Option<log::LevelFilter>) -> Arc<Mutex<Vec<(log::Level, String)>>> { | ||
lazy_static! { | ||
static ref MOCK_LOGGER: MockLogger = MockLogger::default(); | ||
} | ||
|
||
log::set_logger(&MOCK_LOGGER as &'static MockLogger).unwrap(); | ||
log::set_max_level(max_level.unwrap_or(log::LevelFilter::Debug)); | ||
|
||
MOCK_LOGGER.logs.clone() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::mock_logger; | ||
|
||
#[test] | ||
fn captures_logs() { | ||
let logs = mock_logger(None); | ||
const TEST_MESSAGE: &str = "test"; | ||
log::error!("{}", TEST_MESSAGE); | ||
assert_eq!( | ||
*logs.lock().unwrap(), | ||
[(log::Level::Error, String::from(TEST_MESSAGE))] | ||
); | ||
} | ||
} |
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,54 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use sentry::{protocol::Event, ClientInitGuard, Integration, Level}; | ||
|
||
struct MockSentryIntegration { | ||
events: Arc<Mutex<Vec<(Level, String)>>>, | ||
} | ||
|
||
impl Integration for MockSentryIntegration { | ||
fn process_event( | ||
&self, | ||
event: Event<'static>, | ||
_: &sentry::ClientOptions, | ||
) -> Option<Event<'static>> { | ||
self.events | ||
.lock() | ||
.unwrap() | ||
.push((event.level, event.message.unwrap_or_default())); | ||
None | ||
} | ||
} | ||
|
||
pub fn mock_sentry() -> (Arc<Mutex<Vec<(Level, String)>>>, ClientInitGuard) { | ||
let events: Arc<Mutex<Vec<(Level, String)>>> = Default::default(); | ||
|
||
let options = sentry::ClientOptions { | ||
environment: Some("development".into()), | ||
..Default::default() | ||
} | ||
.add_integration(MockSentryIntegration { | ||
events: events.clone(), | ||
}); | ||
|
||
let guard = sentry::init(("https://public@sentry.example.com/1", options)); | ||
|
||
(events, guard) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::mock_sentry; | ||
|
||
#[test] | ||
fn captures_events() { | ||
let (events, guard) = mock_sentry(); | ||
const TEST_MESSAGE: &str = "test"; | ||
sentry::capture_message(TEST_MESSAGE, sentry::Level::Error); | ||
guard.flush(None); | ||
assert_eq!( | ||
*events.lock().unwrap(), | ||
[(sentry::Level::Error, String::from(TEST_MESSAGE))] | ||
); | ||
} | ||
} |
Oops, something went wrong.