Skip to content

Commit

Permalink
fix mitmproxy/mitmproxy#7134: do not log CancelledError (#175)
Browse files Browse the repository at this point in the history
* 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
mhils and autofix-ci[bot] authored Sep 3, 2024
1 parent 4a45bfa commit f68f576
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true

env:
rust_clippy: "1.79" # MSRV
rust_clippy: "1.80" # MSRV

jobs:
protobuf:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
matrix:
include:
- os: windows-latest
rust: "1.79" # MSRV
rust: "1.80" # MSRV
args: --exclude macos-certificate-truster
- os: macos-latest
rust: "1.79"
rust: "1.80"
args: --exclude windows-redirector
- os: ubuntu-latest
rust: stable
Expand Down
100 changes: 96 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ version = "0.7.2"
publish = false
repository = "https://github.com/mitmproxy/mitmproxy-rs"
edition = "2021"
rust-version = "1.79" # MSRV
rust-version = "1.80" # MSRV

[package]
name = "mitmproxy"
Expand Down
11 changes: 8 additions & 3 deletions mitmproxy-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ publish.workspace = true

[lib]
name = "mitmproxy_rs"
crate-type = ["cdylib"]
crate-type = ["lib", "cdylib"]

[dependencies]
mitmproxy = { path = "../" }
anyhow = { version = "1.0.86", features = ["backtrace"] }
data-encoding = "2.6.0"
log = "0.4.22"
once_cell = "1"
pyo3 = { version = "0.21", features = ["abi3", "abi3-py310", "extension-module", "anyhow", "experimental-declarative-modules"] }
pyo3-asyncio-0-21 = { version = "0.21", features = ["tokio-runtime"] }
pyo3 = { version = "0.21", features = ["abi3", "abi3-py310", "anyhow", "experimental-declarative-modules"] }
pyo3-asyncio-0-21 = { version = "0.21", features = ["tokio-runtime", "testing", "attributes"] }
pyo3-log = "0.11.0"
rand_core = { version = "0.6.4", features = ["getrandom"] }
tokio = { version = "1.40", features = ["macros", "net", "rt-multi-thread", "sync"] }
Expand All @@ -33,3 +33,8 @@ env_logger = "0.11"

[features]
tracing = ["console-subscriber"]

[[test]]
name = "test_task"
path = "pytests/test_task.rs"
harness = false
4 changes: 4 additions & 0 deletions mitmproxy-rs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ hook-dirs = "mitmproxy_rs._pyinstaller:hook_dirs"
exclude = [
'mitmproxy_rs/_pyinstaller'
]

# https://pyo3.rs/v0.22.2/faq.html?highlight=cargo%20test#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror
[tool.maturin]
features = ["pyo3/extension-module"]
79 changes: 79 additions & 0 deletions mitmproxy-rs/pytests/logger.rs
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
}
Loading

0 comments on commit f68f576

Please sign in to comment.