-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test for api_client slow API call logging #127
Merged
dfrankland
merged 3 commits into
main
from
dylan/add-api-client-slow-api-call-logging-tests
Oct 17, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we have some really awesome ability to capture logs, Sentry events, and modify APIs for tests