From 76ab54d549cb44bae4dbcb0ddf3c9c76c12f92a0 Mon Sep 17 00:00:00 2001 From: David Morrison Date: Wed, 17 Jul 2024 11:45:28 -0700 Subject: [PATCH] move clockabilly to an external crate --- Cargo.lock | 4 +- Cargo.toml | 5 +- clockabilly/Cargo.toml | 10 --- clockabilly/src/lib.rs | 69 ------------------- sk-core/src/k8s/lease.rs | 4 +- sk-core/src/k8s/tests/lease_test.rs | 14 ++-- sk-driver/src/runner.rs | 2 +- sk-driver/src/tests/runner_test.rs | 6 +- sk-store/src/tests/import_export_test.rs | 2 +- sk-store/src/watchers/dyn_obj_watcher.rs | 2 +- sk-store/src/watchers/pod_watcher.rs | 2 +- .../src/watchers/tests/pod_watcher_test.rs | 2 +- 12 files changed, 21 insertions(+), 101 deletions(-) delete mode 100644 clockabilly/Cargo.toml delete mode 100644 clockabilly/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2f0075a6..a4dd9873 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -625,7 +625,9 @@ checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "clockabilly" -version = "0.0.0" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49ba5ead05fbdc11573017881b95c32343a382f917cf9354f8f22e3654226983" dependencies = [ "chrono", ] diff --git a/Cargo.toml b/Cargo.toml index 9a97e0b0..908fa4ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] resolver = "2" members = [ - "clockabilly", "sk-api", "sk-cli", "sk-core", @@ -26,16 +25,14 @@ debug = false sk-api.path = "sk-api" sk-core.path = "sk-core" sk-store.path = "sk-store" -testutils.path = "testutils" -clockabilly.path = "clockabilly" anyhow = { version = "1.0.75", features = ["backtrace"] } async-recursion = "1.0.5" async-trait = "0.1.80" bytes = "1.5.0" -chrono = "0.4.26" clap = { version = "4.3.21", features = ["cargo", "derive", "string"] } clap_complete = "4.5.6" +clockabilly = "0.1.0" dirs = "5.0.1" either = "1.12.0" futures = "0.3.28" diff --git a/clockabilly/Cargo.toml b/clockabilly/Cargo.toml deleted file mode 100644 index dc5dd7bf..00000000 --- a/clockabilly/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "clockabilly" -description = "Trait wrapper around chrono for testing" -edition = "2021" - -[dependencies] -chrono = { workspace = true } - -[features] -mock = [] diff --git a/clockabilly/src/lib.rs b/clockabilly/src/lib.rs deleted file mode 100644 index 282b4dc5..00000000 --- a/clockabilly/src/lib.rs +++ /dev/null @@ -1,69 +0,0 @@ -pub use chrono::*; - -// This trait exists for testing, so that we can provide consistent timestamp values to objects -// instead of just relying on whatever the current time actually is. - -pub trait Clockable { - fn now(&self) -> DateTime; - fn now_ts(&self) -> i64; -} - -pub struct UtcClock; - -impl UtcClock { - pub fn new() -> Box { - Box::new(UtcClock) - } -} - -impl Clockable for UtcClock { - fn now(&self) -> DateTime { - Utc::now() - } - - fn now_ts(&self) -> i64 { - Utc::now().timestamp() - } -} - -#[cfg(feature = "mock")] -pub mod mock { - use std::sync::atomic::{ - AtomicI64, - Ordering, - }; - use std::sync::Arc; - - use super::*; - - #[derive(Clone)] - pub struct MockUtcClock { - now: Arc, - } - - impl MockUtcClock { - pub fn new(start_ts: i64) -> Box { - Box::new(MockUtcClock { now: Arc::new(AtomicI64::new(start_ts)) }) - } - - pub fn advance(&mut self, duration: i64) -> i64 { - let old = self.now.fetch_add(duration, Ordering::Relaxed); - old + duration - } - - pub fn set(&mut self, ts: i64) -> i64 { - self.now.store(ts, Ordering::Relaxed); - ts - } - } - - impl Clockable for MockUtcClock { - fn now(&self) -> DateTime { - return DateTime::from_timestamp(self.now_ts(), 0).unwrap(); - } - - fn now_ts(&self) -> i64 { - return self.now.load(Ordering::Relaxed); - } - } -} diff --git a/sk-core/src/k8s/lease.rs b/sk-core/src/k8s/lease.rs index 8369ce7f..d6def1cc 100644 --- a/sk-core/src/k8s/lease.rs +++ b/sk-core/src/k8s/lease.rs @@ -42,7 +42,7 @@ pub async fn try_claim_lease( metaroot: &SimulationRoot, lease_ns: &str, ) -> anyhow::Result { - try_claim_lease_with_clock(client, sim, metaroot, lease_ns, UtcClock::new()).await + try_claim_lease_with_clock(client, sim, metaroot, lease_ns, UtcClock::boxed()).await } pub(super) async fn try_claim_lease_with_clock( @@ -103,7 +103,7 @@ pub async fn try_update_lease( lease_ns: &str, lease_duration: i64, ) -> EmptyResult { - try_update_lease_with_clock(client, sim, lease_ns, lease_duration, UtcClock::new()).await + try_update_lease_with_clock(client, sim, lease_ns, lease_duration, UtcClock::boxed()).await } pub(super) async fn try_update_lease_with_clock( diff --git a/sk-core/src/k8s/tests/lease_test.rs b/sk-core/src/k8s/tests/lease_test.rs index e3f21675..efb75273 100644 --- a/sk-core/src/k8s/tests/lease_test.rs +++ b/sk-core/src/k8s/tests/lease_test.rs @@ -31,7 +31,7 @@ fn lease_other_holder() -> coordinationv1::Lease { #[traced_test] #[tokio::test] async fn test_try_claim_lease_with_clock_already_owned_by_us(test_sim: Simulation, test_sim_root: SimulationRoot) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); let lease_obj = build_lease(&test_sim, &test_sim_root, TEST_CTRL_NAMESPACE, clock.now()); fake_apiserver @@ -52,7 +52,7 @@ async fn test_try_claim_lease_with_clock_already_owned_by_us(test_sim: Simulatio #[traced_test] #[tokio::test] async fn test_try_claim_lease_with_clock_other_lease_unowned(test_sim: Simulation, test_sim_root: SimulationRoot) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); let other_lease: coordinationv1::Lease = Default::default(); let lease_obj = build_lease(&test_sim, &test_sim_root, TEST_CTRL_NAMESPACE, clock.now()); @@ -83,7 +83,7 @@ async fn test_try_claim_lease_with_clock_already_owned_by_other( test_sim_root: SimulationRoot, lease_other_holder: coordinationv1::Lease, ) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); fake_apiserver .handle(move |when, then| { @@ -103,7 +103,7 @@ async fn test_try_claim_lease_with_clock_already_owned_by_other( #[traced_test] #[tokio::test] async fn test_try_claim_lease_with_clock(test_sim: Simulation, test_sim_root: SimulationRoot) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); let lease_obj = build_lease(&test_sim, &test_sim_root, TEST_CTRL_NAMESPACE, clock.now()); fake_apiserver @@ -125,7 +125,7 @@ async fn test_try_claim_lease_with_clock(test_sim: Simulation, test_sim_root: Si #[traced_test] #[tokio::test] async fn test_try_update_lease_with_clock_no_lease_found(test_sim: Simulation) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); fake_apiserver .handle_not_found(format!("/apis/coordination.k8s.io/v1/namespaces/{TEST_LEASE_NS}/leases/{SK_LEASE_NAME}")) @@ -142,7 +142,7 @@ async fn test_try_update_lease_with_clock_no_lease_found(test_sim: Simulation) { #[traced_test] #[tokio::test] async fn test_try_update_lease_with_clock_wrong_owner(test_sim: Simulation, lease_other_holder: coordinationv1::Lease) { - let clock = MockUtcClock::new(NOW); + let clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); fake_apiserver .handle(move |when, then| { @@ -163,7 +163,7 @@ async fn test_try_update_lease_with_clock_wrong_owner(test_sim: Simulation, leas #[traced_test] #[tokio::test] async fn test_try_update_lease_with_clock(test_sim: Simulation, test_sim_root: SimulationRoot) { - let mut clock = MockUtcClock::new(NOW); + let mut clock = MockUtcClock::boxed(NOW); let (mut fake_apiserver, client) = make_fake_apiserver(); let lease_obj = build_lease(&test_sim, &test_sim_root, TEST_CTRL_NAMESPACE, clock.now()); let mut patched_lease_obj = build_lease(&test_sim, &test_sim_root, TEST_CTRL_NAMESPACE, clock.now()); diff --git a/sk-driver/src/runner.rs b/sk-driver/src/runner.rs index 7ef0b9e9..a95fe393 100644 --- a/sk-driver/src/runner.rs +++ b/sk-driver/src/runner.rs @@ -169,7 +169,7 @@ pub async fn run_trace(ctx: DriverContext, client: kube::Client) -> EmptyResult } } - let clock = UtcClock::new(); + let clock = UtcClock::boxed(); let timeout = clock.now_ts() + DRIVER_CLEANUP_TIMEOUT_SECONDS; cleanup_trace(&ctx, roots_api, clock, timeout).await } diff --git a/sk-driver/src/tests/runner_test.rs b/sk-driver/src/tests/runner_test.rs index 4af10518..a6eedb16 100644 --- a/sk-driver/src/tests/runner_test.rs +++ b/sk-driver/src/tests/runner_test.rs @@ -30,7 +30,7 @@ async fn test_cleanup_trace_error() { let store = Arc::new(TraceStore::new(Default::default())); let ctx = build_driver_context(cache, store); - let clock = MockUtcClock::new(0); + let clock = MockUtcClock::boxed(0); fake_apiserver .handle(|when, then| { @@ -58,7 +58,7 @@ async fn test_cleanup_trace_timeout() { let store = Arc::new(TraceStore::new(Default::default())); let ctx = build_driver_context(cache, store); - let clock = MockUtcClock::new(DRIVER_CLEANUP_TIMEOUT_SECONDS + 10); + let clock = MockUtcClock::boxed(DRIVER_CLEANUP_TIMEOUT_SECONDS + 10); let res = cleanup_trace(&ctx, roots_api, clock, DRIVER_CLEANUP_TIMEOUT_SECONDS) .await @@ -79,7 +79,7 @@ async fn test_cleanup_trace() { let store = Arc::new(TraceStore::new(Default::default())); let ctx = build_driver_context(cache, store); - let clock = MockUtcClock::new(0); + let clock = MockUtcClock::boxed(0); fake_apiserver .handle(|when, then| { diff --git a/sk-store/src/tests/import_export_test.rs b/sk-store/src/tests/import_export_test.rs index cdab98de..c293bb13 100644 --- a/sk-store/src/tests/import_export_test.rs +++ b/sk-store/src/tests/import_export_test.rs @@ -128,7 +128,7 @@ fn test_stream(clock: MockUtcClock) -> KubeObjectStream { #[traced_test] #[tokio::test] async fn itest_export(#[case] duration: Option) { - let clock = MockUtcClock::new(0); + let clock = MockUtcClock::boxed(0); // Since we're just generating the results from the stream and not actually querying any // Kubernetes internals or whatever, the TracerConfig is empty. diff --git a/sk-store/src/watchers/dyn_obj_watcher.rs b/sk-store/src/watchers/dyn_obj_watcher.rs index 0e97c5bb..165d9377 100644 --- a/sk-store/src/watchers/dyn_obj_watcher.rs +++ b/sk-store/src/watchers/dyn_obj_watcher.rs @@ -75,7 +75,7 @@ impl DynObjWatcher { Ok(( DynObjWatcher { - clock: UtcClock::new(), + clock: UtcClock::boxed(), obj_stream: select_all(apis), store, diff --git a/sk-store/src/watchers/pod_watcher.rs b/sk-store/src/watchers/pod_watcher.rs index e9604081..4abce357 100644 --- a/sk-store/src/watchers/pod_watcher.rs +++ b/sk-store/src/watchers/pod_watcher.rs @@ -87,7 +87,7 @@ impl PodWatcher { owners_cache: OwnersCache::new(apiset), store, - clock: UtcClock::new(), + clock: UtcClock::boxed(), is_ready: false, ready_tx: tx, }, diff --git a/sk-store/src/watchers/tests/pod_watcher_test.rs b/sk-store/src/watchers/tests/pod_watcher_test.rs index 3d5c0bf4..7c1f1e5c 100644 --- a/sk-store/src/watchers/tests/pod_watcher_test.rs +++ b/sk-store/src/watchers/tests/pod_watcher_test.rs @@ -27,7 +27,7 @@ const END_TS: i64 = 5678; #[fixture] fn clock() -> Box { - MockUtcClock::new(START_TS) + MockUtcClock::boxed(START_TS) } fn make_pod_watcher(