dactor 0.3.3

An abstract framework for distributed actors in Rust
Documentation
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Per-actor metrics with lock-free recording and windowed queries.
//!
//! Each actor gets its own [`ActorMetricsHandle`] at spawn time. The handle
//! uses atomic counters for message and error counts — no global lock per
//! message. A shared [`MetricsRegistry`] holds `Arc<ActorMetricsHandle>`
//! references; its lock is only taken when actors are created or destroyed.
//!
//! [`MetricsInterceptor`] is an [`InboundInterceptor`] that holds a direct
//! `Arc<ActorMetricsHandle>` and records metrics without any global map lookup.

use std::any::Any;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::interceptor::*;
use crate::message::{Headers, RuntimeHeaders};
use crate::node::ActorId;

// ---------------------------------------------------------------------------
// RuntimeMetrics
// ---------------------------------------------------------------------------

/// System-level runtime metrics snapshot.
#[derive(Debug, Clone, PartialEq)]
pub struct RuntimeMetrics {
    /// Total actors tracked.
    pub actor_count: usize,
    /// Total messages across all actors (all-time).
    pub total_messages: u64,
    /// Total errors across all actors (all-time).
    pub total_errors: u64,
    /// Messages per second within the window.
    pub message_rate: f64,
    /// Errors per second within the window.
    pub error_rate: f64,
    /// The window duration used for this snapshot.
    pub window: Duration,
    /// Wire envelopes dropped by wire interceptors (all-time).
    pub wire_dropped: u64,
    /// Wire envelopes rejected by wire interceptors (all-time).
    pub wire_rejected: u64,
    /// Wire envelopes delayed by wire interceptors (all-time).
    pub wire_delayed: u64,
}

// ---------------------------------------------------------------------------
// MetricsBucket (internal)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
struct MetricsBucket {
    timestamp: Instant,
    message_count: u64,
    error_count: u64,
    latencies: Vec<Duration>,
    message_counts_by_type: HashMap<String, u64>,
}

