chasquimq 1.1.0

The fastest open-source message broker for Redis. Rust-native engine on Redis Streams + MessagePack, with Node.js and Python bindings.
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use chasquimq::config::{ConsumerConfig, ProducerConfig, PromoterConfig};
use chasquimq::consumer::Consumer;
use chasquimq::producer::{Producer, delayed_key, promoter_lock_key, stream_key};
use chasquimq::{Error, Promoter};
use fred::clients::Client;
use fred::interfaces::ClientLike;
use fred::prelude::Config;
use fred::types::{ClusterHash, CustomCommand, Value};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant, SystemTime};
use tokio_util::sync::CancellationToken;

fn redis_url() -> String {
    std::env::var("REDIS_URL").expect("REDIS_URL must be set to run integration tests")
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct Sample {
    n: u32,
}

fn init_tracing() {
    use std::sync::Once;
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        let _ = tracing_subscriber::fmt()
            .with_env_filter(
                tracing_subscriber::EnvFilter::try_from_default_env()
                    .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")),
            )
            .with_test_writer()
            .try_init();
    });
}

async fn admin() -> Client {
    init_tracing();
    let cfg = Config::from_url(&redis_url()).expect("REDIS_URL");
    let client = Client::new(cfg, None, None, None);
    client.init().await.expect("connect admin");
    client
}

async fn flush_all(admin: &Client, queue: &str) {
    for suffix in ["stream", "dlq", "delayed", "promoter:lock"] {
        let key = format!("{{chasqui:{queue}}}:{suffix}");
        let _: Value = admin
            .custom(
                CustomCommand::new_static("DEL", ClusterHash::FirstKey, false),
                vec![Value::from(key)],
            )
            .await
            .expect("DEL");
    }
}

async fn xlen(admin: &Client, key: &str) -> i64 {
    match admin
        .custom::<Value, Value>(
            CustomCommand::new_static("XLEN", ClusterHash::FirstKey, false),
            vec![Value::from(key)],
        )
        .await
        .expect("XLEN")
    {
        Value::Integer(n) => n,
        Value::Null => 0,
        other => panic!("XLEN unexpected: {other:?}"),
    }
}

async fn zcard(admin: &Client, key: &str) -> i64 {
    match admin
        .custom::<Value, Value>(
            CustomCommand::new_static("ZCARD", ClusterHash::FirstKey, false),
            vec![Value::from(key)],
        )
        .await
        .expect("ZCARD")
    {
        Value::Integer(n) => n,
        Value::Null => 0,
        other => panic!("ZCARD unexpected: {other:?}"),
    }
}

async fn get_string(admin: &Client, key: &str) -> Option<String> {
    let res: Value = admin
        .custom(
            CustomCommand::new_static("GET", ClusterHash::FirstKey, false),
            vec![Value::from(key)],
        )
        .await
        .expect("GET");
    match res {
        Value::String(s) => Some(s.to_string()),
        Value::Bytes(b) => std::str::from_utf8(&b).ok().map(|s| s.to_string()),
        Value::Null => None,
        _ => None,
    }
}

async fn script_flush(admin: &Client) {
    let _: Value = admin
        .custom(
            CustomCommand::new_static("SCRIPT", ClusterHash::FirstKey, false),
            vec![Value::from("FLUSH")],
        )
        .await
        .expect("SCRIPT FLUSH");
}

fn producer_cfg(queue: &str) -> ProducerConfig {
    ProducerConfig {
        queue_name: queue.to_string(),
        pool_size: 2,
        max_stream_len: 100_000,
        ..Default::default()
    }
}

fn producer_cfg_with_max_delay(queue: &str, max_delay_secs: u64) -> ProducerConfig {
    ProducerConfig {
        queue_name: queue.to_string(),
        pool_size: 2,
        max_stream_len: 100_000,
        max_delay_secs,
    }
}

