Skip to content

Commit

Permalink
fix: timeout parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
koivunej committed Feb 13, 2024
1 parent b7bbbd2 commit df74b57
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions libs/remote_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,13 +773,19 @@ impl RemoteStorageConfig {

let timeout = toml
.get("timeout")
.map(|timeout| timeout.as_str())
.ok_or_else(|| anyhow::Error::msg("timeout was not a string"))?
.map(|timeout| {
humantime::parse_duration(timeout)
.map_err(|e| anyhow::Error::new(e).context("parse timeout"))
timeout
.as_str()
.ok_or_else(|| anyhow::Error::msg("timeout was not a string"))
})
.transpose()?
.transpose()
.and_then(|timeout| {
timeout
.map(humantime::parse_duration)
.transpose()
.map_err(anyhow::Error::new)
})
.context("parse timeout")?
.unwrap_or(Self::DEFAULT_TIMEOUT);

if timeout < Duration::from_secs(1) {
Expand Down Expand Up @@ -943,4 +949,24 @@ mod tests {
let err = RemotePath::new(Utf8Path::new("/")).expect_err("Should fail on absolute paths");
assert_eq!(err.to_string(), "Path \"/\" is not relative");
}

#[test]
fn parse_localfs_config_with_timeout() {
let input = "local_path = '.'
timeout = '5s'";

let toml = input.parse::<toml_edit::Document>().unwrap();

let config = RemoteStorageConfig::from_toml(toml.as_item())
.unwrap()
.expect("it exists");

assert_eq!(
config,
RemoteStorageConfig {
storage: RemoteStorageKind::LocalFs(Utf8PathBuf::from(".")),
timeout: Duration::from_secs(5)
}
);
}
}

0 comments on commit df74b57

Please sign in to comment.