duroxide-pg-opt 0.1.22

A PostgreSQL-based provider implementation for Duroxide, a durable task orchestration framework
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
//! Long-polling notifier for PostgreSQL provider.
//!
//! The notifier thread listens for PostgreSQL NOTIFY events and manages timer heaps
//! to wake dispatchers at the right time, reducing idle database polling.

use sqlx::postgres::PgListener;
use sqlx::PgPool;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::sync::{oneshot, Notify};
use tokio::time::sleep_until;
use tracing::{debug, error, info, warn};

#[cfg(feature = "test-fault-injection")]
use crate::fault_injection::FaultInjector;

/// Configuration for long-polling behavior.
#[derive(Debug, Clone)]
pub struct LongPollConfig {
    /// Enable long-polling (LISTEN/NOTIFY based).
    /// Default: true
    pub enabled: bool,

    /// Interval for querying upcoming timers from the database.
    /// The notifier queries for work with visible_at within this window.
    /// Also serves as a safety net to catch any missed NOTIFYs.
    /// Default: 60 seconds
    pub notifier_poll_interval: Duration,

    /// Grace period added to timer delays to ensure we never wake early.
    /// Accounts for tokio timer jitter and processing overhead.
    /// delay = (visible_at - now) + timer_grace_period
    /// Default: 100ms
    pub timer_grace_period: Duration,
}

impl Default for LongPollConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            notifier_poll_interval: Duration::from_secs(60),
            timer_grace_period: Duration::from_millis(100),
        }
    }
}

/// Result of a refresh query containing upcoming timer timestamps.
struct RefreshResult {
    orch_timers: Vec<i64>,   // visible_at as epoch ms
    worker_timers: Vec<i64>, // visible_at as epoch ms
}

/// The notifier thread that manages LISTEN/NOTIFY and timer heaps.
pub struct Notifier {
    /// PostgreSQL connection for LISTEN
    pg_listener: PgListener,
    pool: PgPool,
    schema_name: String,

    /// Timer heaps (min-heap by fire time)
    orch_heap: BinaryHeap<Reverse<Instant>>,
    worker_heap: BinaryHeap<Reverse<Instant>>,

    /// Dispatcher wake channels
    orch_notify: Arc<Notify>,
    worker_notify: Arc<Notify>,

    /// Refresh scheduling
    next_refresh: Instant,

    /// Active refresh task (if any)
    pending_refresh: Option<oneshot::Receiver<RefreshResult>>,

    /// Configuration
    config: LongPollConfig,

    /// Fault injector for testing (only available with test-fault-injection feature)
    #[cfg(feature = "test-fault-injection")]
    fault_injector: Option<Arc<FaultInjector>>,
}

impl Notifier {
    /// Create a new notifier and subscribe to NOTIFY channels.
    pub async fn new(
        pool: PgPool,
        schema_name: String,
        orch_notify: Arc<Notify>,
        worker_notify: Arc<Notify>,
        config: LongPollConfig,
    ) -> Result<Self, sqlx::Error> {
        Self::new_internal(
            pool,
            schema_name,
            orch_notify,
            worker_notify,
            config,
            #[cfg(feature = "test-fault-injection")]
            None,
        )
        .await
    }

    /// Create a new notifier with fault injection for testing.
    #[cfg(feature = "test-fault-injection")]
    pub async fn new_with_fault_injection(
        pool: PgPool,
        schema_name: String,
        orch_notify: Arc<Notify>,
        worker_notify: Arc<Notify>,
        config: LongPollConfig,
        fault_injector: Arc<FaultInjector>,
    ) -> Result<Self, sqlx::Error> {
        Self::new_internal(
            pool,
            schema_name,
            orch_notify,
            worker_notify,
            config,
            Some(fault_injector),
        )
        .await
    }

