liminal-rs 0.2.0

A conversation-based messaging bus built on beamr
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
//! LIM-002 behavioural tests for the process-backed channel actor.
//!
//! Every test runs against a dedicated [`ChannelSupervisor`] (its own beamr
//! scheduler) so they are isolated from the process-global default supervisor
//! and from each other. Delivery is performed synchronously inside the actor's
//! `Publish` handler before its reply is sent, so a message is observable on a
//! subscriber's inbox as soon as `publish` returns; lifecycle effects driven by
//! a process EXIT (subscriber death) are asynchronous and are awaited with a
//! bounded poll.

use std::error::Error;
use std::time::{Duration, Instant};

use beamr::process::ExitReason;
use serde_json::{Value, json};

use crate::channel::registry::ChannelRegistry;
use crate::channel::subscription::SubscriptionHandle;
use crate::channel::supervisor::{ChannelRestartPolicy, ChannelSupervisor};
use crate::channel::{ChannelConfig, ChannelHandle, ChannelMode, Schema, SchemaValidationError};
use crate::envelope::Envelope;
use crate::error::LiminalError;

fn order_schema() -> Result<Schema, SchemaValidationError> {
    Schema::new(json!({
        "type": "object",
        "properties": {
            "order_id": {"type": "string"},
            "quantity": {"type": "integer", "minimum": 1}
        },
        "required": ["order_id", "quantity"],
        "additionalProperties": false
    }))
}

fn order_channel(supervisor: &ChannelSupervisor) -> Result<ChannelHandle, Box<dyn Error>> {
    let config = ChannelConfig::new("orders".to_owned(), order_schema()?, ChannelMode::Ephemeral);
    Ok(ChannelHandle::with_supervisor(config, supervisor.clone()))
}

/// Bounded poll for the next delivered envelope (delivery is in-process and
/// immediate after `publish`, but a small wait absorbs scheduler hand-off).
fn await_envelope(subscription: &SubscriptionHandle) -> Result<Envelope, Box<dyn Error>> {
    let deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < deadline {
        if let Some(envelope) = subscription.try_next()? {
            return Ok(envelope);
        }
        std::thread::sleep(Duration::from_millis(5));
    }
    Err("expected a delivered envelope".into())
}

/// Bounded poll until `predicate` over the live subscriber count holds.
fn await_count<F>(handle: &ChannelHandle, predicate: F) -> Result<usize, Box<dyn Error>>
where
    F: Fn(usize) -> bool,
{
    let deadline = Instant::now() + Duration::from_secs(2);
    let mut last = handle.subscriber_count()?;
    while Instant::now() < deadline {
        last = handle.subscriber_count()?;
        if predicate(last) {
            return Ok(last);
        }
        std::thread::sleep(Duration::from_millis(5));
    }
    Err(format!("subscriber count {last} never satisfied predicate").into())
}

/// Crash the channel actor process `handle` is bound to with an abnormal exit
/// (the same `terminate_process(_, ExitReason::Error)` path the conversation
/// crash tests use) and block until that pid has actually left the scheduler's
/// process table, so the subsequent operation deterministically observes a dead
/// actor and drives a restart. Returns the crashed pid.
fn crash_actor(handle: &ChannelHandle) -> Result<u64, Box<dyn Error>> {
    let pid = handle.actor_pid()?;
    let scheduler = handle.scheduler()?;
    scheduler.terminate_process(pid, ExitReason::Error);
    let deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < deadline {
        if scheduler.process_table().get(pid).is_none() {
            return Ok(pid);
        }
        std::thread::sleep(Duration::from_millis(5));
    }
    Err(format!("channel actor pid {pid} never left the process table after crash").into())
}

#[test]
fn channel_restarts_after_crash_and_publish_succeeds() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;
    let original_pid = handle.actor_pid()?;

    // Crash the live actor, then publish: the supervisor must restart it.
    crash_actor(&handle)?;
    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;

    // (a) The channel came back on a NEW pid and the publish was delivered.
    let restarted_pid = handle.actor_pid()?;
    assert_ne!(
        original_pid, restarted_pid,
        "the restarted actor must run on a fresh pid"
    );
    let envelope = await_envelope(&subscription)?;
    assert_eq!(
        envelope.payload,
        br#"{"order_id":"A1","quantity":3}"#.to_vec()
    );
    supervisor.shutdown();
    Ok(())
}

