durare 0.3.0

A DBOS-compatible durable execution SDK for Rust: write ordinary async code, checkpoint every step to Postgres or SQLite, and resume exactly where you left off after a crash.
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
//! Queue & dispatch tests, backend-free via the in-memory provider:
//! basic enqueue→dispatch, worker concurrency, priority ordering, delayed
//! enqueue, deduplication, and rate limiting.

use durare::{
    DurableContext, DurableEngine, Error, ErrorCode, InMemoryProvider, ListFilter, RateLimiter,
    Result, WorkflowOptions, WorkflowQueue, STATUS_DELAYED, STATUS_ENQUEUED,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// A queue that polls fast enough for tests.
fn test_queue(name: &str) -> WorkflowQueue {
    WorkflowQueue::new(name).base_polling_interval(Duration::from_millis(10))
}

#[tokio::test]
async fn enqueue_dispatches_and_completes() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("add_one", |_ctx: DurableContext, n: i64| async move {
        Ok::<_, Error>(n + 1)
    });
    engine.register_queue(test_queue("q"));
    engine.launch().await?;

    let handle = engine
        .start::<_, i64>("add_one", 41_i64, WorkflowOptions::default().queue("q"))
        .await?;
    assert_eq!(handle.result().await?, 42);

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

#[tokio::test]
async fn enqueue_to_unregistered_queue_errors() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    let res = engine
        .start::<_, ()>("noop", (), WorkflowOptions::default().queue("nope"))
        .await;
    assert!(matches!(res, Err(Error::UnknownQueue(ref q)) if q == "nope"));
    Ok(())
}

