dactor 0.2.0

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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//! Conformance test suite for verifying runtime implementations.
//!
//! Any runtime that implements the dactor v0.2 API can use these tests
//! to verify correct behavior. Call each test function with a factory
//! that creates your runtime's actor ref.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use async_trait::async_trait;
use tokio_util::sync::CancellationToken;

use crate::actor::{
    Actor, ActorContext, ActorError, ActorRef, ReduceHandler, Handler, ExpandHandler,
    TransformHandler,
};
use std::future::Future;
use crate::errors::{ActorSendError, ErrorAction, RuntimeError};
use crate::message::Message;
use crate::stream::{BatchConfig, BoxStream, StreamReceiver, StreamSender};

// ══════════════════════════════════════════════════════
// Test Actor Definitions
// ══════════════════════════════════════════════════════

// ── ConformanceCounter ──────────────────────────────

/// A simple counter actor used by the conformance suite.
pub struct ConformanceCounter {
    count: u64,
}

impl Actor for ConformanceCounter {
    type Args = u64;
    type Deps = ();
    fn create(args: u64, _deps: ()) -> Self {
        Self { count: args }
    }
}

/// Tell message: increment the counter by the given amount.
pub struct Increment(pub u64);
impl Message for Increment {
    type Reply = ();
}

#[async_trait]
impl Handler<Increment> for ConformanceCounter {
    async fn handle(&mut self, msg: Increment, _ctx: &mut ActorContext) {
        self.count += msg.0;
    }
}

/// Ask message: return the current count.
pub struct GetCount;
impl Message for GetCount {
    type Reply = u64;
}

#[async_trait]
impl Handler<GetCount> for ConformanceCounter {
    async fn handle(&mut self, _msg: GetCount, _ctx: &mut ActorContext) -> u64 {
        self.count
    }
}

// ── ConformanceStreamer ─────────────────────────────

/// Streaming actor: emits a sequence of numbers.
pub struct ConformanceStreamer;

impl Actor for ConformanceStreamer {
    type Args = ();
    type Deps = ();
    fn create(_: (), _: ()) -> Self {
        Self
    }
}

/// Stream request: emit `count` sequential numbers starting from 0.
pub struct StreamNumbers {
    pub count: u64,
}
impl Message for StreamNumbers {
    type Reply = u64;
}

#[async_trait]
impl ExpandHandler<StreamNumbers, u64> for ConformanceStreamer {
    async fn handle_expand(
        &mut self,
        msg: StreamNumbers,
        sender: StreamSender<u64>,
        _ctx: &mut ActorContext,
    ) {
        for i in 0..msg.count {
            if sender.send(i).await.is_err() {
                break;
            }
        }
    }
}

// ── ConformanceAggregator ───────────────────────────

/// Feed actor: aggregates a stream of items into a single reply.
pub struct ConformanceAggregator;

impl Actor for ConformanceAggregator {
    type Args = ();
    type Deps = ();
    fn create(_: (), _: ()) -> Self {
        Self
    }
}

#[async_trait]
impl ReduceHandler<i64, i64> for ConformanceAggregator {
    async fn handle_reduce(&mut self, mut rx: StreamReceiver<i64>, _ctx: &mut ActorContext) -> i64 {
        let mut sum = 0i64;
        while let Some(v) = rx.recv().await {
            sum += v;
        }
        sum
    }
}

// ── ConformanceLifecycle ────────────────────────────

/// Lifecycle actor: records on_start and on_stop events in a shared log.
pub struct ConformanceLifecycle {
    pub log: Arc<Mutex<Vec<String>>>,
}

#[async_trait]
impl Actor for ConformanceLifecycle {
    type Args = Arc<Mutex<Vec<String>>>;
    type Deps = ();
    fn create(args: Arc<Mutex<Vec<String>>>, _: ()) -> Self {
        Self { log: args }
    }
    async fn on_start(&mut self, _ctx: &mut ActorContext) {
        self.log.lock().unwrap().push("start".to_string());
    }
    async fn on_stop(&mut self) {
        self.log.lock().unwrap().push("stop".to_string());
    }
}

