A: share tracing capture and peer config fixtures in tests (gate round 1)
This commit is contained in:
1 parent
6bff60c210
commit
d3bef6bd94
5 files changed
+106
-136
No files matched your search
+1
-33
@@ -184,11 +184,10 @@ fn is_loopback(url: &Url) -> bool {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::*;
|
||||
use crate::peers::testing::capture::logged;
|
||||
use crate::peers::types::PeerName;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
fn peer(name: &str, url: &str) -> PeerConfig {
|
||||
PeerConfig {
|
||||
@@ -216,37 +215,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
struct Capture(Arc<Mutex<Vec<String>>>);
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Capture {
|
||||
fn on_event(
|
||||
&self,
|
||||
event: &tracing::Event<'_>,
|
||||
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let mut msg = String::new();
|
||||
event.record(&mut FieldVisit(&mut msg));
|
||||
self.0.lock().expect("log lock").push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
struct FieldVisit<'a>(&'a mut String);
|
||||
|
||||
impl tracing::field::Visit for FieldVisit<'_> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
use std::fmt::Write;
|
||||
let _ = write!(self.0, "{}={value:?} ", field.name());
|
||||
}
|
||||
}
|
||||
|
||||
fn logged(f: impl FnOnce()) -> String {
|
||||
let buf = Arc::new(Mutex::new(Vec::new()));
|
||||
let sub = tracing_subscriber::registry().with(Capture(buf.clone()));
|
||||
tracing::subscriber::with_default(sub, f);
|
||||
let text = buf.lock().expect("log lock").join("\n");
|
||||
text
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_and_loopback_http_are_valid() {
|
||||
validate(&PeersConfig::default()).unwrap();
|
||||
|
||||
Reference in new issue
Block a user