Skip to content

Commit

Permalink
Add test for TimestampedAverage panic
Browse files Browse the repository at this point in the history
When Instant::now() returns the same value during 2 calls of
TimestampedAverage::compute_next, division by 0 occurs,
and Duration::from_secs_f64 panics because of NaN input.

This commit adds regression test for this problem.
  • Loading branch information
Lorak-mmk committed Oct 11, 2023
1 parent 621a2a0 commit e900ac5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion scylla/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bytes = "1.0.1"
futures = "0.3.6"
histogram = "0.6.9"
num_enum = "0.6"
tokio = { version = "1.27", features = ["net", "time", "io-util", "sync", "rt", "macros"] }
tokio = { version = "1.27", features = ["net", "time", "io-util", "sync", "rt", "macros", "test-util"] }
snap = "1.0"
uuid = { version = "1.0", features = ["v4"] }
rand = "0.8.3"
Expand Down
25 changes: 22 additions & 3 deletions scylla/src/transport/load_balancing/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,7 @@ mod latency_awareness {
use futures::{future::RemoteHandle, FutureExt};
use itertools::Either;
use scylla_cql::errors::{DbError, QueryError};
use tokio::time::{Duration, Instant};
use tracing::{instrument::WithSubscriber, trace, warn};
use uuid::Uuid;

Expand All @@ -2143,7 +2144,6 @@ mod latency_awareness {
atomic::{AtomicU64, Ordering},
Arc, RwLock,
},
time::{Duration, Instant},
};

#[derive(Debug)]
Expand All @@ -2168,7 +2168,7 @@ mod latency_awareness {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct TimestampedAverage {
pub(super) timestamp: Instant,
pub(super) average: Duration,
Expand Down Expand Up @@ -2759,7 +2759,7 @@ mod latency_awareness {
},
ExecutionProfile,
};
use std::time::Instant;
use tokio::time::Instant;

trait DefaultPolicyTestExt {
fn set_nodes_latency_stats(
Expand Down Expand Up @@ -3479,5 +3479,24 @@ mod latency_awareness {

session.query("whatever", ()).await.unwrap_err();
}

#[tokio::test]
async fn timestamped_average_works_when_clock_stops() {
tokio::time::pause();
let avg = Some(TimestampedAverage {
timestamp: Instant::now(),
average: Duration::from_secs(123),
num_measures: 1,
});
let new_avg = TimestampedAverage::compute_next(avg, Duration::from_secs(456), 10.0);
assert_eq!(
new_avg,
Some(TimestampedAverage {
timestamp: Instant::now(),
average: Duration::from_secs(123),
num_measures: 2,
}),
);
}
}
}

0 comments on commit e900ac5

Please sign in to comment.