/// No-op tell message for ConformanceLifecycle so we can verify it's running.
pub struct LifecyclePing;
impl Message for LifecyclePing {
    type Reply = ();
}

#[async_trait]
impl Handler<LifecyclePing> for ConformanceLifecycle {
    async fn handle(&mut self, _msg: LifecyclePing, _ctx: &mut ActorContext) {}
}

// ── ConformanceResumeActor ──────────────────────────

/// Resume-on-error actor: returns `ErrorAction::Resume` so it survives panics.
pub struct ConformanceResumeActor {
    pub count: Arc<AtomicU64>,
}

impl Actor for ConformanceResumeActor {
    type Args = Arc<AtomicU64>;
    type Deps = ();
    fn create(args: Arc<AtomicU64>, _: ()) -> Self {
        Self { count: args }
    }
    fn on_error(&mut self, _: &ActorError) -> ErrorAction {
        ErrorAction::Resume
    }
}

/// Tell message that deliberately panics.
pub struct PanicMsg;
impl Message for PanicMsg {
    type Reply = ();
}

#[async_trait]
impl Handler<PanicMsg> for ConformanceResumeActor {
    async fn handle(&mut self, _msg: PanicMsg, _ctx: &mut ActorContext) {
        panic!("intentional conformance panic");
    }
}

/// Tell message that increments the shared counter.
pub struct CountMsg;
impl Message for CountMsg {
    type Reply = ();
}

#[async_trait]
impl Handler<CountMsg> for ConformanceResumeActor {
    async fn handle(&mut self, _msg: CountMsg, _ctx: &mut ActorContext) {
        self.count.fetch_add(1, Ordering::SeqCst);
    }
}

// ── ConformanceMultiHandler ────────────────────────────

/// Actor that implements Handler for two different message types.
pub struct ConformanceMultiHandler {
    value: i64,
}

impl Actor for ConformanceMultiHandler {
    type Args = i64;
    type Deps = ();
    fn create(args: i64, _: ()) -> Self {
        Self { value: args }
    }
}

/// Ask message: add amount and return new value.
pub struct AddValue(pub i64);
impl Message for AddValue {
    type Reply = i64;
}

#[async_trait]
impl Handler<AddValue> for ConformanceMultiHandler {
    async fn handle(&mut self, msg: AddValue, _ctx: &mut ActorContext) -> i64 {
        self.value += msg.0;
        self.value
    }
}

/// Ask message: multiply by factor and return new value.
pub struct MulValue(pub i64);
impl Message for MulValue {
    type Reply = i64;
}

#[async_trait]
impl Handler<MulValue> for ConformanceMultiHandler {
    async fn handle(&mut self, msg: MulValue, _ctx: &mut ActorContext) -> i64 {
        self.value *= msg.0;
        self.value
    }
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Basic
// ══════════════════════════════════════════════════════

/// Test: tell delivers messages and ask returns the correct reply.
pub async fn test_tell_and_ask<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("counter", 0).await.unwrap();
    actor.tell(Increment(5)).unwrap();
    actor.tell(Increment(3)).unwrap();

    // Allow messages to process
    tokio::time::sleep(Duration::from_millis(50)).await;

    let count = actor.ask(GetCount, None).unwrap().await.unwrap();
    assert_eq!(count, 8, "tell+ask: expected 8, got {}", count);

    actor.stop();
    tokio::time::sleep(Duration::from_millis(50)).await;
    assert!(!actor.is_alive(), "actor should be stopped");
}

/// Test: 100 messages processed in order.
pub async fn test_message_ordering<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("ordered", 0).await.unwrap();
    for _ in 1..=100 {
        actor.tell(Increment(1)).unwrap();
    }
    tokio::time::sleep(Duration::from_millis(200)).await;
    let count = actor.ask(GetCount, None).unwrap().await.unwrap();
    assert_eq!(count, 100, "ordering: expected 100, got {}", count);
}