fn consumer_cfg(queue: &str, consumer_id: &str, delayed_enabled: bool) -> ConsumerConfig {
    ConsumerConfig {
        queue_name: queue.to_string(),
        group: "default".to_string(),
        consumer_id: consumer_id.to_string(),
        batch: 64,
        block_ms: 100,
        claim_min_idle_ms: 30_000,
        concurrency: 16,
        max_attempts: 3,
        ack_batch: 64,
        ack_idle_ms: 5,
        shutdown_deadline_secs: 5,
        max_payload_bytes: 1_048_576,
        dlq_inflight: 32,
        delayed_enabled,
        delayed_poll_interval_ms: 50,
        delayed_promote_batch: 256,
        delayed_max_stream_len: 100_000,
        delayed_lock_ttl_secs: 5,
        ..Default::default()
    }
}

fn promoter_cfg(queue: &str, holder_id: &str) -> PromoterConfig {
    PromoterConfig {
        queue_name: queue.to_string(),
        poll_interval_ms: 50,
        promote_batch: 256,
        max_stream_len: 100_000,
        lock_ttl_secs: 5,
        holder_id: holder_id.to_string(),
        ..Default::default()
    }
}

async fn wait_until<F, Fut>(timeout: Duration, mut check: F)
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = bool>,
{
    let start = Instant::now();
    loop {
        if check().await {
            return;
        }
        if start.elapsed() > timeout {
            panic!("wait_until timed out after {:?}", timeout);
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

fn spawn_consumer(
    queue: &str,
    consumer_id: &str,
    delayed_enabled: bool,
    counter: Arc<AtomicUsize>,
    shutdown: CancellationToken,
) -> tokio::task::JoinHandle<chasquimq::Result<()>> {
    let consumer: Consumer<Sample> = Consumer::new(
        redis_url(),
        consumer_cfg(queue, consumer_id, delayed_enabled),
    );
    tokio::spawn(async move {
        consumer
            .run(
                move |_job| {
                    let counter = counter.clone();
                    async move {
                        counter.fetch_add(1, Ordering::SeqCst);
                        Ok(chasquimq::Bytes::new())
                    }
                },
                shutdown,
            )
            .await
    })
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_delivers_within_window() {
    let admin = admin().await;
    let queue = "delayed_e1";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let counter = Arc::new(AtomicUsize::new(0));
    let shutdown = CancellationToken::new();
    let handle = spawn_consumer(queue, "c1", true, counter.clone(), shutdown.clone());

    let scheduled_at = Instant::now();
    producer
        .add_in(Duration::from_millis(150), Sample { n: 1 })
        .await
        .expect("add_in");

    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(
        counter.load(Ordering::SeqCst),
        0,
        "handler must not fire before delay elapses"
    );

    wait_until(Duration::from_millis(800), || {
        let counter = counter.clone();
        async move { counter.load(Ordering::SeqCst) == 1 }
    })
    .await;

    let elapsed = scheduled_at.elapsed();
    assert!(
        elapsed >= Duration::from_millis(140),
        "delivered too early: {elapsed:?}"
    );

    shutdown.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_zero_fast_paths() {
    let admin = admin().await;
    let queue = "delayed_e2";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    producer
        .add_in(Duration::ZERO, Sample { n: 1 })
        .await
        .expect("add_in zero");

    let dkey = delayed_key(queue);
    let skey = stream_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0, "ZSET must stay empty");
    assert_eq!(xlen(&admin, &skey).await, 1, "stream gets the entry");

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_at_past_fast_paths() {
    let admin = admin().await;
    let queue = "delayed_e3";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let past = SystemTime::now() - Duration::from_secs(1);
    producer
        .add_at(past, Sample { n: 1 })
        .await
        .expect("add_at past");

    let dkey = delayed_key(queue);
    let skey = stream_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0);
    assert_eq!(xlen(&admin, &skey).await, 1);

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_bulk_delivers_all() {
    let admin = admin().await;
    let queue = "delayed_e4";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let counter = Arc::new(AtomicUsize::new(0));
    let shutdown = CancellationToken::new();
    let handle = spawn_consumer(queue, "c1", true, counter.clone(), shutdown.clone());

    let payloads: Vec<Sample> = (0..1_000).map(|n| Sample { n }).collect();
    producer
        .add_in_bulk(Duration::from_millis(100), payloads)
        .await
        .expect("add_in_bulk");

    wait_until(Duration::from_secs(15), || {
        let counter = counter.clone();
        async move { counter.load(Ordering::SeqCst) == 1_000 }
    })
    .await;

    let dkey = delayed_key(queue);
    wait_until(Duration::from_secs(5), || {
        let admin = admin.clone();
        let dkey = dkey.clone();
        async move { zcard(&admin, &dkey).await == 0 }
    })
    .await;

    shutdown.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(10), handle).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn delayed_survives_consumer_restart() {
    let admin = admin().await;
    let queue = "delayed_e5";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    producer
        .add_in(Duration::from_millis(500), Sample { n: 1 })
        .await
        .expect("add_in");

    let counter = Arc::new(AtomicUsize::new(0));
    let shutdown1 = CancellationToken::new();
    let h1 = spawn_consumer(queue, "c1", true, counter.clone(), shutdown1.clone());
    tokio::time::sleep(Duration::from_millis(100)).await;
    shutdown1.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h1).await;

    assert_eq!(
        counter.load(Ordering::SeqCst),
        0,
        "should not fire before delay"
    );

    let shutdown2 = CancellationToken::new();
    let h2 = spawn_consumer(queue, "c2", true, counter.clone(), shutdown2.clone());

    wait_until(Duration::from_secs(5), || {
        let counter = counter.clone();
        async move { counter.load(Ordering::SeqCst) == 1 }
    })
    .await;

    shutdown2.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h2).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn standalone_promoter_works() {
    let admin = admin().await;
    let queue = "delayed_e6";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let counter = Arc::new(AtomicUsize::new(0));
    let shutdown_consumer = CancellationToken::new();
    let h_consumer = spawn_consumer(
        queue,
        "c1",
        false,
        counter.clone(),
        shutdown_consumer.clone(),
    );

    let shutdown_promoter = CancellationToken::new();
    let promoter = Promoter::new(redis_url(), promoter_cfg(queue, "p1"));
    let h_promoter = tokio::spawn(promoter.run(shutdown_promoter.clone()));

    producer
        .add_in(Duration::from_millis(100), Sample { n: 1 })
        .await
        .expect("add_in");

    wait_until(Duration::from_secs(5), || {
        let counter = counter.clone();
        async move { counter.load(Ordering::SeqCst) == 1 }
    })
    .await;

    shutdown_promoter.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_promoter).await;
    shutdown_consumer.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_consumer).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn noscript_recovery() {
    let admin = admin().await;
    let queue = "delayed_e7";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let counter = Arc::new(AtomicUsize::new(0));
    let shutdown_consumer = CancellationToken::new();
    let h_consumer = spawn_consumer(
        queue,
        "c1",
        false,
        counter.clone(),
        shutdown_consumer.clone(),
    );

    let shutdown_promoter = CancellationToken::new();
    let promoter = Promoter::new(redis_url(), promoter_cfg(queue, "p1"));
    let h_promoter = tokio::spawn(promoter.run(shutdown_promoter.clone()));

    tokio::time::sleep(Duration::from_millis(200)).await;
    script_flush(&admin).await;

    producer
        .add_in(Duration::from_millis(50), Sample { n: 1 })
        .await
        .expect("add_in");

    wait_until(Duration::from_secs(5), || {
        let counter = counter.clone();
        async move { counter.load(Ordering::SeqCst) == 1 }
    })
    .await;

    shutdown_promoter.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_promoter).await;
    shutdown_consumer.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_consumer).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn leader_election_no_double_promote() {
    let admin = admin().await;
    let queue = "delayed_e8";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let mut promoter_handles = Vec::new();
    let mut promoter_shutdowns = Vec::new();
    for i in 0..3 {
        let shutdown = CancellationToken::new();
        let promoter = Promoter::new(redis_url(), promoter_cfg(queue, &format!("p{i}")));
        promoter_handles.push(tokio::spawn(promoter.run(shutdown.clone())));
        promoter_shutdowns.push(shutdown);
    }

    producer
        .add_in(Duration::from_millis(100), Sample { n: 1 })
        .await
        .expect("add_in");

    let skey = stream_key(queue);
    wait_until(Duration::from_secs(5), || {
        let admin = admin.clone();
        let skey = skey.clone();
        async move { xlen(&admin, &skey).await >= 1 }
    })
    .await;

    tokio::time::sleep(Duration::from_millis(300)).await;

    assert_eq!(
        xlen(&admin, &skey).await,
        1,
        "exactly one XADD even with 3 racing promoters"
    );

    for s in promoter_shutdowns {
        s.cancel();
    }
    for h in promoter_handles {
        let _ = tokio::time::timeout(Duration::from_secs(5), h).await;
    }
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn leader_handover() {
    let admin = admin().await;
    let queue = "delayed_e9";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let shutdown_a = CancellationToken::new();
    let promoter_a = Promoter::new(redis_url(), promoter_cfg(queue, "pA"));
    let h_a = tokio::spawn(promoter_a.run(shutdown_a.clone()));

    let lock_key = promoter_lock_key(queue);
    wait_until(Duration::from_secs(2), || {
        let admin = admin.clone();
        let lock_key = lock_key.clone();
        async move { get_string(&admin, &lock_key).await == Some("pA".to_string()) }
    })
    .await;

    shutdown_a.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_a).await;

    let shutdown_b = CancellationToken::new();
    let promoter_b = Promoter::new(redis_url(), promoter_cfg(queue, "pB"));
    let h_b = tokio::spawn(promoter_b.run(shutdown_b.clone()));

    producer
        .add_in(Duration::from_millis(100), Sample { n: 1 })
        .await
        .expect("add_in");

    let skey = stream_key(queue);
    wait_until(Duration::from_secs(5), || {
        let admin = admin.clone();
        let skey = skey.clone();
        async move { xlen(&admin, &skey).await == 1 }
    })
    .await;

    shutdown_b.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), h_b).await;
    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn permanent_error_escalates() {
    let cfg = PromoterConfig {
        queue_name: "delayed_e10".to_string(),
        poll_interval_ms: 50,
        promote_batch: 64,
        max_stream_len: 1_000,
        lock_ttl_secs: 2,
        holder_id: "p1".to_string(),
        ..Default::default()
    };
    let promoter = Promoter::new("redis://127.0.0.1:1", cfg);
    let shutdown = CancellationToken::new();

    let outcome = tokio::time::timeout(Duration::from_secs(5), promoter.run(shutdown))
        .await
        .expect("promoter should escalate before timeout");

    assert!(outcome.is_err(), "unreachable Redis must escalate to Err");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_bulk_empty_is_noop() {
    let admin = admin().await;
    let queue = "delayed_e11";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");
    let ids = producer
        .add_in_bulk(Duration::from_millis(100), Vec::new())
        .await
        .expect("add_in_bulk empty");
    assert!(ids.is_empty());

    let dkey = delayed_key(queue);
    let skey = stream_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0);
    assert_eq!(xlen(&admin, &skey).await, 0);

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_bulk_zero_fast_paths() {
    let admin = admin().await;
    let queue = "delayed_e12";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");

    let payloads: Vec<Sample> = (0..50).map(|n| Sample { n }).collect();
    producer
        .add_in_bulk(Duration::ZERO, payloads)
        .await
        .expect("add_in_bulk zero");

    let dkey = delayed_key(queue);
    let skey = stream_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0, "ZSET must stay empty");
    assert_eq!(
        xlen(&admin, &skey).await,
        50,
        "all 50 entries land in stream"
    );

    let _: () = admin.quit().await.unwrap();
}

struct AlwaysFailEncode;

impl Serialize for AlwaysFailEncode {
    fn serialize<S: serde::Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
        Err(serde::ser::Error::custom("encode failure for test"))
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_partial_encode_fails_atomically() {
    let admin = admin().await;
    let queue = "delayed_e13";
    flush_all(&admin, queue).await;

    let producer: Producer<AlwaysFailEncode> = Producer::connect(&redis_url(), producer_cfg(queue))
        .await
        .expect("connect producer");
    let res = producer
        .add_in_bulk(
            Duration::from_millis(100),
            vec![AlwaysFailEncode, AlwaysFailEncode],
        )
        .await;
    assert!(matches!(res, Err(Error::Encode(_))), "got {res:?}");

    let dkey = delayed_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0, "no ZADD should reach Redis");

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_in_rejects_beyond_max_delay() {
    let admin = admin().await;
    let queue = "delayed_e14";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> =
        Producer::connect(&redis_url(), producer_cfg_with_max_delay(queue, 60))
            .await
            .expect("connect producer");
    let res = producer
        .add_in(Duration::from_secs(120), Sample { n: 1 })
        .await;
    assert!(matches!(res, Err(Error::Config(_))), "got {res:?}");

    let dkey = delayed_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0);

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn add_at_rejects_beyond_max_delay() {
    let admin = admin().await;
    let queue = "delayed_e15";
    flush_all(&admin, queue).await;

    let producer: Producer<Sample> =
        Producer::connect(&redis_url(), producer_cfg_with_max_delay(queue, 60))
            .await
            .expect("connect producer");
    let when = SystemTime::now() + Duration::from_secs(120);
    let res = producer.add_at(when, Sample { n: 1 }).await;
    assert!(matches!(res, Err(Error::Config(_))), "got {res:?}");

    let dkey = delayed_key(queue);
    assert_eq!(zcard(&admin, &dkey).await, 0);

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn promoter_releases_lock_on_shutdown() {
    let admin = admin().await;
    let queue = "delayed_e16";
    flush_all(&admin, queue).await;

    let shutdown = CancellationToken::new();
    let promoter = Promoter::new(redis_url(), promoter_cfg(queue, "p1"));
    let handle = tokio::spawn(promoter.run(shutdown.clone()));

    let lock_key = promoter_lock_key(queue);
    wait_until(Duration::from_secs(2), || {
        let admin = admin.clone();
        let lock_key = lock_key.clone();
        async move { get_string(&admin, &lock_key).await == Some("p1".to_string()) }
    })
    .await;

    shutdown.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;

    wait_until(Duration::from_millis(500), || {
        let admin = admin.clone();
        let lock_key = lock_key.clone();
        async move { get_string(&admin, &lock_key).await.is_none() }
    })
    .await;

    let _: () = admin.quit().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires REDIS_URL"]
async fn promoter_does_not_release_lock_owned_by_other() {
    let admin = admin().await;
    let queue = "delayed_e17";
    flush_all(&admin, queue).await;

    let lock_key = promoter_lock_key(queue);
    // Plant a lock owned by "other-holder" with a long TTL.
    let _: fred::types::Value = admin
        .custom(
            CustomCommand::new_static("SET", ClusterHash::FirstKey, false),
            vec![
                fred::types::Value::from(lock_key.as_str()),
                fred::types::Value::from("other-holder"),
                fred::types::Value::from("EX"),
                fred::types::Value::from(30_i64),
            ],
        )
        .await
        .expect("SET");

    // Spin up a promoter named "p1". It can't acquire (held by other-holder).
    // On shutdown it must NOT delete the lock.
    let shutdown = CancellationToken::new();
    let promoter = Promoter::new(redis_url(), promoter_cfg(queue, "p1"));
    let handle = tokio::spawn(promoter.run(shutdown.clone()));

    tokio::time::sleep(Duration::from_millis(150)).await;

    shutdown.cancel();
    let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;

    assert_eq!(
        get_string(&admin, &lock_key).await,
        Some("other-holder".to_string()),
        "promoter shutdown must not have deleted another holder's lock"
    );

    let _: () = admin.quit().await.unwrap();
}