rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
use crate::extensions::flow_state::{CasOutcome, FlowStore};
use anyhow::Result;
use parking_lot::RwLock;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, SystemTime};

/// A single stored entry: its value and optional expiry instant.
type Entry = (Value, Option<SystemTime>);
/// One flow's key→entry map.
type FlowMap = HashMap<String, Entry>;
/// The whole store, keyed by `flow_id` then `key`. Nesting (vs. a flat `flow:{id}:{key}` map,
/// issue #483) lets `set_ttl` touch only the target flow's keys instead of scanning every entry,
/// and lets the reaper drop a whole expired flow in one step.
type Store = HashMap<String, FlowMap>;

/// Run a full expired-entry sweep once every this many write operations (issue #483). Amortizes
/// the O(total) reap to O(1) per write while bounding how long an expired entry lingers.
const SWEEP_INTERVAL: usize = 256;

/// In-memory implementation of FlowStore
///
/// This implementation stores flow state in a nested HashMap with automatic TTL expiration.
/// Useful for testing, development, and single-instance deployments.
/// What an [`InMemoryFlowStore`] reads the time from for TTLs (issue #1754): the system clock, or
/// an imposter's clock, which a test can freeze and step.
pub type StoreClock = Arc<dyn Fn() -> SystemTime + Send + Sync>;

pub struct InMemoryFlowStore {
    data: Arc<RwLock<Store>>,
    clock: StoreClock,
    default_ttl: Duration,
    /// Store-growing writes since the last sweep; drives the amortized reaper (issue #483). Only
    /// `set`/`increment_by`/applied-CAS count — the paths that can add an entry. `delete`, `set_ttl`,
    /// and a conflicting CAS never grow the store, so they deliberately don't advance the counter
    /// (delete reclaims directly; a conflicting CAS wrote nothing).
    writes_since_sweep: AtomicUsize,
}

impl InMemoryFlowStore {
    pub fn new(default_ttl_seconds: u64) -> Self {
        Self::with_clock(default_ttl_seconds, Arc::new(SystemTime::now))
    }

    /// A store whose TTLs read `clock` instead of the system clock (issue #1754): every expiry it
    /// sets and every one it checks, the amortized sweep's included.
    pub fn with_clock(default_ttl_seconds: u64, clock: StoreClock) -> Self {
        Self {
            data: Arc::new(RwLock::new(HashMap::new())),
            clock,
            default_ttl: Duration::from_secs(default_ttl_seconds),
            writes_since_sweep: AtomicUsize::new(0),
        }
    }

    /// List every non-expired key currently stored under `flow_id`, for `rift script run`'s
    /// post-execution state dump (issue #360 Item 2) — the [`FlowStore`] trait itself has no
    /// enumeration method (a real backend like Redis may not make that cheap), but the CLI works
    /// with a concrete `InMemoryFlowStore` fixture, so an inherent method here is enough.
    pub fn keys_for_flow(&self, flow_id: &str) -> Vec<String> {
        let data = self.data.read();
        match data.get(flow_id) {
            Some(flow) => flow
                .iter()
                .filter(|(_, (_, expiry))| !self.is_expired(expiry))
                .map(|(key, _)| key.clone())
                .collect(),
            None => Vec::new(),
        }
    }

    fn now(&self) -> SystemTime {
        (self.clock)()
    }

    fn is_expired(&self, expiry: &Option<SystemTime>) -> bool {
        matches!(expiry, Some(exp) if self.now() > *exp)
    }

    /// Remove a flow's entry for `key` if it has expired, so a subsequent read sees it as absent.
    /// Called with the write lock held; mirrors the pre-#483 opportunistic same-key cleanup.
    fn remove_if_expired(&self, flow: &mut FlowMap, key: &str) {
        if let Some((_, expiry)) = flow.get(key)
            && self.is_expired(expiry)
        {
            flow.remove(key);
        }
    }

    /// Count one write toward the amortized reaper and, every [`SWEEP_INTERVAL`] writes, drop
    /// every expired entry (and any flow left empty). Called with the write lock already held.
    fn maybe_sweep(&self, data: &mut Store) {
        if self.writes_since_sweep.fetch_add(1, Ordering::Relaxed) + 1 >= SWEEP_INTERVAL {
            self.writes_since_sweep.store(0, Ordering::Relaxed);
            self.sweep_expired(data);
        }
    }

    /// Drop every expired entry across all flows, and remove any flow map left empty. O(total),
    /// but run only once per [`SWEEP_INTERVAL`] writes.
    fn sweep_expired(&self, data: &mut Store) {
        data.retain(|_flow_id, flow| {
            flow.retain(|_key, (_, expiry)| !self.is_expired(expiry));
            !flow.is_empty()
        });
    }

