Skip to content

Commit 189914c

Browse files
committed
refactor(storage-opendal): build the client config default directly
`Default` delegated to `from_properties` with no properties and called `expect`. A later setting without a `#[property]` default would make it panic, and because of the container-level `#[serde(default)]`, serde calls `default()` on every `client_config` it deserializes. Build the struct directly, and keep the two defaults equal with `test_default_matches_property_defaults`. Name OpenDAL's 60-second control-operation budget in the `OPENDAL_IO_TIMEOUT_MS` doc, along with the calls it covers here, `exists` and `metadata`. `test_default_timeouts_match_opendal` now pins both defaults that the doc states. Pin the serialized-form break with a test that rejects the old `"LocalFs"`, `"Memory"` and `{"Memory": null}` forms. Match the stall test's error on `{ timeout: 45 }`, so a 450-second budget cannot pass.
1 parent 777859d commit 189914c

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

  • crates/storage/opendal/src

‎crates/storage/opendal/src/lib.rs‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ pub use resolving::{OpenDalResolvingStorage, OpenDalResolvingStorageFactory};
108108
/// defaults to [`OPENDAL_IO_TIMEOUT_MS_DEFAULT`].
109109
///
110110
/// Each retry attempt is bounded separately, so it is a per-attempt budget, not a total one.
111-
/// Control operations such as `stat` and `rename` are bounded by a separate, fixed budget.
111+
/// Control operations such as `stat` and `rename` keep OpenDAL's separate 60-second budget,
112+
/// which this property does not change. Here that covers `Storage::exists` and
113+
/// `Storage::metadata`, while reads, writes, listing and deletes use this timeout.
112114
pub const OPENDAL_IO_TIMEOUT_MS: &str = "opendal.io-timeout-ms";
113115

114116
/// Default for [`OPENDAL_IO_TIMEOUT_MS`]. Matches the IO timeout default of OpenDAL's
@@ -136,8 +138,9 @@ pub struct OpenDalClientConfig {
136138

137139
impl Default for OpenDalClientConfig {
138140
fn default() -> Self {
139-
// Reuse the `#[property]` defaults, so serde and `from_properties` cannot disagree.
140-
Self::from_properties(&HashMap::new()).expect("every client setting has a default")
141+
Self {
142+
io_timeout_ms: DEFAULT_IO_TIMEOUT_MS,
143+
}
141144
}
142145
}
143146

@@ -860,24 +863,39 @@ mod tests {
860863
}
861864

862865
#[test]
863-
fn test_default_io_timeout_matches_opendal() {
866+
fn test_default_matches_property_defaults() {
867+
// Serde fills missing settings from `Default`, and `from_properties` from the
868+
// `#[property]` defaults. `Debug` covers every field, including ones added later.
869+
assert_eq!(
870+
format!("{:?}", OpenDalClientConfig::default()),
871+
format!(
872+
"{:?}",
873+
OpenDalClientConfig::from_properties(&HashMap::new()).unwrap()
874+
)
875+
);
876+
}
877+
878+
#[test]
879+
fn test_default_timeouts_match_opendal() {
864880
// `TimeoutLayer` has no getters, so compare through `Debug`. An OpenDAL upgrade that
865-
// changes its default fails here instead of silently diverging from it.
881+
// changes either default stated in the `OPENDAL_IO_TIMEOUT_MS` doc fails here.
866882
let opendal_default = format!("{:?}", TimeoutLayer::new());
867-
let with_io_timeout = |ms| {
883+
let layer = |timeout, io_timeout| {
868884
format!(
869885
"{:?}",
870-
TimeoutLayer::new().with_io_timeout(Duration::from_millis(ms))
886+
TimeoutLayer::new()
887+
.with_timeout(timeout)
888+
.with_io_timeout(io_timeout)
871889
)
872890
};
873-
assert_eq!(
874-
opendal_default,
875-
with_io_timeout(OPENDAL_IO_TIMEOUT_MS_DEFAULT)
876-
);
877-
// If `Debug` stopped printing `io_timeout`, the check above would pass vacuously.
891+
let control = Duration::from_secs(60);
892+
let io = Duration::from_millis(OPENDAL_IO_TIMEOUT_MS_DEFAULT);
893+
assert_eq!(opendal_default, layer(control, io));
894+
// If `Debug` stopped printing either field, the check above would pass vacuously.
895+
assert_ne!(opendal_default, layer(control + Duration::from_secs(1), io));
878896
assert_ne!(
879897
opendal_default,
880-
with_io_timeout(OPENDAL_IO_TIMEOUT_MS_DEFAULT + 1)
898+
layer(control, io + Duration::from_millis(1))
881899
);
882900
}
883901

@@ -911,6 +929,19 @@ mod tests {
911929
);
912930
}
913931

932+
#[cfg(all(feature = "opendal-fs", feature = "opendal-memory"))]
933+
#[test]
934+
fn test_old_unit_variant_forms_are_rejected() {
935+
// `LocalFs` and `Memory` used to serialize as bare strings, and `Memory` also accepted
936+
// `null`. As struct variants they reject all three, which is an intended format change.
937+
for old in [r#""LocalFs""#, r#""Memory""#, r#"{"Memory":null}"#] {
938+
assert!(
939+
serde_json::from_str::<OpenDalStorage>(old).is_err(),
940+
"{old}"
941+
);
942+
}
943+
}
944+
914945
#[cfg(feature = "opendal-memory")]
915946
#[test]
916947
fn test_factory_rejects_invalid_io_timeout() {
@@ -938,7 +969,8 @@ mod tests {
938969
.unwrap_err()
939970
.to_string();
940971
assert!(err.contains("io operation timeout reached"), "{err}");
941-
assert!(err.contains("timeout: 45"), "{err}");
972+
// OpenDAL reports the budget in plain seconds, as `context: { timeout: 45 }`.
973+
assert!(err.contains("{ timeout: 45 }"), "{err}");
942974
}
943975

944976
#[cfg(feature = "opendal-s3")]

0 commit comments

Comments
 (0)