-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix mitmproxy/mitmproxy#7134: do not log CancelledError * add Python integration tests * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4a45bfa
commit f68f576
Showing
10 changed files
with
301 additions
and
13 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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use log::{LevelFilter, Log, Metadata, Record}; | ||
use std::sync::LazyLock; | ||
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; | ||
use tokio::sync::{mpsc, Mutex, MutexGuard}; | ||
|
||
/// A logger for tests to ensure that log statements are made. | ||
pub struct TestLogger { | ||
tx: UnboundedSender<String>, | ||
rx: Mutex<UnboundedReceiver<String>>, | ||
buf: Mutex<Vec<String>>, | ||
} | ||
impl Log for TestLogger { | ||
fn enabled(&self, _metadata: &Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
self.tx.send(format!("{}", record.args())).unwrap() | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
impl TestLogger { | ||
/// Wait for a log line to appear. If the log message already appeared, | ||
/// we return immediately. | ||
pub async fn wait_for(&self, needle: &str) { | ||
let mut buf = self.buf.lock().await; | ||
if buf.iter().any(|m| m.contains(needle)) { | ||
return; | ||
} | ||
|
||
let mut rx = self.rx.lock().await; | ||
while let Some(m) = rx.recv().await { | ||
let done = m.contains(needle); | ||
buf.push(m); | ||
if done { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/// Get a copy of all log lines so far. | ||
pub async fn logs(&self) -> Vec<String> { | ||
let mut buf = self.buf.lock().await; | ||
let mut rx = self.rx.lock().await; | ||
while let Ok(m) = rx.try_recv() { | ||
buf.push(m); | ||
} | ||
buf.clone() | ||
} | ||
|
||
/// Clear log buffer. | ||
pub async fn clear(&self) { | ||
while let Ok(x) = self.rx.lock().await.try_recv() { | ||
drop(x); | ||
} | ||
self.buf.lock().await.clear(); | ||
} | ||
} | ||
static _LOGGER: LazyLock<Mutex<&'static TestLogger>> = LazyLock::new(|| { | ||
let (tx, rx) = mpsc::unbounded_channel(); | ||
let logger = Box::leak(Box::new(TestLogger { | ||
tx, | ||
rx: Mutex::new(rx), | ||
buf: Mutex::new(vec![]), | ||
})); | ||
log::set_logger(logger).expect("cannot set logger"); | ||
log::set_max_level(LevelFilter::Debug); | ||
Mutex::new(logger) | ||
}); | ||
|
||
/// Initialize the logger. | ||
/// pyo3-asyncio tests all run in parallel in the same runtime, so we use a mutex to ensure | ||
/// that only one test that uses TestLogger runs at the same time. | ||
pub async fn setup_logger() -> MutexGuard<'static, &'static TestLogger> { | ||
let logger = _LOGGER.lock().await; | ||
logger.clear().await; | ||
logger | ||
} |
Oops, something went wrong.