/// With worker_concurrency(1), at most one workflow from the queue runs at a
/// time on this executor.
#[tokio::test]
async fn worker_concurrency_is_enforced() -> Result<()> {
    static RUNNING: AtomicUsize = AtomicUsize::new(0);
    static MAX_SEEN: AtomicUsize = AtomicUsize::new(0);

    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("tracked", |_ctx: DurableContext, _: ()| async move {
        let now = RUNNING.fetch_add(1, Ordering::SeqCst) + 1;
        MAX_SEEN.fetch_max(now, Ordering::SeqCst);
        tokio::time::sleep(Duration::from_millis(40)).await;
        RUNNING.fetch_sub(1, Ordering::SeqCst);
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("serial").worker_concurrency(1));
    engine.launch().await?;

    let mut handles = Vec::new();
    for i in 0..3 {
        handles.push(
            engine
                .start::<_, ()>(
                    "tracked",
                    (),
                    WorkflowOptions::with_id(format!("wf-conc-{i}")).queue("serial"),
                )
                .await?,
        );
    }
    for h in &mut handles {
        h.result().await?;
    }

    assert_eq!(
        MAX_SEEN.load(Ordering::SeqCst),
        1,
        "worker_concurrency(1) must serialize queue workflows"
    );
    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// On a priority-enabled serialized queue, lower priority values run first
/// regardless of enqueue order.
#[tokio::test]
async fn priority_orders_execution() -> Result<()> {
    let order: Arc<tokio::sync::Mutex<Vec<i64>>> = Arc::new(tokio::sync::Mutex::new(Vec::new()));

    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    let order_wf = order.clone();
    engine.register("record", move |_ctx: DurableContext, n: i64| {
        let order = order_wf.clone();
        async move {
            order.lock().await.push(n);
            Ok::<_, Error>(n)
        }
    });
    engine.register_queue(test_queue("prio").worker_concurrency(1).priority_enabled());

    // Enqueue before launch so all three are pending when dispatch begins.
    for (i, prio) in [(0, 3), (1, 1), (2, 2)] {
        let mut opts = WorkflowOptions::with_id(format!("wf-prio-{i}"));
        opts.priority = prio;
        let _ = engine
            .start::<_, i64>("record", prio as i64, opts.queue("prio"))
            .await?;
    }
    engine.launch().await?;

    // Wait until all three have run.
    let deadline = Instant::now() + Duration::from_secs(3);
    while order.lock().await.len() < 3 {
        assert!(Instant::now() < deadline, "queue did not drain in time");
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    assert_eq!(*order.lock().await, vec![1, 2, 3]);

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// A delayed enqueue parks in DELAYED, then runs once the delay expires.
#[tokio::test]
async fn delayed_enqueue_waits_then_runs() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("echo", |_ctx: DurableContext, n: i64| async move {
        Ok::<_, Error>(n)
    });
    engine.register_queue(test_queue("later"));
    engine.launch().await?;

    let started = Instant::now();
    let mut opts = WorkflowOptions::with_id("wf-delayed");
    opts.delay = Some(Duration::from_millis(150));
    let handle = engine
        .start::<_, i64>("echo", 7_i64, opts.queue("later"))
        .await?;

    assert_eq!(handle.get_status().await?.status, STATUS_DELAYED);
    assert_eq!(handle.result().await?, 7);
    assert!(
        started.elapsed() >= Duration::from_millis(120),
        "workflow must not run before its delay expires"
    );

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// set_workflow_delay reschedules a DELAYED workflow: a far-future delay is
/// shortened so the workflow runs almost immediately. Non-DELAYED workflows are
/// a no-op (returns false, no error).
#[tokio::test]
async fn set_workflow_delay_reschedules() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("echo", |_ctx: DurableContext, n: i64| async move {
        Ok::<_, Error>(n)
    });
    engine.register_queue(test_queue("resched"));
    engine.launch().await?;

    // Enqueue with a 60s delay so it would never run during the test...
    let mut opts = WorkflowOptions::with_id("wf-resched");
    opts.delay = Some(Duration::from_secs(60));
    let handle = engine
        .start::<_, i64>("echo", 9_i64, opts.queue("resched"))
        .await?;
    assert_eq!(handle.get_status().await?.status, STATUS_DELAYED);

    // ...then pull it forward to ~20ms from now.
    let started = Instant::now();
    assert!(
        engine
            .set_workflow_delay("wf-resched", Duration::from_millis(20))
            .await?,
        "rescheduling a DELAYED workflow must report a match"
    );
    assert_eq!(handle.result().await?, 9);
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "the workflow must run on the shortened delay, not the original 60s"
    );

    // The now-completed workflow is no longer DELAYED: a further reschedule is a
    // silent no-op. A missing id likewise.
    assert!(
        !engine
            .set_workflow_delay("wf-resched", Duration::ZERO)
            .await?
    );
    assert!(!engine.set_workflow_delay("ghost", Duration::ZERO).await?);

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// Delay without a queue is rejected.
#[tokio::test]
async fn delay_requires_queue() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    let opts = WorkflowOptions {
        delay: Some(Duration::from_millis(10)),
        ..Default::default()
    };
    let res = engine.start::<_, ()>("noop", (), opts).await;
    assert!(res.is_err());
    Ok(())
}

/// Two different workflow ids with the same deduplication id on one queue:
/// the second enqueue is rejected.
#[tokio::test]
async fn dedup_id_rejects_duplicates() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("dedup"));

    let mut opts = WorkflowOptions::with_id("wf-dedup-1");
    opts.dedup_id = Some("once".to_string());
    let _first = engine
        .start::<_, ()>("noop", (), opts.queue("dedup"))
        .await?;

    let mut opts = WorkflowOptions::with_id("wf-dedup-2");
    opts.dedup_id = Some("once".to_string());
    let err = match engine.start::<_, ()>("noop", (), opts.queue("dedup")).await {
        Ok(_) => panic!("same dedup id on the same queue must be rejected"),
        Err(e) => e,
    };
    assert_eq!(err.code(), ErrorCode::QueueDeduplicated);
    Ok(())
}

