rsactor 0.16.0

A Simple and Efficient In-Process Actor Model Implementation for 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
// Copyright 2022 Jeff Kim <hiking90@gmail.com>
// SPDX-License-Identifier: Apache-2.0

//! Tests for blocking methods when no tokio runtime is active.
//! These tests verify that tell_blocking and ask_blocking can create
//! their own runtime when called outside of any tokio context.

use std::time::Duration;

use rsactor::{spawn, Actor, ActorRef, Message};

// Test Actor for runtime-less environment tests
struct RuntimelessTestActor {
    counter: i32,
}

impl Actor for RuntimelessTestActor {
    type Args = i32;
    type Error = anyhow::Error;
    type IdleEvent = ();

    async fn on_start(args: Self::Args, _actor_ref: &ActorRef<Self>) -> Result<Self, Self::Error> {
        Ok(Self { counter: args })
    }
}

// Test messages
#[derive(Debug)]
struct IncrementMsg(i32);

#[derive(Debug)]
struct GetCounterMsg;

impl Message<IncrementMsg> for RuntimelessTestActor {
    type Reply = ();
    async fn handle(&mut self, msg: IncrementMsg, _: &ActorRef<Self>) -> Self::Reply {
        self.counter += msg.0;
    }
}

impl Message<GetCounterMsg> for RuntimelessTestActor {
    type Reply = i32;
    async fn handle(&mut self, _msg: GetCounterMsg, _: &ActorRef<Self>) -> Self::Reply {
        self.counter
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn test_tell_blocking_without_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test blocking call from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Calling blocking_tell from std::thread without runtime context...");
        let result = actor_ref_clone.blocking_tell(IncrementMsg(42), None);
        println!("blocking_tell result: {:?}", result);
        result
    })
    .join()
    .expect("Thread should not panic");

    assert!(
        thread_result.is_ok(),
        "blocking_tell should succeed without existing runtime: {:?}",
        thread_result
    );

    // Allow time for message processing
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify the message was processed
    let final_counter = actor_ref
        .ask(GetCounterMsg)
        .await
        .expect("Failed to get counter");
    println!("Final counter value: {}", final_counter);
    assert_eq!(final_counter, 42);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_ask_blocking_without_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(100);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test blocking call from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Calling ask_blocking from std::thread without runtime context...");
        // let result = actor_ref_clone.blocking_tell(IncrementMsg(100));
        let result = actor_ref_clone.blocking_ask(GetCounterMsg, None);
        println!("ask_blocking result: {:?}", result);
        result
    })
    .join()
    .expect("Thread should not panic");

    match thread_result {
        Ok(value) => {
            assert_eq!(value, 100, "Should get initial counter value");
        }
        Err(e) => {
            panic!(
                "ask_blocking should succeed without existing runtime: {:?}",
                e
            );
        }
    }

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_multiple_blocking_calls_without_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test multiple blocking calls in sequence from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Testing multiple blocking calls from std::thread without runtime...");

        // First increment
        let result1 = actor_ref_clone.blocking_tell(IncrementMsg(10), None);
        if result1.is_err() {
            return Err(format!("First blocking_tell failed: {:?}", result1));
        }

        // Get current value (this should work even though we just did tell)
        let result2 = actor_ref_clone.blocking_ask(GetCounterMsg, None);
        if result2.is_err() {
            return Err(format!("blocking_ask failed: {:?}", result2));
        }

        // Second increment
        let result3 = actor_ref_clone.blocking_tell(IncrementMsg(5), None);
        if result3.is_err() {
            return Err(format!("Second blocking_tell failed: {:?}", result3));
        }

        // Final value check
        let final_result = actor_ref_clone.blocking_ask(GetCounterMsg, None);
        match final_result {
            Ok(value) => {
                if value == 15 {
                    Ok(value)
                } else {
                    Err(format!(
                        "Final counter should be 15 (10 + 5), got: {}",
                        value
                    ))
                }
            }
            Err(e) => Err(format!("Final blocking_ask failed: {:?}", e)),
        }
    })
    .join()
    .expect("Thread should not panic");

    match thread_result {
        Ok(final_value) => {
            assert_eq!(final_value, 15, "Final counter should be 15 (10 + 5)");
        }
        Err(error_msg) => {
            panic!("{}", error_msg);
        }
    }

    // Verify final state from the tokio context as well
    let final_counter = actor_ref
        .ask(GetCounterMsg)
        .await
        .expect("Failed to get counter");
    assert_eq!(final_counter, 15);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_calls_without_timeout_and_without_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(50);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test blocking calls without timeout from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Testing blocking calls without timeout from std::thread without runtime...");

        // blocking_tell without timeout
        let result1 = actor_ref_clone.blocking_tell(IncrementMsg(25), None);
        if result1.is_err() {
            return Err(format!("blocking_tell failed: {:?}", result1));
        }

        // blocking_ask without timeout
        let result2 = actor_ref_clone.blocking_ask(GetCounterMsg, None);
        match result2 {
            Ok(value) => {
                if value == 75 {
                    Ok(value)
                } else {
                    Err(format!("Counter should be 75 (50 + 25), got: {}", value))
                }
            }
            Err(e) => Err(format!("blocking_ask failed: {:?}", e)),
        }
    })
    .join()
    .expect("Thread should not panic");

    match thread_result {
        Ok(final_value) => {
            assert_eq!(final_value, 75, "Counter should be 75 (50 + 25)");
        }
        Err(error_msg) => {
            panic!("{}", error_msg);
        }
    }

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_tell_with_timeout() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test blocking_tell with timeout from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Calling blocking_tell with timeout from std::thread without runtime context...");
        let result = actor_ref_clone.blocking_tell(IncrementMsg(42), Some(Duration::from_secs(5)));
        println!("blocking_tell with timeout result: {:?}", result);
        result
    })
    .join()
    .expect("Thread should not panic");

    assert!(
        thread_result.is_ok(),
        "blocking_tell with timeout should succeed: {:?}",
        thread_result
    );

    // Allow time for message processing
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Verify the message was processed
    let final_counter = actor_ref
        .ask(GetCounterMsg)
        .await
        .expect("Failed to get counter");
    println!("Final counter value: {}", final_counter);
    assert_eq!(final_counter, 42);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_ask_with_timeout() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(100);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test blocking_ask with timeout from std::thread (no tokio context)
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        println!("Calling blocking_ask with timeout from std::thread without runtime context...");
        let result = actor_ref_clone.blocking_ask(GetCounterMsg, Some(Duration::from_secs(5)));
        println!("blocking_ask with timeout result: {:?}", result);
        result
    })
    .join()
    .expect("Thread should not panic");

    match thread_result {
        Ok(value) => {
            assert_eq!(value, 100, "Should get initial counter value");
        }
        Err(e) => {
            panic!(
                "blocking_ask with timeout should succeed without existing runtime: {:?}",
                e
            );
        }
    }

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_with_timeout_inside_tokio_context() {
    // Test that blocking methods with timeout work even inside tokio spawn_blocking
    // This tests the separate thread + runtime approach
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Give actor time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    let actor_ref_clone = actor_ref.clone();
    let join_handle = tokio::task::spawn_blocking(move || {
        // blocking_tell with timeout inside spawn_blocking
        let tell_result =
            actor_ref_clone.blocking_tell(IncrementMsg(10), Some(Duration::from_secs(5)));
        assert!(
            tell_result.is_ok(),
            "blocking_tell with timeout should succeed: {:?}",
            tell_result
        );

        // blocking_ask with timeout inside spawn_blocking
        let ask_result = actor_ref_clone.blocking_ask(GetCounterMsg, Some(Duration::from_secs(5)));
        assert!(
            ask_result.is_ok(),
            "blocking_ask with timeout should succeed: {:?}",
            ask_result
        );

        ask_result.unwrap()
    });

    let counter_value = join_handle.await.expect("Blocking task panicked");
    assert_eq!(counter_value, 10, "Counter should be 10 after increment");

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_ask_timeout_on_stopped_actor() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Stop the actor first
    actor_ref.stop().await;
    handle.await.expect("Actor task failed");

    // Test blocking_ask with timeout on stopped actor
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        actor_ref_clone.blocking_ask(GetCounterMsg, Some(Duration::from_secs(1)))
    })
    .join()
    .expect("Thread should not panic");

    assert!(
        thread_result.is_err(),
        "blocking_ask on stopped actor should fail"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_tell_timeout_on_stopped_actor() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // Stop the actor first
    actor_ref.stop().await;
    handle.await.expect("Actor task failed");

    // Test blocking_tell with timeout on stopped actor
    let actor_ref_clone = actor_ref.clone();
    let thread_result = std::thread::spawn(move || {
        actor_ref_clone.blocking_tell(IncrementMsg(1), Some(Duration::from_secs(1)))
    })
    .join()
    .expect("Thread should not panic");

    assert!(
        thread_result.is_err(),
        "blocking_tell on stopped actor should fail"
    );
}