    /// Total entries currently held (including any expired-but-not-yet-swept), for reaper tests.
    #[cfg(test)]
    fn stored_entry_count(&self) -> usize {
        self.data.read().values().map(HashMap::len).sum()
    }

    /// Number of flow maps currently held, for reaper tests — the real high-cardinality growth
    /// vector is the outer map's key count, which `stored_entry_count` (a sum of inner lens) can't
    /// see (an empty flow map contributes 0).
    #[cfg(test)]
    fn stored_flow_count(&self) -> usize {
        self.data.read().len()
    }
}

impl FlowStore for InMemoryFlowStore {
    fn get(&self, flow_id: &str, key: &str) -> Result<Option<Value>> {
        // Use read lock for concurrent read access
        let data = self.data.read();
        match data.get(flow_id).and_then(|flow| flow.get(key)) {
            Some((value, expiry)) if !self.is_expired(expiry) => Ok(Some(value.clone())),
            _ => Ok(None),
        }
    }

    fn set(&self, flow_id: &str, key: &str, value: Value) -> Result<()> {
        let expiry = self.now() + self.default_ttl;
        let mut data = self.data.write();

        data.entry(flow_id.to_string())
            .or_default()
            .insert(key.to_string(), (value, Some(expiry)));
        self.maybe_sweep(&mut data);
        Ok(())
    }

    fn exists(&self, flow_id: &str, key: &str) -> Result<bool> {
        // Use read lock for concurrent read access
        let data = self.data.read();
        match data.get(flow_id).and_then(|flow| flow.get(key)) {
            Some((_, expiry)) if !self.is_expired(expiry) => Ok(true),
            _ => Ok(false),
        }
    }

    fn delete(&self, flow_id: &str, key: &str) -> Result<()> {
        let mut data = self.data.write();
        if let Some(flow) = data.get_mut(flow_id) {
            flow.remove(key);
            if flow.is_empty() {
                data.remove(flow_id);
            }
        }
        Ok(())
    }

    fn increment(&self, flow_id: &str, key: &str) -> Result<i64> {
        self.increment_by(flow_id, key, 1)
    }

    /// Atomic under the single write lock (issue #358), like `increment`: the read-modify-write
    /// happens with the lock held, so no interleaving window with a concurrent increment/set.
    fn increment_by(&self, flow_id: &str, key: &str, by: i64) -> Result<i64> {
        let expiry = self.now() + self.default_ttl;
        let mut data = self.data.write();
        let flow = data.entry(flow_id.to_string()).or_default();

        // Opportunistically clean up this specific key if expired, so a stale value isn't summed.
        self.remove_if_expired(flow, key);

        let current = match flow.get(key) {
            Some((Value::Number(n), _)) if n.is_i64() => n.as_i64().unwrap_or(0),
            _ => 0,
        };
        // `checked_add` so an overflow near i64::MAX errors (fail-loud) instead of panicking in
        // debug / wrapping in release — matching Redis's INCRBY, which also errors on overflow.
        let new_value = current.checked_add(by).ok_or_else(|| {
            anyhow::anyhow!("increment_by overflow: {current} + {by} exceeds i64 range")
        })?;

        flow.insert(
            key.to_string(),
            (Value::Number(new_value.into()), Some(expiry)),
        );
        self.maybe_sweep(&mut data);
        Ok(new_value)
    }

    fn set_ttl(&self, flow_id: &str, ttl_seconds: i64) -> Result<()> {
        let mut data = self.data.write();

        // Flow-level "expire now" (issue #530): a non-positive TTL drops every current key (and the
        // flow map), matching per-key `set_key_ttl(<=0)` and Redis `EXPIRE`.
        if ttl_seconds <= 0 {
            data.remove(flow_id);
            return Ok(());
        }

        let new_expiry = self.now() + Duration::from_secs(u64::try_from(ttl_seconds).unwrap_or(0));
        // Nested keying (issue #483): touch only this flow's entries, not the whole store.
        if let Some(flow) = data.get_mut(flow_id) {
            // Drop entries already logically expired but not yet swept before re-stamping (issue
            // #537): extending an expired entry to `now + ttl` would revive it, diverging from Redis
            // (where an expired key is simply gone). Mirror `set_key_ttl`'s per-key guard.
            flow.retain(|_key, (_, expiry)| !self.is_expired(expiry));
            if flow.is_empty() {
                // Don't leave an empty flow map behind (issue #483 growth vector).
                data.remove(flow_id);
            } else {
                for (_, expiry) in flow.values_mut() {
                    *expiry = Some(new_expiry);
                }
            }
        }

        Ok(())
    }

