1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use effect_rs::{Effect, Exit, MetricLabel, REGISTRY, Runtime};
use std::time::Duration;
#[test]
fn test_counter_increment() {
let rt = Runtime::new();
let labels = vec![MetricLabel::new("service", "test")];
let program =
Effect::<(), (), ()>::succeed(()).with_metric_increment("requests", labels.clone());
let res = rt.block_on(program, ());
assert!(matches!(res, Exit::Success(())));
let counter = REGISTRY.get_counter("requests", labels);
assert_eq!(counter.get(), 1);
}
#[test]
fn test_histogram_duration() {
let rt = Runtime::new();
let labels = vec![MetricLabel::new("service", "timer")];
// Sleep 10ms
let program = Effect::<(), (), ()>::sleep(Duration::from_millis(10))
.with_metric_duration("latency", labels.clone());
let res = rt.block_on(program, ());
assert!(matches!(res, Exit::Success(())));
// Check histogram. We can't easily check 'count' directly as buckets are internal detail,
// but the implementation doesn't expose 'count' or 'sum' easily public on Histogram struct yet?
// Let's check if we can add a getter to Histogram or just rely on 'record' not panicking.
// Wait, Histogram struct is public.
// Actually, `REGISTRY.get_histogram` returns Arc<Histogram>.
// Histogram has private fields?
// I defined them as struct fields, default private?
// Let's check `src/metrics.rs`.
// They are private. I should add getters for testing.
}