// The following tests cover the `async fn -> sync fn -> blocking_*` bridge.
// On a multi-thread runtime the blocking_* APIs detect the current runtime
// and use `block_in_place` + `Handle::block_on` to reuse it. Before that
// change, calling `blocking_tell(_, None)` directly from an async context
// would panic because tokio's `blocking_send` rejects async contexts.

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_tell_from_async_multi_thread_context() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);

    // No timeout: this would panic on the old code path because it called
    // `blocking_send` directly. The block_in_place fast path makes it safe.
    actor_ref
        .blocking_tell(IncrementMsg(7), None)
        .expect("blocking_tell (no timeout) from async ctx should succeed");

    // With timeout: takes the same fast path; verify it still works.
    actor_ref
        .blocking_tell(IncrementMsg(3), Some(Duration::from_secs(5)))
        .expect("blocking_tell (timeout) from async ctx should succeed");

    let counter = actor_ref
        .ask(GetCounterMsg)
        .await
        .expect("Failed to get counter");
    assert_eq!(counter, 10);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_blocking_ask_from_async_multi_thread_context() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(42);

    // No timeout: previously would panic from async ctx. Fast path makes it safe.
    let val = actor_ref
        .blocking_ask(GetCounterMsg, None)
        .expect("blocking_ask (no timeout) from async ctx should succeed");
    assert_eq!(val, 42);

    let val = actor_ref
        .blocking_ask(GetCounterMsg, Some(Duration::from_secs(5)))
        .expect("blocking_ask (timeout) from async ctx should succeed");
    assert_eq!(val, 42);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