    fn set_key_ttl(&self, flow_id: &str, key: &str, ttl_seconds: i64) -> Result<bool> {
        let mut data = self.data.write();
        let Some(flow) = data.get_mut(flow_id) else {
            return Ok(false);
        };
        // An already-expired entry counts as absent: clean it up and report false.
        self.remove_if_expired(flow, key);

        // Non-positive TTL deletes the key immediately (Redis `EXPIRE` semantics), returning whether
        // it existed. Drop the flow map if that was its last key (issue #483's growth vector).
        if ttl_seconds <= 0 {
            let existed = flow.remove(key).is_some();
            if flow.is_empty() {
                data.remove(flow_id);
            }
            return Ok(existed);
        }

        let new_expiry = self.now() + Duration::from_secs(u64::try_from(ttl_seconds).unwrap_or(0));
        let extended = match flow.get_mut(key) {
            Some((_, expiry)) => {
                *expiry = Some(new_expiry);
                true
            }
            None => false,
        };
        // `remove_if_expired` above may have dropped the flow's last key; drop the now-empty flow
        // map so repeated calls on expired sole keys can't leak empty maps (issue #562 / #483).
        if flow.is_empty() {
            data.remove(flow_id);
        }
        Ok(extended)
    }

    fn clear_flow(&self, flow_id: &str) -> Result<()> {
        // Drop the whole flow map in one step (nested keying, issue #483); absent flow → no-op.
        self.data.write().remove(flow_id);
        Ok(())
    }

    /// Every flow id currently holding at least one live (non-expired) key (issue #374). Reads
    /// only — matches `get`/`exists`'s lazy-expiry rule (an expired entry is treated as absent on
    /// read, actual removal happens on write via `remove_if_expired`/the amortized sweeper) rather
    /// than inventing a second expiry rule here. A flow whose only keys have all expired is not
    /// listed, exactly as `exists` would report `false` for each of them.
    fn flow_ids(&self) -> Result<Option<Vec<String>>> {
        let data = self.data.read();
        let ids = data
            .iter()
            .filter(|(_, flow)| flow.values().any(|(_, expiry)| !self.is_expired(expiry)))
            .map(|(flow_id, _)| flow_id.clone())
            .collect();
        Ok(Some(ids))
    }

    /// Count of live (non-expired) keys under `flow_id` (issue #374) — `Some(0)` both for an empty
    /// flow and for one that was never created, matching `exists`'s "absent looks like expired"
    /// treatment rather than distinguishing the two.
    fn entry_count(&self, flow_id: &str) -> Result<Option<usize>> {
        let data = self.data.read();
        let count = data
            .get(flow_id)
            .map(|flow| {
                flow.values()
                    .filter(|(_, expiry)| !self.is_expired(expiry))
                    .count()
            })
            .unwrap_or(0);
        Ok(Some(count))
    }