    async fn new_internal(
        pool: PgPool,
        schema_name: String,
        orch_notify: Arc<Notify>,
        worker_notify: Arc<Notify>,
        config: LongPollConfig,
        #[cfg(feature = "test-fault-injection")] fault_injector: Option<Arc<FaultInjector>>,
    ) -> Result<Self, sqlx::Error> {
        let pg_listener = PgListener::connect_with(&pool).await?;

        let mut notifier = Self {
            pg_listener,
            pool,
            schema_name,
            orch_heap: BinaryHeap::new(),
            worker_heap: BinaryHeap::new(),
            orch_notify,
            worker_notify,
            next_refresh: Instant::now(), // Immediate first refresh
            pending_refresh: None,
            config,
            #[cfg(feature = "test-fault-injection")]
            fault_injector,
        };

        notifier.subscribe_channels().await?;

        info!(
            target = "duroxide::providers::postgres::notifier",
            schema = %notifier.schema_name,
            "Notifier started, listening for NOTIFY events"
        );

        Ok(notifier)
    }

    /// Subscribe to NOTIFY channels. Used by new() and handle_reconnect().
    async fn subscribe_channels(&mut self) -> Result<(), sqlx::Error> {
        let orch_channel = format!("{}_orch_work", self.schema_name);
        let worker_channel = format!("{}_worker_work", self.schema_name);

        self.pg_listener.listen(&orch_channel).await?;
        self.pg_listener.listen(&worker_channel).await?;

        debug!(
            target = "duroxide::providers::postgres::notifier",
            orch_channel = %orch_channel,
            worker_channel = %worker_channel,
            "Subscribed to NOTIFY channels"
        );

        Ok(())
    }

    /// Main loop - runs until the notifier is dropped.
    pub async fn run(&mut self) {
        loop {
            // Check for fault injection: should we panic?
            #[cfg(feature = "test-fault-injection")]
            if let Some(ref fi) = self.fault_injector {
                if fi.should_notifier_panic() {
                    panic!("Fault injection: notifier panic triggered");
                }
                if fi.should_reconnect() {
                    warn!(
                        target = "duroxide::providers::postgres::notifier",
                        "Fault injection: forcing reconnect"
                    );
                    self.handle_reconnect().await;
                    continue;
                }
            }

            // Calculate next wake time
            let next_timer = self.earliest_timer();
            let refresh_in_progress = self.pending_refresh.is_some();

            let next_wake = if refresh_in_progress {
                // Don't wait for refresh time if query already running
                next_timer.unwrap_or_else(|| Instant::now() + Duration::from_secs(60))
            } else {
                match next_timer {
                    Some(t) => t.min(self.next_refresh),
                    None => self.next_refresh,
                }
            };

            tokio::select! {
                // PostgreSQL NOTIFY received
                result = self.pg_listener.recv() => {
                    match result {
                        Ok(notification) => {
                            self.handle_notify(notification);
                        }
                        Err(e) => {
                            warn!(
                                target = "duroxide::providers::postgres::notifier",
                                error = %e,
                                "LISTEN connection error, reconnecting..."
                            );
                            self.handle_reconnect().await;
                        }
                    }
                }

                // Timer or refresh time reached
                _ = sleep_until(next_wake.into()) => {
                    self.pop_and_wake_expired_timers();
                    self.maybe_start_refresh();
                }

                // Refresh query completed (non-blocking)
                Some(result) = async {
                    match &mut self.pending_refresh {
                        Some(rx) => rx.await.ok(),
                        None => std::future::pending().await,
                    }
                } => {
                    self.pending_refresh = None;
                    self.handle_refresh_result(result);
                }
            }
        }
    }