/// Test: ask returns the correct reply type.
pub async fn test_ask_reply<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("ask-reply", 42).await.unwrap();
    let count = actor.ask(GetCount, None).unwrap().await.unwrap();
    assert_eq!(count, 42);
}

/// Test: stop() makes actor not alive.
pub async fn test_stop<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("stopper", 0).await.unwrap();
    assert!(actor.is_alive());
    actor.stop();
    tokio::time::sleep(Duration::from_millis(50)).await;
    assert!(!actor.is_alive());
}

/// Test: actor IDs are unique per spawn.
pub async fn test_unique_ids<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: Fn(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let a1 = spawn("a", 0).await.unwrap();
    let a2 = spawn("b", 0).await.unwrap();
    assert_ne!(a1.id(), a2.id(), "actor IDs should be unique");
}

/// Test: actor name matches the name given at spawn.
pub async fn test_actor_name<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("my-counter", 0).await.unwrap();
    assert_eq!(actor.name(), "my-counter");
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Streams
// ══════════════════════════════════════════════════════

/// Test: stream returns the expected sequence of items.
pub async fn test_stream_items<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceStreamer>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-streamer", ()).await.unwrap();
    let stream = actor
        .expand(StreamNumbers { count: 5 }, 16, None, None)
        .unwrap();
    let items: Vec<u64> = stream.collect().await;
    assert_eq!(items, vec![0, 1, 2, 3, 4]);
}

/// Test: stream with count=0 returns an empty stream.
pub async fn test_stream_empty<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceStreamer>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-streamer-empty", ()).await.unwrap();
    let stream = actor
        .expand(StreamNumbers { count: 0 }, 16, None, None)
        .unwrap();
    let items: Vec<u64> = stream.collect().await;
    assert!(items.is_empty(), "expected empty stream, got {:?}", items);
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Feed
// ══════════════════════════════════════════════════════

/// Test: feed aggregates a stream of items into a single reply.
pub async fn test_feed_sum<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceAggregator>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-agg", ()).await.unwrap();
    let input: BoxStream<i64> = Box::pin(futures::stream::iter(vec![10, 20, 30]));
    let result = actor
        .reduce::<i64, i64>(input, 16, None, None)
        .unwrap()
        .await
        .unwrap();
    assert_eq!(result, 60, "feed sum: expected 60, got {}", result);
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Lifecycle
// ══════════════════════════════════════════════════════