// On a current_thread runtime, the block_in_place fast path is NOT eligible
// (block_in_place requires multi_thread). The blocking_* APIs must therefore
// fall through to the spawn-thread + new-runtime path. These tests pin that
// behaviour by invoking from `tokio::task::spawn_blocking` so that the test
// task yields and the runtime's sole thread is free to drive the actor.
// Calling the blocking_* APIs directly from `async fn` (or via a synchronous
// `std::thread::spawn(...).join()`) on a current_thread runtime would
// deadlock — the single runtime thread would be parked while the actor
// needed it to make progress. This is a fundamental property of
// current_thread runtimes, not a regression.
// `timeout: None` is omitted here because the fallback uses `blocking_send`
// directly which still panics in async context.

#[tokio::test(flavor = "current_thread")]
async fn test_blocking_tell_with_timeout_on_current_thread_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(0);
    tokio::time::sleep(Duration::from_millis(50)).await;

    let actor_ref_clone = actor_ref.clone();
    tokio::task::spawn_blocking(move || {
        actor_ref_clone.blocking_tell(IncrementMsg(11), Some(Duration::from_secs(5)))
    })
    .await
    .expect("spawn_blocking task should not panic")
    .expect("blocking_tell with timeout on current_thread runtime should succeed");

    tokio::time::sleep(Duration::from_millis(50)).await;
    let counter = actor_ref
        .ask(GetCounterMsg)
        .await
        .expect("Failed to get counter");
    assert_eq!(counter, 11);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}

#[tokio::test(flavor = "current_thread")]
async fn test_blocking_ask_with_timeout_on_current_thread_runtime() {
    let (actor_ref, handle) = spawn::<RuntimelessTestActor>(13);
    tokio::time::sleep(Duration::from_millis(50)).await;

    let actor_ref_clone = actor_ref.clone();
    let val = tokio::task::spawn_blocking(move || {
        actor_ref_clone.blocking_ask(GetCounterMsg, Some(Duration::from_secs(5)))
    })
    .await
    .expect("spawn_blocking task should not panic")
    .expect("blocking_ask with timeout on current_thread runtime should succeed");
    assert_eq!(val, 13);

    actor_ref.stop().await;
    handle.await.expect("Actor task failed");
}