    /// Find the earliest timer across both heaps.
    fn earliest_timer(&self) -> Option<Instant> {
        let orch = self.orch_heap.peek().map(|r| r.0);
        let worker = self.worker_heap.peek().map(|r| r.0);
        match (orch, worker) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (Some(a), None) => Some(a),
            (None, Some(b)) => Some(b),
            (None, None) => None,
        }
    }

    /// Handle a NOTIFY from PostgreSQL.
    fn handle_notify(&mut self, notification: sqlx::postgres::PgNotification) {
        let now_ms = current_epoch_ms();
        let window_end_ms = now_ms + self.config.notifier_poll_interval.as_millis() as i64;
        let now_instant = Instant::now();

        let is_orch = notification.channel().ends_with("_orch_work");

        let action = parse_notify_action(
            notification.payload(),
            now_ms,
            window_end_ms,
            self.config.timer_grace_period,
            now_instant,
        );

        match action {
            NotifyAction::WakeNow => {
                debug!(
                    target = "duroxide::providers::postgres::notifier",
                    channel = %notification.channel(),
                    payload = %notification.payload(),
                    "Immediate work, waking dispatchers"
                );
                self.wake_dispatchers(is_orch);
            }
            NotifyAction::AddTimer { fire_at } => {
                debug!(
                    target = "duroxide::providers::postgres::notifier",
                    channel = %notification.channel(),
                    payload = %notification.payload(),
                    "Future timer, adding to heap"
                );
                if is_orch {
                    self.orch_heap.push(Reverse(fire_at));
                } else {
                    self.worker_heap.push(Reverse(fire_at));
                }
            }
            NotifyAction::Ignore => {
                debug!(
                    target = "duroxide::providers::postgres::notifier",
                    channel = %notification.channel(),
                    payload = %notification.payload(),
                    "Timer beyond window, ignoring"
                );
            }
        }
    }

    /// Wake all waiting dispatchers for the given queue type.
    ///
    /// Worker uses `notify_waiters()` so that ALL worker slots wake up.
    /// This is required for session routing: a session-bound item can only
    /// be served by the slot that owns the session, so waking just one
    /// slot (which might be the wrong one) would cause a deadlock.
    fn wake_dispatchers(&self, is_orch: bool) {
        if is_orch {
            self.orch_notify.notify_one();
        } else {
            self.worker_notify.notify_waiters();
        }
    }

    /// Pop and fire any expired timers from both heaps.
    fn pop_and_wake_expired_timers(&mut self) {
        let now = Instant::now();

        // Pop expired orchestrator timers
        while let Some(Reverse(fire_at)) = self.orch_heap.peek() {
            if *fire_at <= now {
                self.orch_heap.pop();
                self.orch_notify.notify_one();
            } else {
                break;
            }
        }

        // Pop expired worker timers (notify_waiters for session routing correctness)
        while let Some(Reverse(fire_at)) = self.worker_heap.peek() {
            if *fire_at <= now {
                self.worker_heap.pop();
                self.worker_notify.notify_waiters();
            } else {
                break;
            }
        }
    }

    /// Start a refresh query if not already in progress and it's time.
    fn maybe_start_refresh(&mut self) {
        if self.pending_refresh.is_some() || Instant::now() < self.next_refresh {
            return;
        }

        let (tx, rx) = oneshot::channel();
        self.pending_refresh = Some(rx);

        let pool = self.pool.clone();
        let schema = self.schema_name.clone();
        let now_ms = current_epoch_ms();
        let window_end_ms = now_ms + self.config.notifier_poll_interval.as_millis() as i64;

        // Get fault injection parameters before spawning
        #[cfg(feature = "test-fault-injection")]
        let fault_injector = self.fault_injector.clone();

        debug!(
            target = "duroxide::providers::postgres::notifier",
            now_ms = now_ms,
            window_end_ms = window_end_ms,
            "Starting refresh query"
        );

        tokio::spawn(async move {
            // Check for fault injection: add delay before refresh
            #[cfg(feature = "test-fault-injection")]
            if let Some(ref fi) = fault_injector {
                let delay = fi.get_refresh_delay();
                if !delay.is_zero() {
                    tokio::time::sleep(delay).await;
                }
            }

            // Check for fault injection: should refresh error?
            #[cfg(feature = "test-fault-injection")]
            if let Some(ref fi) = fault_injector {
                if fi.should_refresh_error() {
                    warn!(
                        target = "duroxide::providers::postgres::notifier",
                        "Fault injection: simulating refresh error"
                    );
                    // Send empty result to simulate error recovery
                    let _ = tx.send(RefreshResult {
                        orch_timers: Vec::new(),
                        worker_timers: Vec::new(),
                    });
                    return;
                }
            }

            // Query for upcoming timers in both queues
            // Use Rust clock ($1) for "now" comparison, not database NOW()
            let orch_timers = sqlx::query_scalar::<_, i64>(&format!(
                "SELECT (EXTRACT(EPOCH FROM visible_at) * 1000)::BIGINT
                 FROM {schema}.orchestrator_queue
                 WHERE (EXTRACT(EPOCH FROM visible_at) * 1000)::BIGINT > $1
                   AND (EXTRACT(EPOCH FROM visible_at) * 1000)::BIGINT <= $2
                   AND lock_token IS NULL"
            ))
            .bind(now_ms)
            .bind(window_end_ms)
            .fetch_all(&pool)
            .await
            .unwrap_or_default();

            // TODO: After adding visible_at column to worker_queue (see docs/WORKER_VISIBLE_AT_PROPOSAL.md),
            // query for future worker timers here similar to orch_timers above.
            // Worker queue items are currently always immediately available (no visible_at column)
            let worker_timers: Vec<i64> = Vec::new();

            // Send result (ignore error if receiver dropped)
            let _ = tx.send(RefreshResult {
                orch_timers,
                worker_timers,
            });
        });
    }

    /// Process the result of a refresh query.
    fn handle_refresh_result(&mut self, result: RefreshResult) {
        let now_ms = current_epoch_ms();
        let now_instant = Instant::now();

        debug!(
            target = "duroxide::providers::postgres::notifier",
            orch_count = result.orch_timers.len(),
            worker_count = result.worker_timers.len(),
            "Refresh query completed"
        );

        // Add orchestrator timers
        for fire_at in timers_from_refresh(
            &result.orch_timers,
            now_ms,
            self.config.timer_grace_period,
            now_instant,
        ) {
            self.orch_heap.push(Reverse(fire_at));
        }

        // Add worker timers
        for fire_at in timers_from_refresh(
            &result.worker_timers,
            now_ms,
            self.config.timer_grace_period,
            now_instant,
        ) {
            self.worker_heap.push(Reverse(fire_at));
        }

        // Schedule next refresh
        self.next_refresh = Instant::now() + self.config.notifier_poll_interval;
    }

    /// Handle reconnection after a LISTEN connection failure.
    async fn handle_reconnect(&mut self) {
        // Backoff before reconnect attempt
        tokio::time::sleep(Duration::from_secs(1)).await;

        // Reconnect and resubscribe
        match PgListener::connect_with(&self.pool).await {
            Ok(listener) => {
                self.pg_listener = listener;

                if self.subscribe_channels().await.is_ok() {
                    info!(
                        target = "duroxide::providers::postgres::notifier",
                        "Reconnected to PostgreSQL LISTEN"
                    );

                    // Wake all dispatchers to catch any missed NOTIFYs during disconnect
                    self.orch_notify.notify_one();
                    self.worker_notify.notify_waiters();

                    // Force immediate refresh to rebuild timer heaps
                    self.next_refresh = Instant::now();
                }
            }
            Err(e) => {
                error!(
                    target = "duroxide::providers::postgres::notifier",
                    error = %e,
                    "Failed to reconnect, will retry on next loop iteration"
                );
            }
        }
        // If reconnect fails, loop will call handle_reconnect again on next recv() error
    }
}