    /// Atomic under the single write lock (issue #311): compare and write happen with no
    /// interleaving window, unlike the trait's get-then-set default.
    fn compare_and_set(
        &self,
        flow_id: &str,
        key: &str,
        expected: Option<&Value>,
        new: Value,
    ) -> Result<CasOutcome> {
        let mut data = self.data.write();
        let flow = data.entry(flow_id.to_string()).or_default();
        self.remove_if_expired(flow, key);

        let current = flow.get(key).map(|(value, _)| value);
        if current == expected {
            let expiry = self.now() + self.default_ttl;
            flow.insert(key.to_string(), (new, Some(expiry)));
            self.maybe_sweep(&mut data);
            Ok(CasOutcome::Applied)
        } else {
            let conflict = current.cloned();
            // A CAS that didn't write can still have created an empty flow via `entry().or_default()`
            // above; drop it so a failed compare never leaks a flow map.
            if flow.is_empty() {
                data.remove(flow_id);
            }
            Ok(CasOutcome::Conflict(conflict))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // Issue #360 Item 2: `rift script run` dumps the flow's keys after execution.
    #[test]
    fn keys_for_flow_lists_only_that_flows_non_expired_keys() {
        let store = InMemoryFlowStore::new(300);
        store.set("flow1", "attempts", json!(2)).unwrap();
        store.set("flow1", "last_status", json!("ok")).unwrap();
        store.set("other-flow", "unrelated", json!(true)).unwrap();

        let mut keys = store.keys_for_flow("flow1");
        keys.sort();
        assert_eq!(
            keys,
            vec!["attempts".to_string(), "last_status".to_string()]
        );

        assert!(store.keys_for_flow("no-such-flow").is_empty());
    }

    #[test]
    fn keys_for_flow_excludes_expired_keys() {
        let store = InMemoryFlowStore::new(1);
        store.set("flow1", "key1", json!("value1")).unwrap();
        assert_eq!(store.keys_for_flow("flow1"), vec!["key1".to_string()]);
        std::thread::sleep(Duration::from_secs(2));
        assert!(store.keys_for_flow("flow1").is_empty());
    }

    #[test]
    fn test_inmemory_get_set() {
        let store = InMemoryFlowStore::new(300);

        store.set("flow1", "key1", json!("value1")).unwrap();
        let value = store.get("flow1", "key1").unwrap();
        assert_eq!(value, Some(json!("value1")));
    }

    #[test]
    fn test_inmemory_exists() {
        let store = InMemoryFlowStore::new(300);

        store.set("flow1", "key1", json!("value1")).unwrap();
        assert!(store.exists("flow1", "key1").unwrap());
        assert!(!store.exists("flow1", "key2").unwrap());
    }

    #[test]
    fn test_inmemory_delete() {
        let store = InMemoryFlowStore::new(300);

        store.set("flow1", "key1", json!("value1")).unwrap();
        store.delete("flow1", "key1").unwrap();
        let value = store.get("flow1", "key1").unwrap();
        assert_eq!(value, None);
    }

    #[test]
    fn test_inmemory_increment() {
        let store = InMemoryFlowStore::new(300);

        let v1 = store.increment("flow1", "counter").unwrap();
        assert_eq!(v1, 1);

        let v2 = store.increment("flow1", "counter").unwrap();
        assert_eq!(v2, 2);

        let v3 = store.increment("flow1", "counter").unwrap();
        assert_eq!(v3, 3);
    }

    #[test]
    fn test_inmemory_ttl() {
        let store = InMemoryFlowStore::new(1); // 1 second TTL

        store.set("flow1", "key1", json!("value1")).unwrap();

        // Should exist immediately
        assert!(store.exists("flow1", "key1").unwrap());

        // Wait for expiry
        std::thread::sleep(Duration::from_secs(2));

        // Should be expired
        assert!(!store.exists("flow1", "key1").unwrap());
    }

    #[test]
    fn test_concurrent_set_get_same_flow() {
        use std::sync::Arc;
        use std::thread;

        let store = Arc::new(InMemoryFlowStore::new(300));
        let num_threads = 10;
        let iterations_per_thread = 100;

        // Spawn multiple threads that rapidly write and read from the same flow_id
        let handles: Vec<_> = (0..num_threads)
            .map(|thread_id| {
                let store_clone = Arc::clone(&store);
                thread::spawn(move || {
                    for i in 0..iterations_per_thread {
                        let value = json!(format!("thread_{}_value_{}", thread_id, i));

                        // Write
                        store_clone
                            .set("shared_flow", &format!("key_{thread_id}"), value.clone())
                            .unwrap();

                        // Immediately read back - should see the value we just wrote
                        let read_value = store_clone
                            .get("shared_flow", &format!("key_{thread_id}"))
                            .unwrap();

                        // This is the critical assertion: we should ALWAYS see our own write
                        assert_eq!(
                            read_value,
                            Some(value.clone()),
                            "Thread {thread_id} iteration {i}: Failed to read back own write"
                        );

                        // Also verify exists() sees it
                        assert!(
                            store_clone
                                .exists("shared_flow", &format!("key_{thread_id}"))
                                .unwrap(),
                            "Thread {thread_id} iteration {i}: exists() returned false after set"
                        );
                    }
                })
            })
            .collect();

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify final state: all keys should exist with their last written values
        for thread_id in 0..num_threads {
            let final_value = store
                .get("shared_flow", &format!("key_{thread_id}"))
                .unwrap();
            assert!(
                final_value.is_some(),
                "Thread {thread_id} final value missing"
            );
        }
    }

    #[test]
    fn test_concurrent_increment_contention() {
        use std::sync::Arc;
        use std::thread;

        let store = Arc::new(InMemoryFlowStore::new(300));
        let num_threads = 10;
        let increments_per_thread = 100;

        // Spawn multiple threads that all increment the same counter
        let handles: Vec<_> = (0..num_threads)
            .map(|_| {
                let store_clone = Arc::clone(&store);
                thread::spawn(move || {
                    for _ in 0..increments_per_thread {
                        store_clone.increment("shared_flow", "counter").unwrap();
                    }
                })
            })
            .collect();

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // The counter should equal exactly num_threads * increments_per_thread
        let final_value = store.get("shared_flow", "counter").unwrap();
        let expected = num_threads * increments_per_thread;

        assert_eq!(
            final_value,
            Some(json!(expected)),
            "Concurrent increments lost updates: expected {expected}, got {final_value:?}"
        );
    }

    // ===== increment_by (issue #358) =====

    #[test]
    fn increment_by_starts_at_zero_and_accumulates() {
        let store = InMemoryFlowStore::new(300);
        assert_eq!(store.increment_by("f", "k", 5).unwrap(), 5);
        assert_eq!(store.increment_by("f", "k", 5).unwrap(), 10);
    }

    #[test]
    fn increment_by_negative_decrements() {
        let store = InMemoryFlowStore::new(300);
        store.increment_by("f", "k", 10).unwrap();
        assert_eq!(store.increment_by("f", "k", -3).unwrap(), 7);
    }

    #[test]
    fn increment_delegates_to_increment_by_one() {
        let store = InMemoryFlowStore::new(300);
        assert_eq!(store.increment("f", "k").unwrap(), 1);
        assert_eq!(store.increment_by("f", "k", 1).unwrap(), 2);
    }

    #[test]
    fn increment_by_overflow_errors_not_panics() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "k", json!(i64::MAX)).unwrap();
        let result = store.increment_by("f", "k", 1);
        assert!(
            result.is_err(),
            "increment_by past i64::MAX must error, not panic/wrap"
        );
        // The stored value must be untouched by the failed op.
        assert_eq!(store.get("f", "k").unwrap(), Some(json!(i64::MAX)));
    }