impl MetricsBucket {
    fn new(timestamp: Instant) -> Self {
        Self {
            timestamp,
            message_count: 0,
            error_count: 0,
            latencies: Vec::new(),
            message_counts_by_type: HashMap::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// ActorMetricsSnapshot
// ---------------------------------------------------------------------------

/// Immutable point-in-time snapshot of an actor's metrics.
#[derive(Debug, Clone)]
pub struct ActorMetricsSnapshot {
    /// Total messages received (all-time).
    pub message_count: u64,
    /// Total errors (all-time).
    pub error_count: u64,
    /// Messages per second within the window.
    pub message_rate: f64,
    /// Errors per second within the window.
    pub error_rate: f64,
    /// Message counts by type within the window.
    pub message_counts_by_type: HashMap<String, u64>,
    /// Average handler latency within the window.
    pub avg_latency: Option<Duration>,
    /// Maximum handler latency within the window.
    pub max_latency: Option<Duration>,
    /// P99 handler latency within the window.
    pub p99_latency: Option<Duration>,
}

// ---------------------------------------------------------------------------
// ActorMetricsHandle
// ---------------------------------------------------------------------------

/// Per-actor metrics handle. Uses atomics for all-time counters and a
/// per-actor `Mutex` for windowed bucket data.
///
/// **Single actor:** The actor task is the only writer, so the Mutex has
/// zero contention on the write path. Readers (metrics queries) briefly
/// contend but are infrequent.
///
/// **Actor pool:** Multiple workers share one handle, so the Mutex is
/// contended proportionally to pool throughput. For very high-throughput
/// pools, consider per-worker handles merged at query time.
pub struct ActorMetricsHandle {
    // All-time counters (lock-free recording path)
    message_count: AtomicU64,
    error_count: AtomicU64,
    // Windowed bucket data (per-actor, low contention: single writer)
    inner: Mutex<HandleInner>,
    window: Duration,
    bucket_duration: Duration,
}

struct HandleInner {
    buckets: VecDeque<MetricsBucket>,
}

impl HandleInner {
    fn new() -> Self {
        Self {
            buckets: VecDeque::new(),
        }
    }

    fn evict_expired(&mut self, window: Duration) {
        let now = Instant::now();
        while let Some(front) = self.buckets.front() {
            if now.duration_since(front.timestamp) > window {
                self.buckets.pop_front();
            } else {
                break;
            }
        }
    }

    fn current_bucket(
        &mut self,
        bucket_duration: Duration,
        window: Duration,
    ) -> &mut MetricsBucket {
        let now = Instant::now();
        self.evict_expired(window);

        let need_new = match self.buckets.back() {
            Some(last) => now.duration_since(last.timestamp) >= bucket_duration,
            None => true,
        };

        if need_new {
            self.buckets.push_back(MetricsBucket::new(now));
        }

        self.buckets.back_mut().unwrap()
    }

    fn active_buckets(&self, window: Duration) -> impl Iterator<Item = &MetricsBucket> {
        let now = Instant::now();
        self.buckets
            .iter()
            .filter(move |b| now.duration_since(b.timestamp) <= window)
    }

    /// Windowed message count (for rate computation).
    fn windowed_message_count(&self, window: Duration) -> u64 {
        self.active_buckets(window).map(|b| b.message_count).sum()
    }

    /// Windowed error count (for rate computation).
    fn windowed_error_count(&self, window: Duration) -> u64 {
        self.active_buckets(window).map(|b| b.error_count).sum()
    }
}

impl ActorMetricsHandle {
    /// Create a new per-actor handle with the given window and bucket duration.
    pub fn new(window: Duration, bucket_duration: Duration) -> Self {
        Self {
            message_count: AtomicU64::new(0),
            error_count: AtomicU64::new(0),
            inner: Mutex::new(HandleInner::new()),
            window,
            bucket_duration,
        }
    }

    /// Record a message receipt. Atomic increment + brief per-actor lock for
    /// windowed bucket data.
    pub fn record_message(&self, message_type: &str) {
        self.message_count.fetch_add(1, Ordering::Relaxed);
        let mut inner = self.inner.lock().unwrap();
        let bucket = inner.current_bucket(self.bucket_duration, self.window);
        bucket.message_count += 1;
        *bucket
            .message_counts_by_type
            .entry(message_type.to_string())
            .or_insert(0) += 1;
    }

    /// Record an error. Atomic increment + brief per-actor lock.
    pub fn record_error(&self) {
        self.error_count.fetch_add(1, Ordering::Relaxed);
        let mut inner = self.inner.lock().unwrap();
        let bucket = inner.current_bucket(self.bucket_duration, self.window);
        bucket.error_count += 1;
    }

    /// Record a handler latency. Brief per-actor lock.
    pub fn record_latency(&self, latency: Duration) {
        let mut inner = self.inner.lock().unwrap();
        let bucket = inner.current_bucket(self.bucket_duration, self.window);
        bucket.latencies.push(latency);
    }

    /// All-time message count (lock-free read).
    pub fn message_count(&self) -> u64 {
        self.message_count.load(Ordering::Relaxed)
    }

    /// All-time error count (lock-free read).
    pub fn error_count(&self) -> u64 {
        self.error_count.load(Ordering::Relaxed)
    }

    /// Create a point-in-time snapshot of this actor's metrics.
    pub fn snapshot(&self) -> ActorMetricsSnapshot {
        let message_count = self.message_count.load(Ordering::Relaxed);
        let error_count = self.error_count.load(Ordering::Relaxed);

        let inner = self.inner.lock().unwrap();
        let secs = self.window.as_secs_f64();

        let windowed_messages = inner.windowed_message_count(self.window);
        let windowed_errors = inner.windowed_error_count(self.window);

        // Merged by-type counts from active buckets
        let mut by_type = HashMap::new();
        for bucket in inner.active_buckets(self.window) {
            for (k, v) in &bucket.message_counts_by_type {
                *by_type.entry(k.clone()).or_insert(0) += v;
            }
        }

        // Latencies from active buckets
        let latencies: Vec<Duration> = inner
            .active_buckets(self.window)
            .flat_map(|b| b.latencies.iter().copied())
            .collect();

        let avg_latency = if latencies.is_empty() {
            None
        } else {
            let total: Duration = latencies.iter().sum();
            Some(total / latencies.len() as u32)
        };

        let max_latency = latencies.iter().max().copied();

        let p99_latency = if latencies.is_empty() {
            None
        } else {
            let mut sorted = latencies;
            sorted.sort();
            let idx = ((sorted.len() as f64) * 0.99).ceil() as usize - 1;
            Some(sorted[idx.min(sorted.len() - 1)])
        };

        ActorMetricsSnapshot {
            message_count,
            error_count,
            message_rate: if secs > 0.0 {
                windowed_messages as f64 / secs
            } else {
                0.0
            },
            error_rate: if secs > 0.0 {
                windowed_errors as f64 / secs
            } else {
                0.0
            },
            message_counts_by_type: by_type,
            avg_latency,
            max_latency,
            p99_latency,
        }
    }
}

impl std::fmt::Debug for ActorMetricsHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ActorMetricsHandle")
            .field("message_count", &self.message_count.load(Ordering::Relaxed))
            .field("error_count", &self.error_count.load(Ordering::Relaxed))
            .finish()
    }
}

// ---------------------------------------------------------------------------
// MetricsRegistry
// ---------------------------------------------------------------------------

/// Central registry of per-actor metrics handles.
///
/// The registry lock is only taken when actors are registered (spawned) or
/// unregistered (stopped). Recording metrics on a handle requires no
/// registry lock at all.
#[derive(Clone)]
pub struct MetricsRegistry {
    handles: Arc<Mutex<HashMap<ActorId, Arc<ActorMetricsHandle>>>>,
    window: Duration,
    bucket_duration: Duration,
    /// Wire envelopes dropped by wire interceptors (all-time).
    wire_dropped: Arc<AtomicU64>,
    /// Wire envelopes rejected by wire interceptors (all-time).
    wire_rejected: Arc<AtomicU64>,
    /// Wire envelopes delayed by wire interceptors (all-time).
    wire_delayed: Arc<AtomicU64>,
}

impl MetricsRegistry {
    /// Create a new registry.
    ///
    /// * `window` — sliding window duration (e.g., 60 seconds). Only events
    ///   within this window contribute to windowed rates and latencies.
    /// * `bucket_duration` — granularity of each time bucket (e.g., 1 second).
    pub fn new(window: Duration, bucket_duration: Duration) -> Self {
        Self {
            handles: Arc::new(Mutex::new(HashMap::new())),
            window,
            bucket_duration,
            wire_dropped: Arc::new(AtomicU64::new(0)),
            wire_rejected: Arc::new(AtomicU64::new(0)),
            wire_delayed: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Register an actor and return its metrics handle.
    pub fn register(&self, actor_id: ActorId) -> Arc<ActorMetricsHandle> {
        let handle = Arc::new(ActorMetricsHandle::new(self.window, self.bucket_duration));
        self.handles
            .lock()
            .unwrap()
            .insert(actor_id, handle.clone());
        handle
    }

    /// Register a pre-existing handle under the given actor ID.
    /// Used for pools where all workers share a single handle.
    pub fn register_handle(&self, actor_id: ActorId, handle: Arc<ActorMetricsHandle>) {
        self.handles.lock().unwrap().insert(actor_id, handle);
    }

    /// Create a new handle without registering it.
    /// Useful for pools that create the handle before knowing the final ID.
    pub fn create_handle(&self) -> Arc<ActorMetricsHandle> {
        Arc::new(ActorMetricsHandle::new(self.window, self.bucket_duration))
    }

    /// Remove an actor's metrics handle.
    pub fn unregister(&self, actor_id: &ActorId) {
        self.handles.lock().unwrap().remove(actor_id);
    }

    /// Get the metrics handle for a specific actor.
    pub fn get(&self, actor_id: &ActorId) -> Option<Arc<ActorMetricsHandle>> {
        self.handles.lock().unwrap().get(actor_id).cloned()
    }

    /// Snapshot metrics for all registered actors.
    pub fn all(&self) -> Vec<(ActorId, ActorMetricsSnapshot)> {
        let handles = self.handles.lock().unwrap();
        handles
            .iter()
            .map(|(id, h)| (id.clone(), h.snapshot()))
            .collect()
    }

    /// Number of registered actors.
    pub fn actor_count(&self) -> usize {
        self.handles.lock().unwrap().len()
    }

    /// Total messages across all actors (all-time, from atomics).
    pub fn total_messages(&self) -> u64 {
        let handles = self.handles.lock().unwrap();
        handles.values().map(|h| h.message_count()).sum()
    }

    /// Total errors across all actors (all-time, from atomics).
    pub fn total_errors(&self) -> u64 {
        let handles = self.handles.lock().unwrap();
        handles.values().map(|h| h.error_count()).sum()
    }

    /// Point-in-time snapshot of system-level metrics.
    pub fn runtime_metrics(&self) -> RuntimeMetrics {
        let handles = self.handles.lock().unwrap();
        let total_messages: u64 = handles.values().map(|h| h.message_count()).sum();
        let total_errors: u64 = handles.values().map(|h| h.error_count()).sum();

        // Windowed rates: sum windowed counts across all handles
        let mut windowed_messages: u64 = 0;
        let mut windowed_errors: u64 = 0;
        for h in handles.values() {
            let inner = h.inner.lock().unwrap();
            windowed_messages += inner.windowed_message_count(h.window);
            windowed_errors += inner.windowed_error_count(h.window);
        }

        let secs = self.window.as_secs_f64();
        RuntimeMetrics {
            actor_count: handles.len(),
            total_messages,
            total_errors,
            message_rate: if secs > 0.0 {
                windowed_messages as f64 / secs
            } else {
                0.0
            },
            error_rate: if secs > 0.0 {
                windowed_errors as f64 / secs
            } else {
                0.0
            },
            window: self.window,
            wire_dropped: self.wire_dropped.load(Ordering::Relaxed),
            wire_rejected: self.wire_rejected.load(Ordering::Relaxed),
            wire_delayed: self.wire_delayed.load(Ordering::Relaxed),
        }
    }

    /// Record that a wire interceptor dropped an envelope.
    pub fn record_wire_dropped(&self) {
        self.wire_dropped.fetch_add(1, Ordering::Relaxed);
    }

    /// Record that a wire interceptor rejected an envelope.
    pub fn record_wire_rejected(&self) {
        self.wire_rejected.fetch_add(1, Ordering::Relaxed);
    }

    /// Record that a wire interceptor delayed an envelope.
    pub fn record_wire_delayed(&self) {
        self.wire_delayed.fetch_add(1, Ordering::Relaxed);
    }
}

/// Default: 60-second window with 1-second buckets.
impl Default for MetricsRegistry {
    fn default() -> Self {
        Self::new(Duration::from_secs(60), Duration::from_secs(1))
    }
}

// ---------------------------------------------------------------------------
// MetricsInterceptor
// ---------------------------------------------------------------------------

/// Built-in inbound interceptor that records per-actor metrics.
///
/// Each instance holds a direct `Arc<ActorMetricsHandle>` — no global map
/// lookup on the message path. Counters use atomics; the only lock is a
/// per-actor `Mutex` with minimal contention (single writer).
///
/// ```ignore
/// let registry = MetricsRegistry::default();
/// let handle = registry.register(actor_id.clone());
/// let interceptor = MetricsInterceptor::new(handle);
/// // Register as inbound interceptor...
/// // Later:
/// let snapshot = registry.get(&actor_id).unwrap().snapshot();
/// println!("Messages: {}", snapshot.message_count);
/// ```
pub struct MetricsInterceptor {
    handle: Arc<ActorMetricsHandle>,
}

impl MetricsInterceptor {
    /// Create a new `MetricsInterceptor` backed by the given handle.
    pub fn new(handle: Arc<ActorMetricsHandle>) -> Self {
        Self { handle }
    }

    /// Get a reference to the underlying [`ActorMetricsHandle`].
    pub fn handle(&self) -> &Arc<ActorMetricsHandle> {
        &self.handle
    }
}

impl InboundInterceptor for MetricsInterceptor {
    fn name(&self) -> &'static str {
        "metrics"
    }

    fn on_receive(
        &self,
        ctx: &InboundContext<'_>,
        _runtime_headers: &RuntimeHeaders,
        _headers: &mut Headers,
        _message: &dyn Any,
    ) -> Disposition {
        self.handle.record_message(ctx.message_type);
        Disposition::Continue
    }

    fn on_complete(
        &self,
        _ctx: &InboundContext<'_>,
        runtime_headers: &RuntimeHeaders,
        _headers: &Headers,
        outcome: &Outcome<'_>,
    ) {
        let latency = runtime_headers.timestamp.elapsed();
        self.handle.record_latency(latency);

        if matches!(outcome, Outcome::HandlerError { .. }) {
            self.handle.record_error();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node::NodeId;

    /// Use a large window so all events stay visible during fast tests.
    fn test_window() -> Duration {
        Duration::from_secs(60)
    }

    fn test_bucket() -> Duration {
        Duration::from_secs(1)
    }

    fn make_id(local: u64) -> ActorId {
        ActorId {
            node: NodeId("n1".into()),
            local,
        }
    }

    #[test]
    fn test_handle_counts() {
        let h = ActorMetricsHandle::new(test_window(), test_bucket());
        h.record_message("Increment");
        h.record_message("Increment");
        h.record_message("GetCount");
        assert_eq!(h.message_count(), 3);
        let snap = h.snapshot();
        assert_eq!(snap.message_count, 3);
        assert_eq!(snap.message_counts_by_type["Increment"], 2);
        assert_eq!(snap.message_counts_by_type["GetCount"], 1);
    }

    #[test]
    fn test_handle_errors() {
        let h = ActorMetricsHandle::new(test_window(), test_bucket());
        h.record_message("Msg");
        h.record_error();
        assert_eq!(h.error_count(), 1);
        assert_eq!(h.message_count(), 1);
    }

    #[test]
    fn test_handle_latencies() {
        let h = ActorMetricsHandle::new(test_window(), test_bucket());
        h.record_latency(Duration::from_millis(10));
        h.record_latency(Duration::from_millis(20));
        h.record_latency(Duration::from_millis(30));
        let snap = h.snapshot();
        assert_eq!(snap.avg_latency.unwrap(), Duration::from_millis(20));
        assert_eq!(snap.max_latency.unwrap(), Duration::from_millis(30));
    }

    #[test]
    fn test_handle_rates() {
        let h = ActorMetricsHandle::new(test_window(), test_bucket());
        h.record_message("A");
        h.record_message("A");
        h.record_error();
        let snap = h.snapshot();
        // 2 messages in a 60s window → ~0.033 msg/s
        assert!(snap.message_rate > 0.0);
        assert!(snap.error_rate > 0.0);
    }

    #[test]
    fn test_registry_per_actor() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        let id1 = make_id(1);
        let id2 = make_id(2);

        let h1 = registry.register(id1.clone());
        let h2 = registry.register(id2.clone());

        h1.record_message("A");
        h1.record_message("B");
        h2.record_message("A");

        assert_eq!(h1.message_count(), 2);
        assert_eq!(h2.message_count(), 1);
        assert_eq!(registry.total_messages(), 3);
        assert_eq!(registry.actor_count(), 2);
    }

    #[test]
    fn test_registry_errors() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        let id = make_id(1);
        let h = registry.register(id);

        h.record_message("Msg");
        h.record_error();
        assert_eq!(h.error_count(), 1);
        assert_eq!(registry.total_errors(), 1);
    }

    #[test]
    fn test_registry_empty() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        assert!(registry.get(&make_id(999)).is_none());
        assert_eq!(registry.total_messages(), 0);
        assert_eq!(registry.actor_count(), 0);
    }