/// Test: on_start runs before message handling, on_stop runs after stop.
pub async fn test_lifecycle_ordering<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceLifecycle>,
    F: FnOnce(&'static str, Arc<Mutex<Vec<String>>>) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let log = Arc::new(Mutex::new(Vec::new()));
    let actor = spawn("conf-lifecycle", log.clone()).await.unwrap();

    // Give on_start time to run
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Send a message to confirm the actor is alive after start
    actor.tell(LifecyclePing).unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;

    actor.stop();
    tokio::time::sleep(Duration::from_millis(100)).await;

    let entries = log.lock().unwrap();
    assert!(
        entries.contains(&"start".to_string()),
        "lifecycle log should contain 'start', got {:?}",
        *entries
    );
    assert!(
        entries.contains(&"stop".to_string()),
        "lifecycle log should contain 'stop', got {:?}",
        *entries
    );
    let start_idx = entries.iter().position(|e| e == "start").unwrap();
    let stop_idx = entries.iter().position(|e| e == "stop").unwrap();
    assert!(
        start_idx < stop_idx,
        "start (idx {}) should come before stop (idx {})",
        start_idx,
        stop_idx
    );
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Cancellation
// ══════════════════════════════════════════════════════

/// Test: ask with a pre-cancelled token returns an error.
pub async fn test_cancel_ask<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let token = CancellationToken::new();
    token.cancel();
    let actor = spawn("conf-cancel-counter", 0).await.unwrap();
    let result = actor.ask(GetCount, Some(token)).unwrap().await;
    assert!(result.is_err(), "ask with cancelled token should fail");
    // Pre-cancelled token should produce Cancelled error specifically
    assert!(
        matches!(result.unwrap_err(), RuntimeError::Cancelled),
        "expected RuntimeError::Cancelled for pre-cancelled token"
    );
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Error Recovery
// ══════════════════════════════════════════════════════

/// Test: actor with ErrorAction::Resume survives a panic and processes the next message.
pub async fn test_on_error_resume<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceResumeActor>,
    F: FnOnce(&'static str, Arc<AtomicU64>) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let count = Arc::new(AtomicU64::new(0));
    let actor = spawn("conf-resume", count.clone()).await.unwrap();

    // This panics inside the handler, but Resume keeps the actor alive.
    actor.tell(PanicMsg).unwrap();
    // This should still be delivered after the panic is recovered.
    actor.tell(CountMsg).unwrap();

    tokio::time::sleep(Duration::from_millis(200)).await;

    assert_eq!(
        count.load(Ordering::SeqCst),
        1,
        "actor should have processed CountMsg after recovering from panic"
    );
    assert!(
        actor.is_alive(),
        "actor with Resume policy should still be alive"
    );
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Batched Streaming
// ══════════════════════════════════════════════════════

/// Test: stream with BatchConfig collects all items correctly.
pub async fn test_batched_stream<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceStreamer>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-batch-stream", ()).await.unwrap();
    let batch = BatchConfig::new(3, Duration::from_millis(50));
    let stream = actor
        .expand(StreamNumbers { count: 7 }, 16, Some(batch), None)
        .unwrap();
    let items: Vec<u64> = stream.collect().await;
    assert_eq!(
        items,
        vec![0, 1, 2, 3, 4, 5, 6],
        "batched stream should deliver all 7 items in order"
    );
}

/// Test: feed with BatchConfig produces correct aggregated result.
pub async fn test_batched_feed<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceAggregator>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-batch-feed", ()).await.unwrap();
    let batch = BatchConfig::new(3, Duration::from_millis(50));
    let input: BoxStream<i64> = Box::pin(futures::stream::iter(vec![1, 2, 3, 4, 5]));
    let result = actor
        .reduce::<i64, i64>(input, 16, Some(batch), None)
        .unwrap()
        .await
        .unwrap();
    assert_eq!(result, 15, "batched feed: expected 15, got {}", result);
}

/// Test: stream with batch_config=None works the same as unbatched.
pub async fn test_stream_with_none_batch<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceStreamer>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-stream-none-batch", ()).await.unwrap();
    let stream = actor
        .expand(StreamNumbers { count: 6 }, 16, None, None)
        .unwrap();
    let items: Vec<u64> = stream.collect().await;
    assert_eq!(
        items,
        vec![0, 1, 2, 3, 4, 5],
        "unbatched stream (None) should deliver all 6 items"
    );
}

/// Test: feed with batch_config=None produces correct result.
pub async fn test_feed_with_none_batch<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceAggregator>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-feed-none-batch", ()).await.unwrap();
    let input: BoxStream<i64> = Box::pin(futures::stream::iter(vec![5, 10, 15, 20]));
    let result = actor
        .reduce::<i64, i64>(input, 16, None, None)
        .unwrap()
        .await
        .unwrap();
    assert_eq!(result, 50, "unbatched feed: expected 50, got {}", result);
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Post-Stop Errors
// ══════════════════════════════════════════════════════

/// Test: tell() on a stopped actor returns ActorSendError.
pub async fn test_tell_after_stop<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-tell-after-stop", 0).await.unwrap();
    actor.stop();
    // Poll until actor is stopped (with timeout)
    for _ in 0..50 {
        if !actor.is_alive() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert!(!actor.is_alive(), "actor should be stopped");

    let result: Result<(), ActorSendError> = actor.tell(Increment(1));
    assert!(
        result.is_err(),
        "tell on stopped actor should return ActorSendError"
    );
}

