Skip to content

Commit f992c9f

Browse files
authored
Fixes: rng warning & flakey example test (#263)
* fix: retries example flaky test This test was flaky as the retry backoff _started_ at 3600ms, instead of capping at 3600ms. This meant that we could have clients picking dead servers for a lot longer, all potentially stuck in long backoff loops. This fixes that, and also fixes the commented jitter. * chore: fix build warning on rng `rng` is now only used on `tokio_unstable`, so fix the build warning.
1 parent b9693a6 commit f992c9f

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

examples/cluster/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ pub fn machine_attrition(
159159
break;
160160
}
161161

162-
// let mut jitter = (Duration::from_secs(0)..mean_delay).gen_range(&mut rng);
163-
tokio::time::sleep(mean_delay).await;
162+
let jitter = cluster.rng().random_range(0..mean_delay.as_millis());
163+
tokio::time::sleep(Duration::from_millis(jitter as u64)).await;
164164

165165
let machines = cluster.get_machines();
166166

@@ -256,7 +256,7 @@ impl Client {
256256

257257
attempt += 1;
258258

259-
let backoff = std::cmp::max(init_backoff * 2u64.pow(attempt), 3600);
259+
let backoff = std::cmp::min(init_backoff * 2u64.pow(attempt), 3600);
260260
let jitter = self.cluster.rng().random_range(0..backoff);
261261

262262
tokio::time::sleep(Duration::from_millis(jitter)).await;

examples/cluster/tests/cluster.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn retries() {
1212

1313
let test_duration = Duration::from_secs(10);
1414

15-
let mut cluster = Cluster::new(50, None);
15+
let mut cluster = Cluster::new(50, Some(8877673959163467592));
1616

1717
let mut sim = turmoil::Builder::new()
1818
.simulation_duration(Duration::from_secs(360))

src/rt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Config {
3838
/// Rng used to seed the tokio runtime.
3939
///
4040
/// Only used if the `tokio_unstable` cfg flag is set.
41+
#[cfg_attr(not(tokio_unstable), expect(dead_code))]
4142
pub rng: Option<SmallRng>,
4243
}
4344

0 commit comments

Comments
 (0)