runact 1.3.2

A Rust-native actor runtime with BEAM-style processes
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
use runact::{Actor, ActorContext, ActorError, Runtime, RuntimeConfig, RuntimeError};
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// Simple counter actor for testing.
struct CounterActor {
    count: u64,
}

enum CounterMessage {
    Increment,
    _Decrement,
    GetValue,
}

impl Actor for CounterActor {
    type Message = CounterMessage;

    fn handle(&mut self, msg: CounterMessage, ctx: &mut ActorContext) -> Result<(), ActorError> {
        match msg {
            CounterMessage::Increment => {
                self.count += 1;
            }
            CounterMessage::_Decrement => {
                self.count = self.count.saturating_sub(1);
            }
            CounterMessage::GetValue => {
                let _ = ctx.reply(self.count);
            }
        }
        Ok(())
    }
}

/// Actor that tracks received messages.
struct LoggerActor {
    messages: Arc<Mutex<Vec<String>>>,
}

enum LoggerMessage {
    Log(String),
    GetCount,
}

impl Actor for LoggerActor {
    type Message = LoggerMessage;

    fn handle(&mut self, msg: LoggerMessage, ctx: &mut ActorContext) -> Result<(), ActorError> {
        match msg {
            LoggerMessage::Log(text) => {
                self.messages.lock().unwrap().push(text);
            }
            LoggerMessage::GetCount => {
                let count = self.messages.lock().unwrap().len();
                let _ = ctx.reply(count);
            }
        }
        Ok(())
    }
}

/// Actor that sends messages to another actor.
struct ForwardActor {
    target: Option<runact::ActorId>,
}

enum ForwardMessage {
    SetTarget(runact::ActorId),
    Forward(String),
}

impl Actor for ForwardActor {
    type Message = ForwardMessage;

    fn handle(&mut self, msg: ForwardMessage, ctx: &mut ActorContext) -> Result<(), ActorError> {
        match msg {
            ForwardMessage::SetTarget(id) => {
                self.target = Some(id);
            }
            ForwardMessage::Forward(text) => {
                if let Some(target) = self.target {
                    let _ = ctx.send_to(target, LoggerMessage::Log(text));
                }
            }
        }
        Ok(())
    }
}

#[test]
fn test_runtime_creation() {
    let runtime = Runtime::new();
    assert!(runtime.is_ok());
}

#[test]
fn test_actor_spawning() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let actor_id = runtime.spawn(CounterActor { count: 0 });
    assert!(actor_id.is_ok());
}

#[test]
fn test_multiple_actors() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let actor1 = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn actor1");
    let actor2 = runtime
        .spawn(CounterActor { count: 10 })
        .expect("Failed to spawn actor2");
    let actor3 = runtime
        .spawn(CounterActor { count: 100 })
        .expect("Failed to spawn actor3");

    assert_ne!(actor1, actor2);
    assert_ne!(actor2, actor3);
    assert_ne!(actor1, actor3);
}

#[test]
fn test_actor_listing() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let _ = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn actor");
    let _ = runtime
        .spawn(CounterActor { count: 10 })
        .expect("Failed to spawn actor");

    let actors = runtime.list_actors().expect("Failed to list actors");
    assert_eq!(actors.len(), 2);
}

#[test]
fn test_actor_inspection() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let actor_id = runtime
        .spawn(CounterActor { count: 42 })
        .expect("Failed to spawn actor");
    let info = runtime
        .inspect_actor(actor_id)
        .expect("Failed to inspect actor");

    assert!(info.is_some());
    let info = info.unwrap();
    assert_eq!(info.id, actor_id);
}

#[test]
fn test_runtime_shutdown() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let _ = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn actor");
    let _ = runtime
        .spawn(CounterActor { count: 10 })
        .expect("Failed to spawn actor");

    let result = runtime.shutdown();
    assert!(result.is_ok());
}

#[test]
fn test_actor_id_uniqueness() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let mut ids = Vec::new();
    for _ in 0..100 {
        let id = runtime
            .spawn(CounterActor { count: 0 })
            .expect("Failed to spawn actor");
        ids.push(id);
    }

    // Check all IDs are unique
    let unique_ids: std::collections::HashSet<_> = ids.iter().collect();
    assert_eq!(unique_ids.len(), 100);
}

// ============================================================
// Real message passing tests
// ============================================================

#[test]
fn test_send_fire_and_forget() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let counter = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn");

    // Send 5 increments
    for _ in 0..5 {
        runtime
            .send(counter, CounterMessage::Increment)
            .expect("Failed to send");
    }

    // Verify by requesting the value
    let handle = runtime
        .request(counter, CounterMessage::GetValue)
        .expect("Failed to request");
    let reply = handle
        .recv_timeout(Duration::from_secs(1))
        .expect("Failed to receive reply");
    let count = reply.downcast::<u64>().expect("Failed to downcast");
    assert_eq!(*count, 5);
}

#[test]
fn test_request_reply() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let counter = runtime
        .spawn(CounterActor { count: 42 })
        .expect("Failed to spawn");

    // Request current value
    let handle = runtime
        .request(counter, CounterMessage::GetValue)
        .expect("Failed to request");
    let reply = handle
        .recv_timeout(Duration::from_secs(1))
        .expect("Failed to receive reply");
    let count = reply.downcast::<u64>().expect("Failed to downcast");
    assert_eq!(*count, 42);
}