/// Get current time as epoch milliseconds (Rust clock).
fn current_epoch_ms() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as i64
}

/// Action determined by parsing a NOTIFY payload.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotifyAction {
    /// Work is immediately visible, wake dispatchers now.
    WakeNow,
    /// Work will be visible in the future, add timer.
    AddTimer { fire_at: Instant },
    /// Work is too far in the future, ignore (refresh will catch it).
    Ignore,
}

/// Parse a NOTIFY payload and determine what action to take.
///
/// This is a pure function for testability.
pub fn parse_notify_action(
    payload: &str,
    now_ms: i64,
    window_end_ms: i64,
    grace_period: Duration,
    now_instant: Instant,
) -> NotifyAction {
    let visible_at_ms: i64 = payload.parse().unwrap_or(0); // 0 = treat as immediate

    if visible_at_ms <= now_ms {
        // Immediately visible (or past) → wake dispatchers now
        NotifyAction::WakeNow
    } else if visible_at_ms <= window_end_ms {
        // Future timer within current window → schedule a timer
        let delay_ms = (visible_at_ms - now_ms) + grace_period.as_millis() as i64;
        let fire_at = now_instant + Duration::from_millis(delay_ms as u64);
        NotifyAction::AddTimer { fire_at }
    } else {
        // Beyond window → ignore, refresh will catch it
        NotifyAction::Ignore
    }
}