/// With `ReturnExisting`, a colliding deduplication id returns a handle to the
/// workflow already holding the slot instead of erroring; a non-default policy
/// without a dedup id is rejected.
#[tokio::test]
async fn dedup_return_existing_returns_the_holder() -> Result<()> {
    use durare::DeduplicationPolicy;
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("dedup"));

    let first = engine
        .start::<_, ()>(
            "noop",
            (),
            WorkflowOptions::with_id("wf-1")
                .dedup_id("once")
                .queue("dedup"),
        )
        .await?;

    let again = engine
        .start::<_, ()>(
            "noop",
            (),
            WorkflowOptions::with_id("wf-2")
                .dedup_id("once")
                .dedup_policy(DeduplicationPolicy::ReturnExisting)
                .queue("dedup"),
        )
        .await?;
    assert_eq!(again.id(), first.id(), "returned the slot holder");
    assert_eq!(again.id(), "wf-1");

    assert!(
        engine
            .start::<_, ()>(
                "noop",
                (),
                WorkflowOptions::with_id("wf-3")
                    .dedup_policy(DeduplicationPolicy::ReturnExisting)
                    .queue("dedup")
            )
            .await
            .is_err(),
        "a non-default policy requires a dedup id"
    );
    Ok(())
}

/// Sending to a workflow id that does not exist is a typed, classifiable error.
#[tokio::test]
async fn send_to_nonexistent_workflow_is_typed() -> Result<()> {
    let engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    let err = engine
        .send("no-such-workflow", "hi", "topic")
        .await
        .expect_err("sending to a nonexistent workflow must fail");
    assert_eq!(err.code(), ErrorCode::NonExistentWorkflow);
    Ok(())
}

/// With a rate limit of 2 per long period, only 2 of 4 enqueued workflows
/// start; the rest stay ENQUEUED.
#[tokio::test]
async fn rate_limit_caps_starts() -> Result<()> {
    static STARTED: AtomicUsize = AtomicUsize::new(0);

    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("counted", |_ctx: DurableContext, _: ()| async move {
        STARTED.fetch_add(1, Ordering::SeqCst);
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("limited").rate_limiter(RateLimiter {
        limit: 2,
        period: Duration::from_secs(60),
    }));

    let mut handles = Vec::new();
    for i in 0..4 {
        handles.push(
            engine
                .start::<_, ()>(
                    "counted",
                    (),
                    WorkflowOptions::with_id(format!("wf-rate-{i}")).queue("limited"),
                )
                .await?,
        );
    }
    engine.launch().await?;

    // Give the dispatcher several iterations to (not) over-dispatch.
    tokio::time::sleep(Duration::from_millis(200)).await;
    assert_eq!(
        STARTED.load(Ordering::SeqCst),
        2,
        "only `limit` workflows may start within the rate period"
    );
    let enqueued = futures_count_enqueued(&mut handles).await?;
    assert_eq!(enqueued, 2, "the overflow workflows must remain ENQUEUED");

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// The registry reports every registered queue, sorted by name, regardless of
/// which ones this process listens to.
#[tokio::test]
async fn list_registered_queues_is_sorted() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register_queue(WorkflowQueue::new("zebra").worker_concurrency(2));
    engine.register_queue(WorkflowQueue::new("alpha"));

    let names: Vec<String> = engine
        .list_registered_queues()
        .into_iter()
        .map(|q| q.name)
        .collect();
    assert_eq!(names, vec!["alpha".to_string(), "zebra".to_string()]);
    Ok(())
}

/// `listen_queues` dispatches only the named subset: an unlistened queue still
/// accepts enqueues, but nothing claims them in this process.
#[tokio::test]
async fn listen_queues_dispatches_only_listened() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("add_one", |_ctx: DurableContext, n: i64| async move {
        Ok::<_, Error>(n + 1)
    });
    engine.register_queue(test_queue("listened"));
    engine.register_queue(test_queue("ignored"));
    engine.listen_queues(["listened"]);
    engine.launch().await?;

    // The listened queue runs to completion.
    let run = engine
        .start::<_, i64>(
            "add_one",
            41_i64,
            WorkflowOptions::default().queue("listened"),
        )
        .await?;
    assert_eq!(run.result().await?, 42);

    // The ignored queue accepts the enqueue but never dispatches it here.
    let idle = engine
        .start::<_, i64>(
            "add_one",
            1_i64,
            WorkflowOptions::default().queue("ignored"),
        )
        .await?;
    tokio::time::sleep(Duration::from_millis(100)).await;
    assert_eq!(
        idle.get_status().await?.status,
        STATUS_ENQUEUED,
        "an unlistened queue is not dispatched by this process"
    );

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// `ListFilter::queues_only` returns only workflows that are on a queue,
/// excluding directly-run ones.
#[tokio::test]
async fn queues_only_filters_to_queued_workflows() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("q"));
    engine.launch().await?;

    // One direct run, one enqueued.
    engine
        .start::<_, ()>("noop", (), WorkflowOptions::with_id("direct"))
        .await?
        .result()
        .await?;
    engine
        .start::<_, ()>("noop", (), WorkflowOptions::with_id("queued").queue("q"))
        .await?
        .result()
        .await?;

    let queued: Vec<String> = engine
        .list_workflows(&ListFilter {
            queues_only: true,
            ..Default::default()
        })
        .await?
        .into_iter()
        .map(|w| w.id)
        .collect();
    assert_eq!(queued, vec!["queued".to_string()]);

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