/// Test: ask() on a stopped actor returns an error.
pub async fn test_ask_after_stop<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-ask-after-stop", 0).await.unwrap();
    actor.stop();
    // Poll until actor is stopped (with timeout)
    for _ in 0..50 {
        if !actor.is_alive() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert!(!actor.is_alive(), "actor should be stopped");

    let result = actor.ask(GetCount, None);
    assert!(
        result.is_err(),
        "ask on stopped actor should return ActorSendError"
    );
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Multiple Handlers
// ══════════════════════════════════════════════════════

/// Test: actor implementing Handler for two different message types works via the same ref.
pub async fn test_multiple_handlers<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceMultiHandler>,
    F: FnOnce(&'static str, i64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("conf-multi-handler", 10).await.unwrap();

    // AddValue: 10 + 5 = 15
    let v1 = actor.ask(AddValue(5), None).unwrap().await.unwrap();
    assert_eq!(v1, 15, "after add: expected 15, got {}", v1);

    // MulValue: 15 * 3 = 45
    let v2 = actor.ask(MulValue(3), None).unwrap().await.unwrap();
    assert_eq!(v2, 45, "after mul: expected 45, got {}", v2);

    // AddValue again: 45 + 5 = 50
    let v3 = actor.ask(AddValue(5), None).unwrap().await.unwrap();
    assert_eq!(v3, 50, "after second add: expected 50, got {}", v3);
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Corner-Case Edge Tests
// ══════════════════════════════════════════════════════

/// Test: send 100 ask(Increment) in sequence with values 1..=100, verify FIFO ordering.
/// Each ask returns the reply after the increment, and we verify the running total
/// matches the expected sum at each step — this detects reordering since addition
/// of different values in wrong order produces wrong intermediate results.
pub async fn test_message_ordering_under_load<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("ordering-load", 0).await.unwrap();
    let mut expected_sum = 0u64;
    for i in 1..=100u64 {
        expected_sum += i;
        actor.ask(Increment(i), None).unwrap().await.unwrap();
        // Verify running total after each increment to detect reordering
        let count = actor.ask(GetCount, None).unwrap().await.unwrap();
        assert_eq!(
            count, expected_sum,
            "ordering: after Increment({}), expected {}, got {}",
            i, expected_sum, count
        );
    }
}

/// Test: concurrent asks from 10 tasks don't corrupt actor state.
/// Each task sends 10 ask(GetCount) messages and verifies replies are non-negative
/// and monotonically non-decreasing within each task.
pub async fn test_concurrent_asks<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("concurrent-asks", 0).await.unwrap();

    // Send some increments so GetCount returns increasing values
    for i in 1..=50u64 {
        actor.tell(Increment(i)).unwrap();
    }

    let mut handles = Vec::new();
    for _ in 0..10 {
        let actor_clone = actor.clone();
        handles.push(tokio::spawn(async move {
            let mut prev = 0u64;
            for _ in 0..10 {
                let count = actor_clone.ask(GetCount, None).unwrap().await.unwrap();
                assert!(
                    count >= prev,
                    "concurrent asks: count went backwards ({} < {})",
                    count,
                    prev
                );
                prev = count;
            }
        }));
    }
    for h in handles {
        h.await.expect("concurrent ask task panicked");
    }
}

/// Test: stream with small buffer (backpressure) and a slow consumer.
/// Starts a stream of 20 items with buffer=2, sleeps 10ms between each item,
/// and verifies all 20 items arrive in order.
pub async fn test_stream_slow_consumer<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceStreamer>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("stream-slow", ()).await.unwrap();
    let mut stream = actor
        .expand(StreamNumbers { count: 20 }, 2, None, None)
        .unwrap();

    let mut items = Vec::new();
    while let Some(item) = stream.next().await {
        items.push(item);
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    let expected: Vec<u64> = (0..20).collect();
    assert_eq!(
        items, expected,
        "slow consumer: expected 0..20 in order, got {:?}",
        items
    );
}

