acktor 1.0.8

Pure-Rust actor framework built on top of the Tokio async runtime
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
//! Traits and type definitions for the address of actor model.
//!
//! In the actor model, an [`Address`] is a handle to an actor. It is the only way to interact
//! with an actor since the runtime will take the ownership of the actor itself after it is
//! spawned.
//!
//! This modules defines the [`Address`] type for an actor. It also provides the [`Sender`] trait
//! and a [`Recipient`] type which are alternative ways to organize the addresses of actors.
//!

mod address_impl;
pub use address_impl::Address;

mod permit;
pub use permit::{OwnedSendPermit, SendPermit};

mod sender;
pub use sender::{
    ClosedResultFuture, DoSendResult, DoSendResultFuture, SendResult, SendResultFuture, Sender,
    SenderId,
};

mod recipient;
pub use recipient::Recipient;

mod mailbox;
pub use mailbox::Mailbox;

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use pretty_assertions::assert_eq;
    use tokio::time::{Duration, timeout};

    use super::*;
    use crate::channel::mpsc;
    use crate::envelope::Envelope;
    use crate::errors::SendError;
    use crate::test_utils::{Dummy, Ping};

    fn make_address(capacity: usize) -> (Address<Dummy>, Mailbox<Dummy>) {
        let (tx, rx) = mpsc::channel::<Envelope<Dummy>>(capacity);
        (Address::new(tx), Mailbox::new(rx))
    }

    #[tokio::test]
    async fn test_address() {
        // clone + eq
        let (a1, m1) = make_address(4);
        let clone = a1.clone();
        assert_eq!(a1, clone);
        assert_eq!(a1.index(), clone.index());

        let debug_str = format!("{a1:?}");
        assert_eq!(debug_str, format!("Address<Dummy>({})", a1.index()));

        let debug_str = format!("{m1:?}");
        assert_eq!(debug_str, "Mailbox<Dummy>");

        // hash
        #[allow(clippy::mutable_key_type)]
        let mut map = HashSet::new();
        map.insert(a1);
        map.insert(clone);
        assert_eq!(
            map.len(),
            1,
            "clones should have the same hash and be equal"
        );

        // index is unique
        let (a1, m1) = make_address(4);
        let (a2, _) = make_address(4);
        assert_ne!(a1, a2);
        assert_ne!(a1.index(), a2.index());

        // capacity + is_closed + closed
        assert_eq!(a1.capacity(), 4);
        assert!(!a1.is_closed());

        let closed = a1.closed();
        drop(m1);
        assert!(a1.is_closed());
        timeout(Duration::from_millis(500), closed)
            .await
            .expect("closed() did not resolve after mailbox drop");

        // do_send
        let (a1, mut m1) = make_address(1);
        a1.do_send(Ping(1)).await.expect("do_send should succeed");
        assert_eq!(m1.len(), 1);
        m1.recv().await.expect("recv should succeed");

        // send
        a1.send(Ping(2)).await.expect("send should succeed");
        m1.recv().await.expect("recv should succeed");

        // try_do_send
        let (a1, m1) = make_address(1);
        a1.try_do_send(Ping(3)).expect("first message fits");
        assert_eq!(m1.len(), 1);
        let result = a1.try_do_send(Ping(4));
        assert!(
            matches!(result, Err(SendError::Full(_))),
            "expected Full, got {result:?}"
        );
        drop(m1);
        let result = a1.try_do_send(Ping(5));
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );

        // try_send
        let (a1, m1) = make_address(1);
        a1.try_send(Ping(6)).expect("first message fits");
        assert_eq!(m1.len(), 1);
        let result = a1.try_send(Ping(7));
        assert!(
            matches!(result, Err(SendError::Full(_))),
            "expected Full, got {result:?}"
        );
        drop(m1);
        let result = a1.try_send(Ping(8));
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );

        // do_send_timeout
        let (a1, m1) = make_address(1);
        a1.do_send_timeout(Ping(9), Duration::from_millis(10))
            .await
            .expect("first message fits");
        assert_eq!(m1.len(), 1);
        let result = a1
            .do_send_timeout(Ping(10), Duration::from_millis(10))
            .await;
        assert!(
            matches!(result, Err(SendError::Timeout(_))),
            "expected Timeout, got {result:?}"
        );
        drop(m1);
        let result = a1
            .do_send_timeout(Ping(11), Duration::from_millis(10))
            .await;
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );

        // send_timeout
        let (a1, m1) = make_address(1);
        a1.send_timeout(Ping(12), Duration::from_millis(10))
            .await
            .expect("first message fits");
        assert_eq!(m1.len(), 1);
        let result = a1.send_timeout(Ping(13), Duration::from_millis(10)).await;
        assert!(
            matches!(result, Err(SendError::Timeout(_))),
            "expected Timeout, got {result:?}"
        );
        drop(m1);
        let result = a1.send_timeout(Ping(14), Duration::from_millis(10)).await;
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );

        // blocking_do_send
        let (a1, mut m1) = make_address(1);
        tokio::task::spawn_blocking(move || {
            a1.blocking_do_send(Ping(15)).expect("first message fits");
            assert_eq!(m1.len(), 1);
            m1.try_recv().expect("recv should succeed");
        })
        .await
        .expect("spawn_blocking join");

        // blocking_send
        let (a1, mut m1) = make_address(1);
        tokio::task::spawn_blocking(move || {
            a1.blocking_send(Ping(16)).expect("first message fits");
            assert_eq!(m1.len(), 1);
            m1.try_recv().expect("recv should succeed");
        })
        .await
        .expect("spawn_blocking join");
    }

    #[tokio::test]
    async fn test_mailbox() {
        // basic properties
        let (a1, mut m1) = make_address(4);
        assert!(m1.is_empty());
        assert_eq!(m1.len(), 0);
        assert_eq!(m1.capacity(), 4);
        assert_eq!(m1.max_capacity(), 4);
        assert!(m1.try_recv().is_err());
        assert!(!m1.is_closed());

        // len reflects pending messages
        a1.try_do_send(Ping(1)).unwrap();
        a1.try_do_send(Ping(2)).unwrap();
        assert_eq!(m1.len(), 2);
        assert!(!m1.is_empty());

        // close() propagates to the address and rejects further sends
        assert!(!a1.is_closed());
        m1.close();
        assert!(a1.is_closed());
        let result = a1.try_do_send(Ping(3));
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );
    }

    #[tokio::test]
    async fn test_recipient() {
        // create() delivers to the receiver
        let (recipient, mut rx) = Recipient::<Ping>::create(4);
        recipient
            .do_send(Ping(1))
            .await
            .expect("do_send should succeed");
        let msg = rx.recv().await.expect("recv should succeed");
        assert_eq!(msg.0, 1);

        let debug_str = format!("{recipient:?}");
        assert_eq!(debug_str, format!("Recipient<Ping>({})", recipient.index()));

        // clone preserves identity
        let clone = recipient.clone();
        assert_eq!(recipient, clone);
        assert_eq!(recipient.index(), clone.index());

        // capacity + is_closed + closed
        assert!(!recipient.is_closed());
        assert_eq!(recipient.capacity(), 4);
        drop(rx);
        assert!(recipient.is_closed());
        timeout(Duration::from_millis(500), recipient.closed())
            .await
            .expect("closed() should resolve after receiver drop");

        // send functions with create() recipient
        let (recipient, rx) = Recipient::<Ping>::create(8);
        recipient.send(Ping(2)).await.expect("send should succeed");
        recipient
            .do_send(Ping(3))
            .await
            .expect("do_send should succeed");
        recipient
            .try_send(Ping(4))
            .expect("try_send should succeed");
        recipient
            .try_do_send(Ping(5))
            .expect("try_do_send should succeed");
        recipient
            .send_timeout(Ping(6), Duration::from_millis(10))
            .await
            .expect("send_timeout should succeed");
        recipient
            .do_send_timeout(Ping(7), Duration::from_millis(10))
            .await
            .expect("do_send_timeout should succeed");
        tokio::task::spawn_blocking(move || {
            recipient
                .blocking_send(Ping(8))
                .expect("blocking_send should succeed");
            recipient
                .blocking_do_send(Ping(9))
                .expect("blocking_do_send should succeed");
        })
        .await
        .expect("spawn_blocking join");
        assert_eq!(rx.len(), 8);

        // From<Address> preserves index
        let (a1, m1) = make_address(8);
        let index = a1.index();
        let recipient: Recipient<Ping> = a1.into();
        assert_eq!(recipient.index(), index);

        let clone = recipient.clone();
        assert_eq!(recipient, clone);
        assert_eq!(recipient.index(), clone.index());

        // send functions with From<Address> recipient
        recipient.send(Ping(10)).await.expect("send should succeed");
        recipient
            .do_send(Ping(11))
            .await
            .expect("do_send should succeed");
        recipient
            .try_send(Ping(12))
            .expect("try_send should succeed");
        recipient
            .try_do_send(Ping(13))
            .expect("try_do_send should succeed");
        recipient
            .send_timeout(Ping(14), Duration::from_millis(10))
            .await
            .expect("send_timeout should succeed");
        recipient
            .do_send_timeout(Ping(15), Duration::from_millis(10))
            .await
            .expect("do_send_timeout should succeed");
        tokio::task::spawn_blocking(move || {
            recipient
                .blocking_send(Ping(16))
                .expect("blocking_send should succeed");
            recipient
                .blocking_do_send(Ping(17))
                .expect("blocking_do_send should succeed");
        })
        .await
        .expect("spawn_blocking join");
        assert_eq!(m1.len(), 8);

        assert!(!clone.is_closed());
        drop(m1);
        assert!(clone.is_closed());
        timeout(Duration::from_millis(100), clone.closed())
            .await
            .expect("closed() should resolve after mailbox drop");
    }

    #[tokio::test]
    async fn test_permits() {
        // reserve
        let (a1, m1) = make_address(2);
        let permit = a1.reserve().await.expect("reserve should succeed");
        permit.do_send(Ping(1));
        let permit = a1.try_reserve().expect("try_reserve should succeed");
        permit.send(Ping(2));
        assert_eq!(m1.len(), 2);

        // capacity
        let (a1, m1) = make_address(2);
        let p1 = a1.reserve().await.expect("first reserve should succeed");
        let _p2 = a1.reserve().await.expect("second reserve should succeed");
        let result = a1.try_reserve();
        assert!(
            matches!(result, Err(SendError::Full(_))),
            "expected Full, got {result:?}"
        );

        // drop a permit releases the slot
        drop(p1);
        let _ = a1.try_reserve().expect("try_reserve should succeed");

        // close
        drop(m1);
        let result = a1.try_reserve();
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );

        // reserve_owned
        let (a1, m1) = make_address(2);
        let permit = a1
            .reserve_owned()
            .await
            .expect("reserve_owned should succeed");
        permit.do_send(Ping(2));
        let permit = a1
            .try_reserve_owned()
            .expect("try_reserve_owned should succeed");
        permit.send(Ping(3));
        assert_eq!(m1.len(), 2);

        // capacity
        let (a1, m1) = make_address(2);
        let p1 = a1
            .reserve_owned()
            .await
            .expect("first reserve should succeed");
        let _p2 = a1
            .reserve_owned()
            .await
            .expect("second reserve should succeed");
        let result = a1.try_reserve_owned();
        assert!(
            matches!(result, Err(SendError::Full(_))),
            "expected Full, got {result:?}"
        );

        // drop a permit releases the slot
        drop(p1);
        let _ = a1
            .try_reserve_owned()
            .expect("try_reserve_owned should succeed");

        // close
        drop(m1);
        let result = a1.try_reserve_owned();
        assert!(
            matches!(result, Err(SendError::Closed(_))),
            "expected Closed, got {result:?}"
        );
    }

    #[test]
    fn test_sender() {
        let sender_id = u64::MAX;
        assert_eq!(sender_id.index(), u64::MAX);
        #[cfg(feature = "ipc")]
        assert_eq!(sender_id.is_remote(), true);
    }
}