/// A partitioned queue applies worker concurrency *per partition*: with a limit
/// of 1 and two active partitions, one workflow from each runs at once, so two
/// run concurrently overall.
#[tokio::test]
async fn partitioned_queue_concurrency_is_per_partition() -> Result<()> {
    static CURRENT: AtomicUsize = AtomicUsize::new(0);
    static PEAK: AtomicUsize = AtomicUsize::new(0);

    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("work", |ctx: DurableContext, _: ()| async move {
        let now = CURRENT.fetch_add(1, Ordering::SeqCst) + 1;
        PEAK.fetch_max(now, Ordering::SeqCst);
        ctx.sleep(Duration::from_millis(80)).await?;
        CURRENT.fetch_sub(1, Ordering::SeqCst);
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("pq").partitioned().worker_concurrency(1));
    engine.launch().await?;

    // Two workflows in each of two partitions.
    let mut handles = Vec::new();
    for part in ["a", "a", "b", "b"] {
        handles.push(
            engine
                .start::<_, ()>(
                    "work",
                    (),
                    WorkflowOptions::default().partition_key(part).queue("pq"),
                )
                .await?,
        );
    }
    for h in &mut handles {
        h.result().await?;
    }

    assert_eq!(
        PEAK.load(Ordering::SeqCst),
        2,
        "each partition runs one at a time, but the two partitions run in parallel"
    );

    engine.shutdown(Duration::from_secs(2)).await?;
    Ok(())
}

/// A workflow enqueued to a partitioned queue without a partition key is never
/// dispatched (partition discovery only sees keyed rows).
#[tokio::test]
async fn partitioned_queue_ignores_keyless_enqueue() -> Result<()> {
    let mut engine = DurableEngine::new(Arc::new(InMemoryProvider::new())).await?;
    engine.register("noop", |_ctx: DurableContext, _: ()| async move {
        Ok::<_, Error>(())
    });
    engine.register_queue(test_queue("pq").partitioned());
    engine.launch().await?;

    let idle = engine
        .start::<_, ()>("noop", (), WorkflowOptions::default().queue("pq"))
        .await?;
    tokio::time::sleep(Duration::from_millis(80)).await;
    assert_eq!(
        idle.get_status().await?.status,
        STATUS_ENQUEUED,
        "no partition key means nothing dispatches it"
    );

    engine.shutdown(Duration::from_secs(1)).await?;
    Ok(())
}

async fn futures_count_enqueued(handles: &mut [durare::WorkflowHandle<()>]) -> Result<usize> {
    let mut n = 0;
    for h in handles {
        if h.get_status().await?.status == STATUS_ENQUEUED {
            n += 1;
        }
    }
    Ok(n)
}