This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new b00f846a2f fix(layers/chaos): preserve fractional error_ratio
precision (#8911)
b00f846a2f is described below
commit b00f846a2f7edce46a1899d9c7f3a270f8144901
Author: Xuanwo <[email protected]>
AuthorDate: Tue Nov 24 03:11:01 2026 +0800
fix(layers/chaos): preserve fractional error_ratio precision (#8911)
---
core/layers/chaos/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++--
1 file changed, 41 insertions(+), 2 deletions(-)
diff --git a/core/layers/chaos/src/lib.rs b/core/layers/chaos/src/lib.rs
index b3e21fd363..aa444e5193 100644
--- a/core/layers/chaos/src/lib.rs
+++ b/core/layers/chaos/src/lib.rs
@@ -210,8 +210,8 @@ impl<R> ChaosReader<R> {
/// If I feel lucky, we can return the correct response. Otherwise,
/// we need to generate an error.
fn i_feel_lucky(&self) -> bool {
- let point: u32 = self.rng.lock().unwrap().random_range(0..100);
- point >= (self.error_ratio * 100.0) as u32
+ let point: f64 = self.rng.lock().unwrap().random();
+ point >= self.error_ratio
}
fn unexpected_eof() -> Error {
@@ -253,3 +253,42 @@ impl<R: oio::Read> oio::Read for ChaosReader<R> {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn count_failures(error_ratio: f64, trials: usize) -> usize {
+ let reader = ChaosReader::new(
+ (),
+ Arc::new(Mutex::new(StdRng::seed_from_u64(0))),
+ error_ratio,
+ );
+ (0..trials).filter(|_| !reader.i_feel_lucky()).count()
+ }
+
+ #[test]
+ fn test_sub_percent_error_ratio_injects_errors() {
+ let failures = count_failures(0.005, 2_000);
+ assert!(
+ failures > 0 && failures < 2_000,
+ "expected error_ratio=0.005 to inject some errors, got
{failures}/2000"
+ );
+ }
+
+ #[test]
+ fn test_fractional_percent_error_ratio_not_truncated() {
+ let failures_one_percent = count_failures(0.01, 2_000);
+ let failures_one_point_five_percent = count_failures(0.015, 2_000);
+ assert!(
+ failures_one_point_five_percent > failures_one_percent,
+ "expected error_ratio=0.015
({failures_one_point_five_percent}/2000) to inject more errors than 0.01
({failures_one_percent}/2000)"
+ );
+ }
+
+ #[test]
+ fn test_error_ratio_boundaries() {
+ assert_eq!(count_failures(0.0, 1_000), 0);
+ assert_eq!(count_failures(1.0, 1_000), 1_000);
+ }
+}