Skip to content

Commit bfa94ac

Browse files
committed
tests: improve qlog test util
1 parent 02eb142 commit bfa94ac

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

iroh/src/endpoint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ mod tests {
13391339
let server_secret_key = SecretKey::generate(&mut rng);
13401340
let server_peer_id = server_secret_key.public();
13411341

1342-
let qlog = QlogFileGroup::new("endpoint_connect_close");
1342+
let qlog = QlogFileGroup::from_env("endpoint_connect_close");
13431343

13441344
// Wait for the endpoint to be started to make sure it's up before clients try to connect
13451345
let ep = Endpoint::empty_builder(RelayMode::Custom(relay_map.clone()))

iroh/src/test_utils.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,76 @@ pub mod qlog {
3030
}
3131

3232
impl QlogFileGroup {
33-
pub fn new(title: impl ToString) -> Self {
33+
/// Creates a new [`QlogFileGroup] that is only enabled if feature flags and environment variables match.
34+
///
35+
/// The [`QlogFileGroup] can be used independent of feature flags, but it will only emit qlog files
36+
/// if the "qlog" feature is enabled and the environment variable IROH_QLOG is set to 1.
37+
///
38+
/// qlog files will be written to `CARGO_MANIFEST_DIR/qlog`.
39+
pub fn from_env(title: impl ToString) -> Self {
3440
let directory = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("qlog");
35-
Self::new_in(directory, title)
41+
Self::new(directory, title)
3642
}
3743

38-
pub fn new_in(directory: impl AsRef<Path>, group_name: impl ToString) -> Self {
44+
/// Creates a new [`QlogFileGroup`] that writes qlog files to the specified directory.
45+
pub fn new(directory: impl AsRef<Path>, title: impl ToString) -> Self {
3946
Self {
40-
title: group_name.to_string(),
47+
title: title.to_string(),
4148
directory: directory.as_ref().to_owned(),
4249
start: Instant::now(),
4350
}
4451
}
4552

53+
/// Creates a [`TransportConfig`] that emits qlog files with a client vantage point, if enabled.
54+
///
55+
/// If the "qlog" feature is enabled, and the environment varialbe IROH_QLOG is set to "1",
56+
/// returns transport config that writes qlog configs to the configured output directory.
57+
/// Otherwise, a default transport config is returned.
4658
pub fn client(&self, name: impl ToString) -> Result<TransportConfig> {
4759
#[cfg(not(feature = "qlog"))]
4860
let config = Default::default();
61+
4962
#[cfg(feature = "qlog")]
50-
let config = self.transport_config(name.to_string(), VantagePointType::Client)?;
63+
let config = if std::env::var("IROH_QLOG").ok().as_deref() == Some("1") {
64+
self.transport_config(name.to_string(), VantagePointType::Client)?
65+
} else {
66+
Default::default()
67+
};
5168
Ok(config)
5269
}
5370

71+
/// Creates a [`TransportConfig`] that emits qlog files with a server vantage point, if enabled.
72+
///
73+
/// If the "qlog" feature is enabled, and the environment varialbe IROH_QLOG is set to "1",
74+
/// returns transport config that writes qlog configs to the configured output directory.
75+
/// Otherwise, a default transport config is returned.
5476
pub fn server(&self, name: impl ToString) -> Result<TransportConfig> {
5577
#[cfg(not(feature = "qlog"))]
5678
let config = Default::default();
79+
5780
#[cfg(feature = "qlog")]
58-
let config = self.transport_config(name.to_string(), VantagePointType::Client)?;
81+
let config = if std::env::var("IROH_QLOG").ok().as_deref() == Some("1") {
82+
self.transport_config(name.to_string(), VantagePointType::Server)?
83+
} else {
84+
Default::default()
85+
};
5986
Ok(config)
6087
}
6188

89+
/// Creates a qlog config with a client vantage point.
6290
#[cfg(feature = "qlog")]
6391
pub fn client_config(&self, name: impl ToString) -> Result<QlogConfig> {
6492
self.qlog_config(name.to_string(), VantagePointType::Client)
6593
}
6694

95+
/// Creates a qlog config with a server vantage point.
6796
#[cfg(feature = "qlog")]
6897
pub fn server_config(&self, name: impl ToString) -> Result<QlogConfig> {
6998
self.qlog_config(name.to_string(), VantagePointType::Server)
7099
}
71100

72101
#[cfg(feature = "qlog")]
73-
pub fn transport_config(
102+
fn transport_config(
74103
&self,
75104
name: String,
76105
vantage_point: VantagePointType,
@@ -82,11 +111,7 @@ pub mod qlog {
82111
}
83112

84113
#[cfg(feature = "qlog")]
85-
pub fn qlog_config(
86-
&self,
87-
name: String,
88-
vantage_point: VantagePointType,
89-
) -> Result<QlogConfig> {
114+
fn qlog_config(&self, name: String, vantage_point: VantagePointType) -> Result<QlogConfig> {
90115
let full_name = format!("{}.{}", self.title, name);
91116
let file_name = format!("{full_name}.qlog");
92117
let file_path = self.directory.join(file_name);

0 commit comments

Comments
 (0)