/// Test: spawn, send 5 messages, ask for count, stop, then verify post-stop sends fail.
/// Validates that messages before stop are processed and post-stop sends return errors.
pub async fn test_rapid_stop_and_send<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceCounter>,
    F: FnOnce(&'static str, u64) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    let actor = spawn("rapid-stop", 0).await.unwrap();

    // Send 5 messages and ask for the count before stopping
    for _ in 0..5 {
        actor.tell(Increment(1)).unwrap();
    }
    let count = actor.ask(GetCount, None).unwrap().await.unwrap();
    assert_eq!(
        count, 5,
        "rapid stop: expected 5 messages processed before stop, got {}",
        count
    );

    actor.stop();
    // Poll until actor is stopped
    for _ in 0..50 {
        if !actor.is_alive() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert!(!actor.is_alive(), "actor should be stopped");

    // Post-stop tell should fail
    let tell_result = actor.tell(Increment(1));
    assert!(tell_result.is_err(), "tell after stop should return error");

    // Post-stop ask should fail
    let ask_result = actor.ask(GetCount, None);
    assert!(ask_result.is_err(), "ask after stop should return error");
}

// ══════════════════════════════════════════════════════
// Transform Actor Definitions
// ══════════════════════════════════════════════════════

/// Transform actor: doubles each input item.
pub struct ConformanceDoubler;

impl Actor for ConformanceDoubler {
    type Args = ();
    type Deps = ();
    fn create(_: (), _: ()) -> Self {
        Self
    }
}

#[async_trait]
impl TransformHandler<i32, i32> for ConformanceDoubler {
    async fn handle_transform(
        &mut self,
        item: i32,
        sender: &StreamSender<i32>,
        _ctx: &mut ActorContext,
    ) {
        let _ = sender.send(item * 2).await;
    }
}

// ══════════════════════════════════════════════════════
// Conformance Tests – Transform
// ══════════════════════════════════════════════════════

/// Test: transform doubles each item in a stream.
pub async fn test_transform_doubler<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceDoubler>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-doubler", ()).await.unwrap();
    let input: BoxStream<i32> = Box::pin(futures::stream::iter(vec![1, 2, 3, 4, 5]));
    let output: Vec<i32> = actor
        .transform::<i32, i32>(input, 8, None, None)
        .unwrap()
        .collect()
        .await;
    assert_eq!(
        output,
        vec![2, 4, 6, 8, 10],
        "transform doubler: expected doubled values"
    );
}

/// Test: transform with empty input produces empty output.
pub async fn test_transform_empty<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceDoubler>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-doubler-empty", ()).await.unwrap();
    let input: BoxStream<i32> = Box::pin(futures::stream::iter(Vec::<i32>::new()));
    let output: Vec<i32> = actor
        .transform::<i32, i32>(input, 8, None, None)
        .unwrap()
        .collect()
        .await;
    assert!(
        output.is_empty(),
        "transform empty: expected empty output, got {:?}",
        output
    );
}

/// Test: transform with BatchConfig delivers all items in order.
pub async fn test_transform_batched<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceDoubler>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-doubler-batched", ()).await.unwrap();
    let batch = BatchConfig::new(2, Duration::from_millis(50));
    let input: BoxStream<i32> = Box::pin(futures::stream::iter(vec![1, 2, 3, 4, 5]));
    let output: Vec<i32> = actor
        .transform::<i32, i32>(input, 8, Some(batch), None)
        .unwrap()
        .collect()
        .await;
    assert_eq!(
        output,
        vec![2, 4, 6, 8, 10],
        "batched transform: expected doubled values in order"
    );
}

/// Test: transform with batch_config=None works the same as unbatched.
pub async fn test_transform_with_none_batch<R, F, Fut>(spawn: F)
where
    R: ActorRef<ConformanceDoubler>,
    F: FnOnce(&'static str, ()) -> Fut,
    Fut: Future<Output = Result<R, RuntimeError>>,
{
    use tokio_stream::StreamExt;

    let actor = spawn("conf-doubler-none-batch", ()).await.unwrap();
    let input: BoxStream<i32> = Box::pin(futures::stream::iter(vec![10, 20, 30]));
    let output: Vec<i32> = actor
        .transform::<i32, i32>(input, 8, None, None)
        .unwrap()
        .collect()
        .await;
    assert_eq!(
        output,
        vec![20, 40, 60],
        "transform with None batch: expected doubled values"
    );
}