#[test]
fn subscriber_death_detection_survives_restart() -> Result<(), Box<dyn Error>> {
    // MAJOR-1 regression guard. A subscriber created BEFORE the crash must still
    // have its death detected AFTER the actor restarts. This only works if the
    // restarted actor re-links to the surviving subscriber on boot; without the
    // re-link the EXIT never reaches the new actor and the count never drops, so
    // this test fails (times out in `await_count`) before the fix and passes
    // after it.
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let keep = handle.subscribe()?;
    let transient = handle.subscribe()?;
    assert_eq!(await_count(&handle, |count| count == 2)?, 2);

    // Crash + restart (the publish drives the restart and the boot re-link).
    crash_actor(&handle)?;
    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;
    assert_eq!(
        await_count(&handle, |count| count == 2)?,
        2,
        "both pre-crash subscribers must still be registered after restart"
    );

    // Drop one subscriber AFTER the restart: its EXIT must prune it from the
    // restarted actor's fan-out — proving the re-link was re-established.
    drop(transient);
    assert_eq!(await_count(&handle, |count| count == 1)?, 1);

    // The survivor still receives published messages on the restarted actor.
    handle.publish(br#"{"order_id":"A2","quantity":4}"#)?;
    let _ = await_envelope(&keep)?;
    supervisor.shutdown();
    Ok(())
}

#[test]
fn max_restarts_budget_bounds_restarts() -> Result<(), Box<dyn Error>> {
    // A budget of exactly one restart: the first crash restarts, the second
    // crash exhausts the budget and the next operation must surface an error
    // rather than silently respawning again.
    let supervisor = ChannelSupervisor::with_policy(ChannelRestartPolicy::one_for_one(1))?;
    let handle = order_channel(&supervisor)?;
    let _subscription = handle.subscribe()?;

    // First crash + restart (consumes the single restart from the budget).
    crash_actor(&handle)?;
    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;

    // Second crash: the budget is now exhausted, so the next operation must fail
    // and the actor must NOT be respawned.
    crash_actor(&handle)?;
    let result = handle.publish(br#"{"order_id":"A2","quantity":4}"#);
    assert!(
        matches!(result, Err(LiminalError::DeliveryFailed { .. })),
        "exhausting the restart budget must surface a DeliveryFailed error, got {result:?}"
    );
    supervisor.shutdown();
    Ok(())
}

#[test]
fn command_on_dead_actor_returns_promptly_not_after_timeout() -> Result<(), Box<dyn Error>> {
    // MINOR-2: if the actor dies while a command's reply sender sits in the
    // command queue, the host must NOT block the full 5s COMMAND_TIMEOUT. With a
    // `never` restart policy a crashed actor stays dead, so a subsequent
    // operation must surface an error well within the timeout. We assert a tight
    // wall-clock bound: a hang would blow far past it.
    let supervisor = ChannelSupervisor::with_policy(ChannelRestartPolicy::never())?;
    let handle = order_channel(&supervisor)?;
    let _subscription = handle.subscribe()?;
    // Confirm the actor is live, then crash it so it can never reply.
    let _ = handle.actor_pid()?;
    crash_actor(&handle)?;

    let started = Instant::now();
    let result = handle.publish(br#"{"order_id":"A1","quantity":3}"#);
    let elapsed = started.elapsed();
    assert!(
        result.is_err(),
        "publish to a dead non-restarting actor must fail"
    );
    assert!(
        elapsed < Duration::from_secs(2),
        "command on a dead actor must return promptly (took {elapsed:?}), not block the 5s timeout"
    );
    supervisor.shutdown();
    Ok(())
}

#[test]
fn restarting_one_channel_does_not_disturb_another() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let orders = ChannelHandle::with_supervisor(
        ChannelConfig::new("orders".to_owned(), order_schema()?, ChannelMode::Ephemeral),
        supervisor.clone(),
    );
    let events = ChannelHandle::with_supervisor(
        ChannelConfig::new("events".to_owned(), order_schema()?, ChannelMode::Ephemeral),
        supervisor.clone(),
    );
    let orders_sub = orders.subscribe()?;
    let events_sub = events.subscribe()?;
    let events_pid = events.actor_pid()?;

    // Crash + restart ONLY the orders channel.
    crash_actor(&orders)?;
    orders.publish(br#"{"order_id":"only-orders","quantity":2}"#)?;
    let delivered = await_envelope(&orders_sub)?;
    assert_eq!(
        delivered.payload,
        br#"{"order_id":"only-orders","quantity":2}"#.to_vec()
    );

    // The events channel was untouched: same pid, still delivering.
    assert_eq!(
        events.actor_pid()?,
        events_pid,
        "the unrelated channel actor must keep its original pid"
    );
    events.publish(br#"{"order_id":"events-live","quantity":1}"#)?;
    let event = await_envelope(&events_sub)?;
    assert_eq!(
        event.payload,
        br#"{"order_id":"events-live","quantity":1}"#.to_vec()
    );
    supervisor.shutdown();
    Ok(())
}

#[test]
fn publish_valid_message_delivers_envelope() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;

    handle.publish_from("publisher-1", br#"{"order_id":"A1","quantity":3}"#)?;
    let envelope = await_envelope(&subscription)?;

    assert_eq!(
        envelope.payload,
        br#"{"order_id":"A1","quantity":3}"#.to_vec()
    );
    assert_eq!(envelope.publisher_id.as_str(), "publisher-1");
    assert!(envelope.causal_context.is_none());
    supervisor.shutdown();
    Ok(())
}

#[test]
fn publish_invalid_message_returns_schema_mismatch_without_delivery() -> Result<(), Box<dyn Error>>
{
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;

    let result = handle.publish(br#"{"order_id":"A1","quantity":0}"#);

    assert!(matches!(result, Err(LiminalError::SchemaMismatch { .. })));
    assert!(subscription.try_next()?.is_none());
    supervisor.shutdown();
    Ok(())
}

#[test]
fn evolved_schema_keeps_existing_subscriber_and_applies_default() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;
    let schema_id =
        handle.evolve_schema_add_field("priority", json!({"type":"string"}), json!("normal"))?;

    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;
    let envelope = await_envelope(&subscription)?;
    let payload: Value = serde_json::from_slice(&envelope.payload)?;

    assert_eq!(envelope.schema_id, schema_id);
    assert_eq!(payload.get("priority"), Some(&json!("normal")));
    supervisor.shutdown();
    Ok(())
}

#[test]
fn predicate_subscription_only_receives_matching_messages() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    // Only deliver orders with quantity >= 5.
    let filtered = handle.subscribe_filtered(|envelope: &Envelope| {
        serde_json::from_slice::<Value>(&envelope.payload)
            .ok()
            .and_then(|value| value.get("quantity").and_then(Value::as_u64))
            .is_some_and(|quantity| quantity >= 5)
    })?;
    let unfiltered = handle.subscribe()?;

    handle.publish(br#"{"order_id":"small","quantity":1}"#)?;
    handle.publish(br#"{"order_id":"big","quantity":9}"#)?;

    // The unfiltered subscriber receives both, in order.
    let first = await_envelope(&unfiltered)?;
    let second = await_envelope(&unfiltered)?;
    assert_eq!(
        first.payload,
        br#"{"order_id":"small","quantity":1}"#.to_vec()
    );
    assert_eq!(
        second.payload,
        br#"{"order_id":"big","quantity":9}"#.to_vec()
    );

    // The filtered subscriber receives ONLY the matching (big) order.
    let matched = await_envelope(&filtered)?;
    assert_eq!(
        matched.payload,
        br#"{"order_id":"big","quantity":9}"#.to_vec()
    );
    assert!(filtered.try_next()?.is_none());
    supervisor.shutdown();
    Ok(())
}

#[test]
fn dropping_subscription_removes_subscriber_via_exit() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let keep = handle.subscribe()?;
    let transient = handle.subscribe()?;
    assert_eq!(await_count(&handle, |count| count == 2)?, 2);

    // Dropping the handle terminates the subscriber process; the channel actor
    // traps the resulting EXIT and prunes it from the fan-out list — no polling
    // of a weak pointer is involved.
    drop(transient);
    assert_eq!(await_count(&handle, |count| count == 1)?, 1);

    // The surviving subscriber still receives published messages.
    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;
    let envelope = await_envelope(&keep)?;
    assert_eq!(
        envelope.payload,
        br#"{"order_id":"A1","quantity":3}"#.to_vec()
    );
    supervisor.shutdown();
    Ok(())
}

#[test]
fn explicit_unsubscribe_removes_subscriber() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;
    assert_eq!(await_count(&handle, |count| count == 1)?, 1);

    handle.unsubscribe(&subscription)?;
    assert_eq!(await_count(&handle, |count| count == 0)?, 0);
    supervisor.shutdown();
    Ok(())
}

#[test]
fn publish_after_close_is_rejected() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let handle = order_channel(&supervisor)?;
    handle.close()?;

    // The actor process stopped on close; a publish therefore cannot be
    // delivered. It must fail rather than silently succeed.
    let result = handle.publish(br#"{"order_id":"A1","quantity":3}"#);
    assert!(result.is_err());
    supervisor.shutdown();
    Ok(())
}

#[test]
fn registry_creates_looks_up_lists_and_rejects_duplicates() -> Result<(), Box<dyn Error>> {
    let registry = ChannelRegistry::new()?;
    let config = ChannelConfig::new("orders".to_owned(), order_schema()?, ChannelMode::Ephemeral);

    let handle = registry.create(config.clone())?;
    let _subscription = handle.subscribe()?;

    // Lookup returns the same channel.
    assert!(registry.lookup("orders")?.is_some());
    assert!(registry.lookup("missing")?.is_none());

    // Duplicate names are rejected.
    assert!(matches!(
        registry.create(config),
        Err(LiminalError::PublishFailed { .. })
    ));

    // List reports the channel with its live subscriber count.
    let summaries = registry.list()?;
    assert_eq!(summaries.len(), 1);
    assert_eq!(summaries[0].name, "orders");
    assert_eq!(summaries[0].subscriber_count, 1);

    // Close removes it.
    assert!(registry.close("orders")?);
    assert!(registry.lookup("orders")?.is_none());
    assert!(!registry.close("orders")?);
    registry.shutdown();
    Ok(())
}

#[test]
fn separate_channels_are_independent_processes() -> Result<(), Box<dyn Error>> {
    let supervisor = ChannelSupervisor::new()?;
    let orders = ChannelHandle::with_supervisor(
        ChannelConfig::new("orders".to_owned(), order_schema()?, ChannelMode::Ephemeral),
        supervisor.clone(),
    );
    let events = ChannelHandle::with_supervisor(
        ChannelConfig::new("events".to_owned(), order_schema()?, ChannelMode::Ephemeral),
        supervisor.clone(),
    );
    let orders_sub = orders.subscribe()?;
    let events_sub = events.subscribe()?;

    orders.publish(br#"{"order_id":"only-orders","quantity":2}"#)?;
    let delivered = await_envelope(&orders_sub)?;
    assert_eq!(
        delivered.payload,
        br#"{"order_id":"only-orders","quantity":2}"#.to_vec()
    );
    // The events channel's subscriber receives nothing — separate process,
    // separate subscriber list.
    assert!(events_sub.try_next()?.is_none());
    supervisor.shutdown();
    Ok(())
}

#[test]
fn restart_policy_never_is_configurable() -> Result<(), Box<dyn Error>> {
    // Supervision is configurable (R4): a `never` policy is accepted and exposed.
    let supervisor = ChannelSupervisor::with_policy(ChannelRestartPolicy::never())?;
    assert_eq!(supervisor.policy(), &ChannelRestartPolicy::never());
    let handle = order_channel(&supervisor)?;
    let subscription = handle.subscribe()?;
    handle.publish(br#"{"order_id":"A1","quantity":3}"#)?;
    let _ = await_envelope(&subscription)?;
    supervisor.shutdown();
    Ok(())
}