#[test]
fn test_request_blocking() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let counter = runtime
        .spawn(CounterActor { count: 99 })
        .expect("Failed to spawn");

    // Blocking request with timeout
    let count: u64 = runtime
        .request_blocking(
            counter,
            CounterMessage::GetValue,
            Some(Duration::from_secs(1)),
        )
        .expect("Failed to receive reply");
    assert_eq!(count, 99);
}

#[test]
fn test_actor_to_actor_communication() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");

    let messages: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let logger = runtime
        .spawn(LoggerActor {
            messages: messages.clone(),
        })
        .expect("Failed to spawn logger");

    let forwarder = runtime
        .spawn(ForwardActor { target: None })
        .expect("Failed to spawn forwarder");

    // Set the forwarder's target to the logger
    runtime
        .send(forwarder, ForwardMessage::SetTarget(logger))
        .expect("Failed to send");

    // Send through the forwarder
    runtime
        .send(forwarder, ForwardMessage::Forward("hello".to_string()))
        .expect("Failed to send");
    runtime
        .send(forwarder, ForwardMessage::Forward("world".to_string()))
        .expect("Failed to send");

    std::thread::sleep(Duration::from_millis(50));

    // Verify logger received both messages
    let handle = runtime
        .request(logger, LoggerMessage::GetCount)
        .expect("Failed to request");
    let reply = handle
        .recv_timeout(Duration::from_secs(1))
        .expect("Failed to receive reply");
    let count = reply.downcast::<usize>().expect("Failed to downcast");
    assert_eq!(*count, 2);

    let msgs = messages.lock().unwrap();
    assert_eq!(msgs.len(), 2);
    assert_eq!(msgs[0], "hello");
    assert_eq!(msgs[1], "world");
}

#[test]
fn test_request_to_nonexistent_actor() {
    let runtime = Runtime::new().expect("Failed to create runtime");
    let fake_id = runact::ActorId::new(999);

    let result = runtime.send(fake_id, "hello".to_string());
    assert!(result.is_err());
}

#[test]
fn test_backpressure_rejects_when_full() {
    struct SlowActor;

    impl Actor for SlowActor {
        type Message = u64;

        fn handle(&mut self, _msg: u64, _ctx: &mut ActorContext) -> Result<(), ActorError> {
            std::thread::sleep(Duration::from_millis(50));
            Ok(())
        }
    }

    let config = RuntimeConfig {
        mailbox_capacity: 2,
        ..RuntimeConfig::default()
    };
    let mut runtime = Runtime::with_config(config).expect("create runtime");
    let id = runtime.spawn(SlowActor).expect("spawn");

    std::thread::sleep(Duration::from_millis(30));

    let mut got_full = false;
    for _ in 0..20 {
        if let Err(RuntimeError::MailboxFull(_)) = runtime.send(id, 1u64) {
            got_full = true;
            break;
        }
    }
    assert!(got_full, "expected MailboxFull error");
}

#[test]
fn test_send_blocking_does_not_reject() {
    let config = RuntimeConfig {
        mailbox_capacity: 1,
        ..RuntimeConfig::default()
    };
    let mut runtime = Runtime::with_config(config).expect("create runtime");
    let id = runtime.spawn(CounterActor { count: 0 }).expect("spawn");

    for _ in 0..10 {
        let result = runtime.send_blocking(id, CounterMessage::Increment);
        assert!(result.is_ok());
    }
}

#[test]
fn test_multiple_requests_concurrent() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let counter = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn");

    // Send 10 increments
    for _ in 0..10 {
        runtime
            .send(counter, CounterMessage::Increment)
            .expect("Failed to send");
    }

    // Make 5 concurrent requests
    let mut handles = Vec::new();
    for _ in 0..5 {
        let handle = runtime
            .request(counter, CounterMessage::GetValue)
            .expect("Failed to request");
        handles.push(handle);
    }

    // All should return the same value (10)
    for handle in handles {
        let reply = handle
            .recv_timeout(Duration::from_secs(1))
            .expect("Failed to receive reply");
        let count = reply.downcast::<u64>().expect("Failed to downcast");
        assert_eq!(*count, 10);
    }
}

#[test]
fn test_sender_request_roundtrip() {
    let mut runtime = Runtime::new().expect("Failed to create runtime");
    let counter = runtime
        .spawn(CounterActor { count: 0 })
        .expect("Failed to spawn");

    runtime
        .send(counter, CounterMessage::Increment)
        .expect("Failed to send");

    // Clone the sender and drop the runtime borrow — request must work
    // through the sender alone (for threads/extensions that outlive &Runtime).
    let sender = runtime.sender();
    let handle = sender
        .request(counter, CounterMessage::GetValue)
        .expect("Failed to request via sender");
    let reply = handle
        .recv_timeout(Duration::from_secs(1))
        .expect("Failed to receive reply");
    let count = reply.downcast::<u64>().expect("Failed to downcast");
    assert_eq!(*count, 1);
}