Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rust/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,13 @@ impl Execution {
});
}
Ok(found) => {
let seconds = self.seconds_since(start);
self.emit(Event::Request {
labels: self.labels(Layer::Local),
});
self.emit(Event::Get {
labels: self.labels(Layer::Local),
seconds: self.seconds_since(start),
seconds,
});
let reason = match found {
Some(value) if value.as_ref().type_id() == self.op.metadata.value_type => {
Expand Down
61 changes: 60 additions & 1 deletion rust/tests/metrics_exporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ mod prometheus_exporter {

mod isolation {
use super::*;
use dialcache::testing::{TestExecutor, WALL_EPOCH_MS};
use dialcache::testing::{TestExecutor, VirtualClock, WALL_EPOCH_MS};
use dialcache::{DialCache, KeySpec, Policy};
use std::sync::atomic::{AtomicUsize, Ordering};

Expand All @@ -782,6 +782,65 @@ mod isolation {
}
}

struct AdvancingObserver {
clock: Arc<VirtualClock>,
durations: Arc<Mutex<Vec<f64>>>,
}

impl Observer for AdvancingObserver {
fn observe(&self, event: &Event) {
match event {
Event::Request { labels } if labels.layer == Layer::Local => {
self.clock.shift(250, false);
}
Event::Get { labels, seconds } if labels.layer == Layer::Local => {
self.durations.lock().push(*seconds);
}
_ => {}
}
}
}

#[test]
fn local_get_duration_excludes_request_observer_time() {
let mut exec = TestExecutor::new(WALL_EPOCH_MS);
let durations = Arc::new(Mutex::new(Vec::new()));
let cache = DialCache::builder()
.clock_arc(exec.clock.clone())
.runtime_arc(exec.runtime.clone())
.observer(AdvancingObserver {
clock: exec.clock.clone(),
durations: durations.clone(),
})
.build()
.expect("configuration");
let source_calls = Arc::new(AtomicUsize::new(0));
let counting = source_calls.clone();
let lookup = cache
.use_case::<i64, i64>("item", "lookup")
.policy(Policy::default().local_ttl_sec(60))
.key(|id: &i64| KeySpec::new(id.to_string()))
.source(move |_scope, id: i64| {
counting.fetch_add(1, Ordering::SeqCst);
async move { Ok(id * 10) }
})
.register()
.expect("use case");

for _ in 0..2 {
let cache = cache.clone();
let lookup = lookup.clone();
let value = exec.block_on(async move {
cache
.enable(|scope| async move { lookup.get(&scope, 7).await })
.await
});
assert_eq!(*value.expect("lookup result"), 70);
}
assert_eq!(source_calls.load(Ordering::SeqCst), 1);
assert_eq!(*durations.lock(), [0.0, 0.0]);
}

/// A panicking observer never changes the result of a cached call.
#[test]
fn panicking_observer_does_not_change_cached_results() {
Expand Down
Loading