    // ===== compare_and_set (issue #311) =====

    #[test]
    fn cas_expected_absent_applies() {
        let store = InMemoryFlowStore::new(300);
        let outcome = store
            .compare_and_set("f", "state", None, json!("paid"))
            .expect("cas");
        assert!(matches!(outcome, CasOutcome::Applied));
        assert_eq!(store.get("f", "state").expect("get"), Some(json!("paid")));
    }

    #[test]
    fn cas_expected_present_applies() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "state", json!("Started")).expect("set");
        let outcome = store
            .compare_and_set("f", "state", Some(&json!("Started")), json!("paid"))
            .expect("cas");
        assert!(matches!(outcome, CasOutcome::Applied));
        assert_eq!(store.get("f", "state").expect("get"), Some(json!("paid")));
    }

    #[test]
    fn cas_conflict_returns_current() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "state", json!("shipped")).expect("set");
        let outcome = store
            .compare_and_set("f", "state", Some(&json!("Started")), json!("paid"))
            .expect("cas");
        match outcome {
            CasOutcome::Conflict(current) => assert_eq!(current, Some(json!("shipped"))),
            CasOutcome::Applied => panic!("must conflict"),
        }
        assert_eq!(
            store.get("f", "state").expect("get"),
            Some(json!("shipped")),
            "conflict must not write"
        );

        let outcome = store
            .compare_and_set("f", "absent", Some(&json!("Started")), json!("paid"))
            .expect("cas");
        assert!(matches!(outcome, CasOutcome::Conflict(None)));
    }

    // AC1: N racers, one CAS each on the same expected state — exactly one Applied,
    // the rest Conflict, final state legal.
    #[test]
    fn cas_race_exactly_one_winner() {
        use std::sync::Barrier;
        use std::thread;

        let store = Arc::new(InMemoryFlowStore::new(300));
        store.set("f", "state", json!("Started")).expect("seed");
        let racers = 64;
        let barrier = Arc::new(Barrier::new(racers));

        let handles: Vec<_> = (0..racers)
            .map(|i| {
                let store = Arc::clone(&store);
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    let outcome = store
                        .compare_and_set(
                            "f",
                            "state",
                            Some(&json!("Started")),
                            json!(format!("paid-by-{i}")),
                        )
                        .expect("cas");
                    matches!(outcome, CasOutcome::Applied)
                })
            })
            .collect();

        let applied = handles
            .into_iter()
            .map(|h| h.join().expect("thread"))
            .filter(|won| *won)
            .count();
        assert_eq!(applied, 1, "exactly one racer must win the CAS");

        let final_state = store.get("f", "state").expect("get").expect("present");
        let s = final_state.as_str().expect("string state");
        assert!(s.starts_with("paid-by-"), "final state legal, got {s}");
    }

    // ===== reaping + targeted set_ttl (issue #483) =====

    // The amortized sweeper must actually reclaim expired entries across many short-lived flows,
    // so a long-running store doesn't grow without bound. Before this fix expired entries were
    // only ever removed on a same-key rewrite, so high-cardinality flow ids leaked forever.
    #[test]
    fn sweeper_reaps_expired_entries_across_flows() {
        let store = InMemoryFlowStore::new(1); // 1s TTL

        // One entry in each of many distinct, never-rewritten flows.
        for i in 0..300 {
            store.set(&format!("flow-{i}"), "k", json!(i)).unwrap();
        }
        assert_eq!(store.stored_entry_count(), 300);

        // Let them all expire.
        std::thread::sleep(Duration::from_secs(2));

        // Drive enough writes (to a single overwritten key) to trigger at least one sweep now
        // that the 300 are expired.
        for _ in 0..(SWEEP_INTERVAL * 2) {
            store.set("sink", "k", json!(1)).unwrap();
        }

        // Only the live sink entry survives; the 300 expired flows were reaped — asserted on both
        // the entry count AND the outer flow-map count (the real high-cardinality growth vector:
        // an empty flow map left in the outer HashMap contributes 0 to stored_entry_count).
        assert_eq!(
            store.stored_entry_count(),
            1,
            "expired entries across distinct flows must be reaped, leaving only the live one"
        );
        assert_eq!(
            store.stored_flow_count(),
            1,
            "empty flow maps for the expired flows must be dropped from the outer map"
        );
    }

    // delete of a flow's sole key must drop the flow map from the outer store immediately, not
    // leave an empty map to accumulate (issue #483's growth vector).
    #[test]
    fn delete_last_key_removes_the_flow_map() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "k", json!("v")).unwrap();
        assert_eq!(store.stored_flow_count(), 1);
        store.delete("f", "k").unwrap();
        assert_eq!(
            store.stored_flow_count(),
            0,
            "deleting a flow's last key must remove its flow map, not leave it empty"
        );
    }

    // Issue #475: the in-memory store never blocks, so the request path must NOT offload its
    // calls to spawn_blocking (that hop would be pure overhead on the common backend).
    #[test]
    fn in_memory_store_is_not_blocking() {
        assert!(!InMemoryFlowStore::new(300).is_blocking());
    }

    // A conflicting CAS on a brand-new flow_id must not leak the empty flow map that
    // `entry().or_default()` transiently created.
    #[test]
    fn cas_conflict_on_new_flow_leaves_no_flow_map() {
        let store = InMemoryFlowStore::new(300);
        // Expect a value on a flow that doesn't exist -> current None != expected -> Conflict.
        let outcome = store
            .compare_and_set("ghost", "k", Some(&json!("nope")), json!("x"))
            .expect("cas");
        assert!(matches!(outcome, CasOutcome::Conflict(None)));
        assert_eq!(
            store.stored_flow_count(),
            0,
            "a conflicting CAS on a new flow must not leak an empty flow map"
        );
    }

    // set_ttl must extend only the target flow's entries — and, per #483, do so without scanning
    // unrelated flows. Verified behaviorally: a sibling flow keeps its short TTL and expires.
    #[test]
    fn set_ttl_only_affects_target_flow() {
        let store = InMemoryFlowStore::new(1); // 1s default TTL
        store.set("keep", "k", json!("v")).unwrap();
        store.set("drop", "k", json!("v")).unwrap();

        // Extend only "keep" well past the sleep below.
        store.set_ttl("keep", 100).unwrap();

        std::thread::sleep(Duration::from_secs(2));

        assert!(
            store.exists("keep", "k").unwrap(),
            "set_ttl should have extended the target flow"
        );
        assert!(
            !store.exists("drop", "k").unwrap(),
            "a sibling flow must keep its original (now expired) TTL"
        );
    }

    // Issue #537: flow-level set_ttl(positive) must not revive an entry that is already logically
    // expired but not yet swept. Re-stamping its expiry to `now + ttl` would resurrect it, diverging
    // from Redis (where an expired key is simply gone). The single expired key is also the flow's
    // last, so the now-empty flow map must be dropped (issue #483 growth vector), not left behind.
    #[test]
    fn set_ttl_does_not_revive_expired_entries() {
        let store = InMemoryFlowStore::new(1); // 1s default TTL
        store.set("f", "k", json!("v")).unwrap();
        std::thread::sleep(Duration::from_secs(2)); // expired, but < SWEEP_INTERVAL writes → unswept

        store.set_ttl("f", 100).unwrap();

        assert!(
            !store.exists("f", "k").unwrap(),
            "an expired-but-unswept entry must not be revived by flow-level set_ttl"
        );
        assert_eq!(store.get("f", "k").unwrap(), None);
        assert_eq!(
            store.stored_flow_count(),
            0,
            "pruning the flow's only (expired) entry must drop the empty flow map"
        );
    }

    // set_ttl over a flow holding both a live and an expired-but-unswept entry must extend only the
    // live one and drop the expired one — the live key survives a subsequent expiry window, the
    // expired key stays gone.
    #[test]
    fn set_ttl_extends_live_and_drops_expired_in_same_flow() {
        let store = InMemoryFlowStore::new(1); // 1s default TTL
        store.set("f", "old", json!("v")).unwrap();
        std::thread::sleep(Duration::from_secs(2)); // "old" now expired, still unswept
        store.set("f", "new", json!("v")).unwrap(); // fresh 1s TTL, live

        store.set_ttl("f", 100).unwrap();

        std::thread::sleep(Duration::from_secs(2)); // outlives the original 1s TTLs, not the new 100s

        assert!(
            store.exists("f", "new").unwrap(),
            "the live key must have its TTL extended by set_ttl"
        );
        assert!(
            !store.exists("f", "old").unwrap(),
            "the expired key must be dropped, not revived, by set_ttl"
        );
    }

    // ===== per-key ttl + clear_flow (issue #530) =====

    // set_key_ttl extends only the target key: a short-lived sibling key in the same flow expires
    // while the extended key survives.
    #[test]
    fn set_key_ttl_extends_only_the_target_key() {
        let store = InMemoryFlowStore::new(1); // 1s default TTL
        store.set("f", "keep", json!("v")).unwrap();
        store.set("f", "drop", json!("v")).unwrap();

        assert!(
            store.set_key_ttl("f", "keep", 100).unwrap(),
            "extending an existing key returns true"
        );

        std::thread::sleep(Duration::from_secs(2));

        assert!(store.exists("f", "keep").unwrap(), "target key extended");
        assert!(
            !store.exists("f", "drop").unwrap(),
            "sibling key keeps its original (now expired) TTL"
        );
    }

    // Issue #562: a positive-TTL set_key_ttl on an already-expired *sole* key must drop the now-
    // empty flow map, not leave it behind — otherwise repeated calls across distinct flow ids leak
    // empty maps in the outer store without bound (the #483 growth vector).
    #[test]
    fn set_key_ttl_positive_on_expired_sole_key_drops_empty_flow_map() {
        let store = InMemoryFlowStore::new(1); // 1s default TTL
        store.set("f", "only", json!("v")).unwrap();
        std::thread::sleep(Duration::from_secs(2)); // the sole key is now expired

        assert!(
            !store.set_key_ttl("f", "only", 100).unwrap(),
            "there is no live key to extend"
        );
        assert_eq!(
            store.stored_flow_count(),
            0,
            "the emptied flow map must be dropped, not leaked"
        );
    }

    #[test]
    fn set_key_ttl_absent_key_returns_false() {
        let store = InMemoryFlowStore::new(300);
        assert!(!store.set_key_ttl("f", "nope", 60).unwrap());
        // A flow that exists but lacks the key also returns false, and must not create the key.
        store.set("f", "other", json!(1)).unwrap();
        assert!(!store.set_key_ttl("f", "nope", 60).unwrap());
        assert!(!store.exists("f", "nope").unwrap());
    }

    #[test]
    fn set_key_ttl_non_positive_deletes_the_key() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "k", json!("v")).unwrap();
        assert!(
            store.set_key_ttl("f", "k", 0).unwrap(),
            "deleting an existing key returns true"
        );
        assert!(!store.exists("f", "k").unwrap(), "key deleted by ttl<=0");
        // Deleting the flow's last key drops the flow map (no empty-map leak, issue #483).
        assert_eq!(store.stored_flow_count(), 0);
        // Deleting an absent key returns false.
        assert!(!store.set_key_ttl("f", "k", -5).unwrap());
    }

    #[test]
    fn set_key_ttl_on_expired_key_returns_false() {
        let store = InMemoryFlowStore::new(1);
        store.set("f", "k", json!("v")).unwrap();
        std::thread::sleep(Duration::from_secs(2));
        assert!(
            !store.set_key_ttl("f", "k", 100).unwrap(),
            "an already-expired key is absent, so per-key ttl returns false"
        );
    }

    // Flow-level ttl(<=0) expires every current key (issue #530), matching per-key semantics.
    #[test]
    fn set_ttl_non_positive_expires_whole_flow() {
        let store = InMemoryFlowStore::new(300);
        store.set("f", "a", json!(1)).unwrap();
        store.set("f", "b", json!(2)).unwrap();
        store.set_ttl("f", 0).unwrap();
        assert!(!store.exists("f", "a").unwrap());
        assert!(!store.exists("f", "b").unwrap());
        assert_eq!(store.stored_flow_count(), 0, "flow map dropped");
    }

    #[test]
    fn clear_flow_drops_only_the_target_flow() {
        let store = InMemoryFlowStore::new(300);
        store.set("keep", "k", json!(1)).unwrap();
        store.set("drop", "k1", json!(1)).unwrap();
        store.set("drop", "k2", json!(2)).unwrap();

        store.clear_flow("drop").unwrap();

        assert!(
            store.keys_for_flow("drop").is_empty(),
            "target flow emptied"
        );
        assert!(store.exists("keep", "k").unwrap(), "sibling flow untouched");
        // No empty flow-map left behind for the cleared flow (issue #483 growth vector).
        assert_eq!(store.stored_flow_count(), 1);
        // Clearing an absent flow is an idempotent no-op success.
        assert!(store.clear_flow("no-such-flow").is_ok());
    }

    // ===== flow_ids / entry_count (issue #374) =====

    // An empty store can still enumerate — it just has nothing to report. `Some(vec![])`, not
    // `None`: the store CAN answer, distinct from a backend that cannot enumerate at all.
    #[test]
    fn flow_ids_on_empty_store_is_some_empty() {
        let store = InMemoryFlowStore::new(300);
        assert_eq!(store.flow_ids().unwrap(), Some(Vec::new()));
    }

    #[test]
    fn flow_ids_lists_each_flow_exactly_once() {
        let store = InMemoryFlowStore::new(300);
        store.set("flow1", "a", json!(1)).unwrap();
        store.set("flow1", "b", json!(2)).unwrap();
        store.set("flow2", "a", json!(1)).unwrap();

        let mut ids = store
            .flow_ids()
            .unwrap()
            .expect("in-memory store enumerates");
        ids.sort();
        assert_eq!(ids, vec!["flow1".to_string(), "flow2".to_string()]);
    }

    #[test]
    fn clear_flow_removes_the_id_from_flow_ids() {
        let store = InMemoryFlowStore::new(300);
        store.set("keep", "k", json!(1)).unwrap();
        store.set("drop", "k", json!(1)).unwrap();

        store.clear_flow("drop").unwrap();

        let ids = store
            .flow_ids()
            .unwrap()
            .expect("in-memory store enumerates");
        assert_eq!(ids, vec!["keep".to_string()]);
    }

    #[test]
    fn entry_count_counts_only_the_target_flows_keys() {
        let store = InMemoryFlowStore::new(300);
        store.set("f1", "a", json!(1)).unwrap();
        store.set("f1", "b", json!(2)).unwrap();
        store.set("f2", "x", json!(1)).unwrap();

        assert_eq!(store.entry_count("f1").unwrap(), Some(2));
        assert_eq!(store.entry_count("f2").unwrap(), Some(1));
    }

    // `Some(0)`, not `None`: the store can answer, and the answer is "no keys".
    #[test]
    fn entry_count_for_absent_flow_is_some_zero() {
        let store = InMemoryFlowStore::new(300);
        assert_eq!(store.entry_count("no-such-flow").unwrap(), Some(0));
    }

    #[test]
    fn expired_flow_is_absent_from_flow_ids_and_entry_count() {
        let store = InMemoryFlowStore::new(1); // 1s TTL
        store.set("f", "k", json!("v")).unwrap();

        // Live: appears in both.
        assert_eq!(store.flow_ids().unwrap(), Some(vec!["f".to_string()]));
        assert_eq!(store.entry_count("f").unwrap(), Some(1));

        std::thread::sleep(Duration::from_secs(2));

        // Expired: gone from both, matching get/exists's lazy-expiry rule.
        assert_eq!(store.flow_ids().unwrap(), Some(Vec::new()));
        assert_eq!(store.entry_count("f").unwrap(), Some(0));
    }

    // A minimal third-party-style store that overrides nothing beyond the required methods must
    // still compile and get `None` from both — proving the trait defaults are what an embedder
    // gets without opting in.
    #[test]
    fn default_flow_ids_and_entry_count_are_none() {
        use std::sync::Mutex;
        struct Bare(Mutex<()>);
        impl FlowStore for Bare {
            fn get(&self, _: &str, _: &str) -> Result<Option<Value>> {
                let _g = self.0.lock();
                Ok(None)
            }
            fn set(&self, _: &str, _: &str, _: Value) -> Result<()> {
                Ok(())
            }
            fn exists(&self, _: &str, _: &str) -> Result<bool> {
                Ok(false)
            }
            fn delete(&self, _: &str, _: &str) -> Result<()> {
                Ok(())
            }
            fn increment(&self, _: &str, _: &str) -> Result<i64> {
                Ok(1)
            }
            fn set_ttl(&self, _: &str, _: i64) -> Result<()> {
                Ok(())
            }
        }
        let store = Bare(Mutex::new(()));
        assert_eq!(store.flow_ids().unwrap(), None);
        assert_eq!(store.entry_count("f").unwrap(), None);
    }

    // Issue #1754: a store built `with_clock` sets and checks expiry on that clock, so a test steps
    // past a TTL instead of sleeping through it.
    #[test]
    fn ttls_follow_the_injected_clock() {
        let now = Arc::new(parking_lot::Mutex::new(
            SystemTime::UNIX_EPOCH + Duration::from_secs(1_000),
        ));
        let read = Arc::clone(&now);
        let store = InMemoryFlowStore::with_clock(60, Arc::new(move || *read.lock()));
        store.set("f", "k", serde_json::json!(1)).expect("set");
        *now.lock() += Duration::from_secs(59);
        assert_eq!(
            store.get("f", "k").expect("get"),
            Some(serde_json::json!(1))
        );
        *now.lock() += Duration::from_secs(2);
        assert_eq!(
            store.get("f", "k").expect("get"),
            None,
            "61 s later the key is gone"
        );
        // An explicit key TTL follows it too.
        store.set("f", "j", serde_json::json!(2)).expect("set");
        assert!(store.set_key_ttl("f", "j", 10).expect("ttl"));
        *now.lock() += Duration::from_secs(11);
        assert!(!store.exists("f", "j").expect("exists"));
    }
}