    #[test]
    fn test_metrics_interceptor_name() {
        let handle = Arc::new(ActorMetricsHandle::new(test_window(), test_bucket()));
        let interceptor = MetricsInterceptor::new(handle);
        assert_eq!(interceptor.name(), "metrics");
    }

    #[test]
    fn test_p99_latency() {
        let h = ActorMetricsHandle::new(test_window(), test_bucket());
        for i in 1..=100 {
            h.record_latency(Duration::from_millis(i));
        }
        let snap = h.snapshot();
        assert!(snap.p99_latency.unwrap() >= Duration::from_millis(99));
    }

    #[test]
    fn test_runtime_metrics_snapshot_includes_rates() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        let id = make_id(1);
        let h = registry.register(id);

        h.record_message("A");
        h.record_message("B");

        let snapshot = registry.runtime_metrics();
        assert_eq!(snapshot.actor_count, 1);
        assert_eq!(snapshot.total_messages, 2);
        assert_eq!(snapshot.total_errors, 0);
        assert_eq!(snapshot.window, test_window());
        assert!(snapshot.message_rate > 0.0);
        assert_eq!(snapshot.error_rate, 0.0);
    }

    #[test]
    fn test_default_registry() {
        let registry = MetricsRegistry::default();
        assert_eq!(registry.total_messages(), 0);
        let snapshot = registry.runtime_metrics();
        assert_eq!(snapshot.window, Duration::from_secs(60));
    }

    #[test]
    fn test_shared_handle_for_pool() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        let handle = registry.create_handle();
        let pool_id = make_id(100);

        // Multiple "workers" share the same handle
        handle.record_message("A");
        handle.record_message("B");
        handle.record_message("A");

        // Register under the pool's ActorId
        registry.register_handle(pool_id.clone(), handle.clone());

        assert_eq!(handle.message_count(), 3);
        assert_eq!(registry.total_messages(), 3);
        assert_eq!(registry.actor_count(), 1);
        let snap = handle.snapshot();
        assert_eq!(snap.message_counts_by_type["A"], 2);
        assert_eq!(snap.message_counts_by_type["B"], 1);
    }

    #[test]
    fn test_unregister() {
        let registry = MetricsRegistry::new(test_window(), test_bucket());
        let id = make_id(1);
        let h = registry.register(id.clone());
        h.record_message("A");

        assert_eq!(registry.actor_count(), 1);
        registry.unregister(&id);
        assert_eq!(registry.actor_count(), 0);
        assert!(registry.get(&id).is_none());
    }
}