/// Calculate timers to add from refresh result.
///
/// Returns Vec of (fire_at Instant) for timers that should be added to the heap.
/// Filters out any timers that are already expired.
///
/// NOTE: There's an edge case where items could be missed if:
/// 1. Item had visible_at just barely in the future when query started
/// 2. Query takes longer than that margin to complete
/// 3. By the time we process results, the item has expired (delay <= 0)
///
/// This is acceptable because:
/// - The original NOTIFY when the item was inserted should have already woken dispatchers
/// - poll_timeout (default 5s) provides a safety net for any missed items
/// - This edge case is rare (query must be slow AND item must be near-immediate)
/// - Adding an extra wake for expired items isn't worth the overhead
pub fn timers_from_refresh(
    visible_at_times: &[i64],
    now_ms: i64,
    grace_period: Duration,
    now_instant: Instant,
) -> Vec<Instant> {
    visible_at_times
        .iter()
        .filter_map(|&visible_at_ms| {
            let delay_ms = (visible_at_ms - now_ms) + grace_period.as_millis() as i64;
            if delay_ms > 0 {
                Some(now_instant + Duration::from_millis(delay_ms as u64))
            } else {
                None
            }
        })
        .collect()
}

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

    // =======================================================================
    // Basic Tests
    // =======================================================================

    #[test]
    fn test_current_epoch_ms() {
        let ms = current_epoch_ms();
        // Should be a reasonable timestamp (after 2020)
        assert!(ms > 1_577_836_800_000); // 2020-01-01
        assert!(ms < 2_524_608_000_000); // 2050-01-01
    }

    #[test]
    fn test_longpoll_config_default() {
        let config = LongPollConfig::default();
        assert!(config.enabled);
        assert_eq!(config.notifier_poll_interval, Duration::from_secs(60));
        assert_eq!(config.timer_grace_period, Duration::from_millis(100));
    }

    // =======================================================================
    // Category 1: NOTIFY Handling Tests
    // =======================================================================

    #[test]
    fn notify_immediate_work_wakes_dispatchers() {
        // NOTIFY with visible_at = now should wake dispatchers immediately
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &now_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        assert_eq!(action, NotifyAction::WakeNow);
    }

    #[test]
    fn notify_past_visible_at_wakes_immediately() {
        // NOTIFY with visible_at in the past should wake immediately
        let now_ms = 1_700_000_000_000i64;
        let past_ms = now_ms - 5_000; // 5 seconds ago
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &past_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        assert_eq!(action, NotifyAction::WakeNow);
    }

    #[test]
    fn notify_future_timer_adds_to_heap() {
        // NOTIFY with visible_at in the future (within window) should add timer
        let now_ms = 1_700_000_000_000i64;
        let future_ms = now_ms + 30_000; // 30 seconds from now
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &future_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        match action {
            NotifyAction::AddTimer { fire_at } => {
                // fire_at should be approximately now + 30s + 100ms grace
                let expected_delay = Duration::from_millis(30_100);
                let actual_delay = fire_at.duration_since(now_instant);
                assert!(
                    actual_delay >= expected_delay - Duration::from_millis(10)
                        && actual_delay <= expected_delay + Duration::from_millis(10),
                    "Expected delay ~30.1s, got {actual_delay:?}"
                );
            }
            other => panic!("Expected AddTimer, got {other:?}"),
        }
    }

    #[test]
    fn notify_beyond_window_ignored() {
        // NOTIFY with visible_at beyond the refresh window should be ignored
        let now_ms = 1_700_000_000_000i64;
        let far_future_ms = now_ms + 90_000; // 90 seconds (beyond 60s window)
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &far_future_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        assert_eq!(action, NotifyAction::Ignore);
    }

    #[test]
    fn notify_invalid_payload_treated_as_immediate() {
        // Invalid payload should be treated as immediate (parsed as 0)
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action("garbage", now_ms, window_end_ms, grace, now_instant);

        assert_eq!(action, NotifyAction::WakeNow);
    }

    #[test]
    fn notify_empty_payload_treated_as_immediate() {
        // Empty payload should be treated as immediate (parsed as 0)
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action("", now_ms, window_end_ms, grace, now_instant);

        assert_eq!(action, NotifyAction::WakeNow);
    }

    // =======================================================================
    // Category 2: Timer Heap Management Tests
    // =======================================================================

    #[test]
    fn timer_fires_at_visible_at_plus_grace() {
        // Timer should fire at visible_at + grace_period
        let now_ms = 1_700_000_000_000i64;
        let visible_at_ms = now_ms + 10_000; // 10 seconds from now
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &visible_at_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        match action {
            NotifyAction::AddTimer { fire_at } => {
                let delay = fire_at.duration_since(now_instant);
                // Should be 10s + 100ms = 10.1s
                let expected = Duration::from_millis(10_100);
                assert!(
                    delay >= expected - Duration::from_millis(5)
                        && delay <= expected + Duration::from_millis(5),
                    "Timer should fire at visible_at + grace, got {delay:?}"
                );
            }
            other => panic!("Expected AddTimer, got {other:?}"),
        }
    }

    #[test]
    fn timer_heap_ordering() {
        // Min-heap should return timers in order (earliest first)
        let mut heap: BinaryHeap<Reverse<Instant>> = BinaryHeap::new();
        let now = Instant::now();

        let t1 = now + Duration::from_secs(10);
        let t2 = now + Duration::from_secs(5);
        let t3 = now + Duration::from_secs(15);

        heap.push(Reverse(t1));
        heap.push(Reverse(t2));
        heap.push(Reverse(t3));

        // Should pop in order: t2 (5s), t1 (10s), t3 (15s)
        assert_eq!(heap.pop().unwrap().0, t2);
        assert_eq!(heap.pop().unwrap().0, t1);
        assert_eq!(heap.pop().unwrap().0, t3);
    }

    #[test]
    fn expired_timers_popped_in_batch() {
        // Multiple expired timers should all be poppable
        let mut heap: BinaryHeap<Reverse<Instant>> = BinaryHeap::new();
        let past = Instant::now() - Duration::from_secs(1);

        // Add 3 timers all in the past
        heap.push(Reverse(past - Duration::from_millis(100)));
        heap.push(Reverse(past - Duration::from_millis(200)));
        heap.push(Reverse(past - Duration::from_millis(300)));

        let now = Instant::now();
        let mut fired = 0;

        while let Some(Reverse(fire_at)) = heap.peek() {
            if *fire_at <= now {
                heap.pop();
                fired += 1;
            } else {
                break;
            }
        }

        assert_eq!(fired, 3);
        assert!(heap.is_empty());
    }

    #[test]
    fn timer_does_not_fire_early() {
        // A timer in the future should not fire yet
        let mut heap: BinaryHeap<Reverse<Instant>> = BinaryHeap::new();
        let now = Instant::now();
        let future = now + Duration::from_secs(10);

        heap.push(Reverse(future));

        // Simulate checking at "now" - timer should not fire
        if let Some(Reverse(fire_at)) = heap.peek() {
            assert!(*fire_at > now, "Timer should not fire early");
        }
    }

    // =======================================================================
    // Category 3: Refresh Query Tests
    // =======================================================================

    #[test]
    fn refresh_adds_timers_to_heap() {
        // Refresh with future timers should add them to the heap
        let now_ms = 1_700_000_000_000i64;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let timers = vec![
            now_ms + 10_000, // 10s from now
            now_ms + 30_000, // 30s from now
        ];

        let result = timers_from_refresh(&timers, now_ms, grace, now_instant);

        assert_eq!(result.len(), 2);
        // First timer should fire at ~10.1s
        let delay1 = result[0].duration_since(now_instant);
        assert!(delay1 >= Duration::from_millis(10_000));
        assert!(delay1 <= Duration::from_millis(10_200));
    }

    #[test]
    fn refresh_skips_already_passed_timers() {
        // Timers in the past should not be added
        let now_ms = 1_700_000_000_000i64;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let timers = vec![
            now_ms - 5_000, // 5s ago - should be skipped
            now_ms + 100,   // 100ms from now - delay would be 200ms, should be added
        ];

        let result = timers_from_refresh(&timers, now_ms, grace, now_instant);

        // Only the future timer should be added
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn refresh_with_empty_result() {
        // Empty refresh result should produce no timers
        let now_ms = 1_700_000_000_000i64;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let timers: Vec<i64> = vec![];
        let result = timers_from_refresh(&timers, now_ms, grace, now_instant);

        assert!(result.is_empty());
    }

    #[test]
    fn refresh_timer_includes_grace_period() {
        // Timers from refresh should include grace period
        let now_ms = 1_700_000_000_000i64;
        let grace = Duration::from_millis(500); // Larger grace for clearer test
        let now_instant = Instant::now();

        let timers = vec![now_ms + 10_000]; // 10s from now

        let result = timers_from_refresh(&timers, now_ms, grace, now_instant);

        assert_eq!(result.len(), 1);
        let delay = result[0].duration_since(now_instant);
        // Should be 10s + 500ms = 10.5s
        assert!(delay >= Duration::from_millis(10_400));
        assert!(delay <= Duration::from_millis(10_600));
    }

    #[test]
    fn refresh_boundary_timer_at_exactly_now() {
        // Timer exactly at now should have delay = grace_period only
        let now_ms = 1_700_000_000_000i64;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let timers = vec![now_ms]; // Exactly now

        let result = timers_from_refresh(&timers, now_ms, grace, now_instant);

        // delay = (now_ms - now_ms) + grace = grace = 100ms
        // This is > 0, so it should be added
        assert_eq!(result.len(), 1);
        let delay = result[0].duration_since(now_instant);
        assert!(delay >= Duration::from_millis(90));
        assert!(delay <= Duration::from_millis(110));
    }

    // =======================================================================
    // Edge Case Tests
    // =======================================================================

    #[test]
    fn notify_at_window_boundary_included() {
        // Timer exactly at window end should be included
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &window_end_ms.to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        // At window_end should still be included (<=)
        match action {
            NotifyAction::AddTimer { .. } => {}
            other => panic!("Expected AddTimer at window boundary, got {other:?}"),
        }
    }

    #[test]
    fn notify_just_past_window_boundary_ignored() {
        // Timer just past window end should be ignored
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action(
            &(window_end_ms + 1).to_string(),
            now_ms,
            window_end_ms,
            grace,
            now_instant,
        );

        assert_eq!(action, NotifyAction::Ignore);
    }

    #[test]
    fn notify_negative_timestamp_treated_as_immediate() {
        // Negative timestamp (way in the past) should wake immediately
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action("-1000", now_ms, window_end_ms, grace, now_instant);

        assert_eq!(action, NotifyAction::WakeNow);
    }

    #[test]
    fn notify_zero_timestamp_treated_as_immediate() {
        // Zero timestamp (epoch) is way in the past, should wake immediately
        let now_ms = 1_700_000_000_000i64;
        let window_end_ms = now_ms + 60_000;
        let grace = Duration::from_millis(100);
        let now_instant = Instant::now();

        let action = parse_notify_action("0", now_ms, window_end_ms, grace, now_instant);

        assert_eq!(action, NotifyAction::WakeNow);
    }
}