celers-core 0.2.0

Core traits and types for CeleRS distributed task queue
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
//! Task Revocation
//!
//! This module provides enhanced task revocation capabilities:
//!
//! - **Revoke by ID**: Revoke a specific task by its ID
//! - **Revoke by Pattern**: Revoke tasks matching a name pattern (glob or regex)
//! - **Bulk Revocation**: Revoke multiple tasks at once
//! - **Persistent Revocation**: Revocations that survive worker restarts
//!
//! # Example
//!
//! ```rust
//! use celers_core::revocation::{RevocationManager, RevocationMode};
//! use uuid::Uuid;
//!
//! let mut manager = RevocationManager::new();
//! let task_id = Uuid::new_v4();
//!
//! // Revoke a single task
//! manager.revoke(task_id, RevocationMode::Terminate);
//!
//! // Revoke all tasks matching a pattern
//! manager.revoke_by_pattern("email.*", RevocationMode::Ignore);
//!
//! // Check if a task is revoked
//! assert!(manager.is_revoked(task_id));
//! ```

use crate::router::PatternMatcher;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use uuid::Uuid;

/// Revocation mode for how to handle a revoked task
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum RevocationMode {
    /// Terminate the task if already running
    #[default]
    Terminate,
    /// Ignore the task (don't execute but don't terminate if running)
    Ignore,
}

/// A request to revoke a task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevocationRequest {
    /// Task ID to revoke
    pub task_id: Uuid,
    /// Revocation mode
    pub mode: RevocationMode,
    /// When the revocation was issued (Unix timestamp)
    pub timestamp: f64,
    /// Optional expiration time (Unix timestamp)
    pub expires: Option<f64>,
    /// Reason for revocation
    pub reason: Option<String>,
    /// Signal to send (for terminate mode)
    pub signal: Option<String>,
}

impl RevocationRequest {
    /// Create a new revocation request
    #[must_use]
    pub fn new(task_id: Uuid, mode: RevocationMode) -> Self {
        Self {
            task_id,
            mode,
            timestamp: current_timestamp(),
            expires: None,
            reason: None,
            signal: None,
        }
    }

    /// Set expiration time
    #[must_use]
    pub fn with_expiration(mut self, expires_in: Duration) -> Self {
        self.expires = Some(current_timestamp() + expires_in.as_secs_f64());
        self
    }

    /// Set reason for revocation
    #[must_use]
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }

    /// Set signal to send (for terminate mode)
    #[must_use]
    pub fn with_signal(mut self, signal: impl Into<String>) -> Self {
        self.signal = Some(signal.into());
        self
    }

    /// Check if this revocation has expired
    #[inline]
    #[must_use]
    pub fn is_expired(&self) -> bool {
        if let Some(expires) = self.expires {
            current_timestamp() > expires
        } else {
            false
        }
    }
}

/// A pattern-based revocation rule
#[derive(Debug, Clone)]
pub struct PatternRevocation {
    /// Pattern matcher for task names
    pub pattern: PatternMatcher,
    /// Revocation mode
    pub mode: RevocationMode,
    /// When the revocation was issued
    pub timestamp: f64,
    /// Optional expiration time
    pub expires: Option<f64>,
    /// Reason for revocation
    pub reason: Option<String>,
}

impl PatternRevocation {
    /// Create a new pattern revocation
    #[must_use]
    pub fn new(pattern: PatternMatcher, mode: RevocationMode) -> Self {
        Self {
            pattern,
            mode,
            timestamp: current_timestamp(),
            expires: None,
            reason: None,
        }
    }

    /// Set expiration time
    #[must_use]
    pub fn with_expiration(mut self, expires_in: Duration) -> Self {
        self.expires = Some(current_timestamp() + expires_in.as_secs_f64());
        self
    }

    /// Set reason for revocation
    #[must_use]
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }

    /// Check if this revocation has expired
    #[inline]
    #[must_use]
    pub fn is_expired(&self) -> bool {
        if let Some(expires) = self.expires {
            current_timestamp() > expires
        } else {
            false
        }
    }

    /// Check if a task name matches this pattern
    #[inline]
    #[must_use]
    pub fn matches(&self, task_name: &str) -> bool {
        self.pattern.matches(task_name)
    }
}

/// Result of a revocation check
#[derive(Debug, Clone)]
pub struct RevocationResult {
    /// Whether the task is revoked
    pub revoked: bool,
    /// Revocation mode
    pub mode: RevocationMode,
    /// Reason for revocation
    pub reason: Option<String>,
    /// Signal to send (for terminate mode)
    pub signal: Option<String>,
}

impl RevocationResult {
    /// Create a result indicating task is not revoked
    #[must_use]
    pub fn not_revoked() -> Self {
        Self {
            revoked: false,
            mode: RevocationMode::Ignore,
            reason: None,
            signal: None,
        }
    }

    /// Create a result indicating task is revoked
    #[must_use]
    pub fn revoked(mode: RevocationMode, reason: Option<String>, signal: Option<String>) -> Self {
        Self {
            revoked: true,
            mode,
            reason,
            signal,
        }
    }
}

/// Serializable revocation state for persistence
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RevocationState {
    /// Revoked task IDs (`task_id` -> request)
    pub revoked_tasks: HashMap<String, RevocationRequest>,
    /// Pattern-based revocations (serializable form)
    pub pattern_revocations: Vec<SerializablePatternRevocation>,
}

/// Serializable form of `PatternRevocation`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializablePatternRevocation {
    /// Pattern string (glob format)
    pub pattern: String,
    /// Revocation mode
    pub mode: RevocationMode,
    /// When the revocation was issued
    pub timestamp: f64,
    /// Optional expiration time
    pub expires: Option<f64>,
    /// Reason for revocation
    pub reason: Option<String>,
}

impl From<&PatternRevocation> for SerializablePatternRevocation {
    fn from(rev: &PatternRevocation) -> Self {
        // Extract pattern string (simplified - assumes glob pattern)
        let pattern = match &rev.pattern {
            PatternMatcher::Exact(s) => s.clone(),
            PatternMatcher::Glob(g) => g.pattern().to_string(),
            PatternMatcher::Regex(r) => r.pattern().to_string(),
            PatternMatcher::All => "*".to_string(),
        };
        Self {
            pattern,
            mode: rev.mode,
            timestamp: rev.timestamp,
            expires: rev.expires,
            reason: rev.reason.clone(),
        }
    }
}

impl SerializablePatternRevocation {
    /// Convert to `PatternRevocation`
    #[must_use]
    pub fn into_pattern_revocation(self) -> PatternRevocation {
        PatternRevocation {
            pattern: PatternMatcher::glob(&self.pattern),
            mode: self.mode,
            timestamp: self.timestamp,
            expires: self.expires,
            reason: self.reason,
        }
    }
}

/// Revocation manager for tracking revoked tasks
#[derive(Debug, Default)]
pub struct RevocationManager {
    /// Revoked task IDs
    revoked_ids: HashMap<Uuid, RevocationRequest>,
    /// Pattern-based revocations
    pattern_revocations: Vec<PatternRevocation>,
    /// Set of currently terminated task IDs
    terminated: HashSet<Uuid>,
}

impl RevocationManager {
    /// Create a new revocation manager
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Revoke a task by ID
    pub fn revoke(&mut self, task_id: Uuid, mode: RevocationMode) {
        let request = RevocationRequest::new(task_id, mode);
        self.revoked_ids.insert(task_id, request);
    }

    /// Revoke a task with a full request
    pub fn revoke_with_request(&mut self, request: RevocationRequest) {
        self.revoked_ids.insert(request.task_id, request);
    }

    /// Revoke all tasks matching a pattern
    pub fn revoke_by_pattern(&mut self, pattern: &str, mode: RevocationMode) {
        let pattern_rev = PatternRevocation::new(PatternMatcher::glob(pattern), mode);
        self.pattern_revocations.push(pattern_rev);
    }

    /// Revoke by pattern with full configuration
    pub fn revoke_with_pattern(&mut self, revocation: PatternRevocation) {
        self.pattern_revocations.push(revocation);
    }

    /// Bulk revoke multiple tasks
    pub fn bulk_revoke(&mut self, task_ids: &[Uuid], mode: RevocationMode) {
        for &task_id in task_ids {
            self.revoke(task_id, mode);
        }
    }

    /// Check if a task is revoked (by ID)
    #[inline]
    #[must_use]
    pub fn is_revoked(&self, task_id: Uuid) -> bool {
        if let Some(request) = self.revoked_ids.get(&task_id) {
            !request.is_expired()
        } else {
            false
        }
    }

    /// Check if a task should be revoked (by ID or pattern)
    #[must_use]
    pub fn check_revocation(&self, task_id: Uuid, task_name: &str) -> RevocationResult {
        // Check by ID first
        if let Some(request) = self.revoked_ids.get(&task_id) {
            if !request.is_expired() {
                return RevocationResult::revoked(
                    request.mode,
                    request.reason.clone(),
                    request.signal.clone(),
                );
            }
        }

        // Check by pattern
        for pattern_rev in &self.pattern_revocations {
            if !pattern_rev.is_expired() && pattern_rev.matches(task_name) {
                return RevocationResult::revoked(
                    pattern_rev.mode,
                    pattern_rev.reason.clone(),
                    None,
                );
            }
        }

        RevocationResult::not_revoked()
    }

    /// Mark a task as terminated
    pub fn mark_terminated(&mut self, task_id: Uuid) {
        self.terminated.insert(task_id);
    }

    /// Check if a task has been terminated
    #[inline]
    #[must_use]
    pub fn is_terminated(&self, task_id: Uuid) -> bool {
        self.terminated.contains(&task_id)
    }

    /// Remove revocation for a task ID
    pub fn unrevoke(&mut self, task_id: Uuid) {
        self.revoked_ids.remove(&task_id);
    }

    /// Remove pattern-based revocations matching a pattern string
    pub fn remove_pattern(&mut self, pattern: &str) {
        self.pattern_revocations.retain(|p| {
            if let PatternMatcher::Glob(g) = &p.pattern {
                g.pattern() != pattern
            } else {
                true
            }
        });
    }

    /// Clean up expired revocations
    pub fn cleanup_expired(&mut self) {
        self.revoked_ids.retain(|_, request| !request.is_expired());
        self.pattern_revocations.retain(|rev| !rev.is_expired());
    }

    /// Get all revoked task IDs
    #[must_use]
    pub fn revoked_ids(&self) -> Vec<Uuid> {
        self.revoked_ids
            .iter()
            .filter(|(_, request)| !request.is_expired())
            .map(|(id, _)| *id)
            .collect()
    }

    /// Get count of revoked tasks
    #[inline]
    #[must_use]
    pub fn revoked_count(&self) -> usize {
        self.revoked_ids
            .values()
            .filter(|request| !request.is_expired())
            .count()
    }

    /// Clear all revocations
    pub fn clear(&mut self) {
        self.revoked_ids.clear();
        self.pattern_revocations.clear();
        self.terminated.clear();
    }

    /// Export state for persistence
    pub fn export_state(&self) -> RevocationState {
        let revoked_tasks = self
            .revoked_ids
            .iter()
            .filter(|(_, req)| !req.is_expired())
            .map(|(id, req)| (id.to_string(), req.clone()))
            .collect();

        let pattern_revocations = self
            .pattern_revocations
            .iter()
            .filter(|rev| !rev.is_expired())
            .map(SerializablePatternRevocation::from)
            .collect();

        RevocationState {
            revoked_tasks,
            pattern_revocations,
        }
    }

    /// Import state from persistence
    pub fn import_state(&mut self, state: RevocationState) {
        for (id_str, request) in state.revoked_tasks {
            if !request.is_expired() {
                if let Ok(id) = Uuid::parse_str(&id_str) {
                    self.revoked_ids.insert(id, request);
                }
            }
        }

        for ser_pattern in state.pattern_revocations {
            let pattern_rev = ser_pattern.into_pattern_revocation();
            if !pattern_rev.is_expired() {
                self.pattern_revocations.push(pattern_rev);
            }
        }
    }
}

/// Thread-safe revocation manager for workers
#[derive(Debug, Clone, Default)]
pub struct WorkerRevocationManager {
    inner: Arc<RwLock<RevocationManager>>,
}

impl WorkerRevocationManager {
    /// Create a new worker revocation manager
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Revoke a task by ID
    pub fn revoke(&self, task_id: Uuid, mode: RevocationMode) {
        if let Ok(mut guard) = self.inner.write() {
            guard.revoke(task_id, mode);
        }
    }

    /// Revoke a task with a full request
    pub fn revoke_with_request(&self, request: RevocationRequest) {
        if let Ok(mut guard) = self.inner.write() {
            guard.revoke_with_request(request);
        }
    }

    /// Revoke by pattern
    pub fn revoke_by_pattern(&self, pattern: &str, mode: RevocationMode) {
        if let Ok(mut guard) = self.inner.write() {
            guard.revoke_by_pattern(pattern, mode);
        }
    }

    /// Bulk revoke multiple tasks
    pub fn bulk_revoke(&self, task_ids: &[Uuid], mode: RevocationMode) {
        if let Ok(mut guard) = self.inner.write() {
            guard.bulk_revoke(task_ids, mode);
        }
    }

    /// Check if a task is revoked by ID
    #[must_use]
    pub fn is_revoked(&self, task_id: Uuid) -> bool {
        if let Ok(guard) = self.inner.read() {
            guard.is_revoked(task_id)
        } else {
            false
        }
    }

    /// Check revocation status (by ID and pattern)
    #[must_use]
    pub fn check_revocation(&self, task_id: Uuid, task_name: &str) -> RevocationResult {
        if let Ok(guard) = self.inner.read() {
            guard.check_revocation(task_id, task_name)
        } else {
            RevocationResult::not_revoked()
        }
    }

    /// Mark a task as terminated
    pub fn mark_terminated(&self, task_id: Uuid) {
        if let Ok(mut guard) = self.inner.write() {
            guard.mark_terminated(task_id);
        }
    }

    /// Check if a task has been terminated
    #[must_use]
    pub fn is_terminated(&self, task_id: Uuid) -> bool {
        if let Ok(guard) = self.inner.read() {
            guard.is_terminated(task_id)
        } else {
            false
        }
    }

    /// Remove revocation for a task ID
    pub fn unrevoke(&self, task_id: Uuid) {
        if let Ok(mut guard) = self.inner.write() {
            guard.unrevoke(task_id);
        }
    }

    /// Clean up expired revocations
    pub fn cleanup_expired(&self) {
        if let Ok(mut guard) = self.inner.write() {
            guard.cleanup_expired();
        }
    }

    /// Get all revoked task IDs
    #[must_use]
    pub fn revoked_ids(&self) -> Vec<Uuid> {
        if let Ok(guard) = self.inner.read() {
            guard.revoked_ids()
        } else {
            Vec::new()
        }
    }

    /// Get count of revoked tasks
    #[must_use]
    pub fn revoked_count(&self) -> usize {
        if let Ok(guard) = self.inner.read() {
            guard.revoked_count()
        } else {
            0
        }
    }

    /// Export state for persistence
    #[must_use]
    pub fn export_state(&self) -> RevocationState {
        if let Ok(guard) = self.inner.read() {
            guard.export_state()
        } else {
            RevocationState::default()
        }
    }

    /// Import state from persistence
    pub fn import_state(&self, state: RevocationState) {
        if let Ok(mut guard) = self.inner.write() {
            guard.import_state(state);
        }
    }

    /// Clear all revocations
    pub fn clear(&self) {
        if let Ok(mut guard) = self.inner.write() {
            guard.clear();
        }
    }
}

/// Get current timestamp as f64
fn current_timestamp() -> f64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs_f64()
}

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

    #[test]
    fn test_revocation_request() {
        let task_id = Uuid::new_v4();
        let request = RevocationRequest::new(task_id, RevocationMode::Terminate);

        assert_eq!(request.task_id, task_id);
        assert_eq!(request.mode, RevocationMode::Terminate);
        assert!(!request.is_expired());
    }

    #[test]
    fn test_revocation_request_with_expiration() {
        let task_id = Uuid::new_v4();
        let request = RevocationRequest::new(task_id, RevocationMode::Terminate)
            .with_expiration(Duration::from_secs(0));

        // Immediately expired
        std::thread::sleep(Duration::from_millis(10));
        assert!(request.is_expired());
    }

    #[test]
    fn test_revocation_manager_basic() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        manager.revoke(task_id, RevocationMode::Terminate);
        assert!(manager.is_revoked(task_id));

        let other_id = Uuid::new_v4();
        assert!(!manager.is_revoked(other_id));
    }

    #[test]
    fn test_revocation_by_pattern() {
        let mut manager = RevocationManager::new();

        manager.revoke_by_pattern("email.*", RevocationMode::Ignore);

        let result = manager.check_revocation(Uuid::new_v4(), "email.send");
        assert!(result.revoked);
        assert_eq!(result.mode, RevocationMode::Ignore);

        let result = manager.check_revocation(Uuid::new_v4(), "sms.send");
        assert!(!result.revoked);
    }

    #[test]
    fn test_bulk_revoke() {
        let mut manager = RevocationManager::new();
        let ids: Vec<Uuid> = (0..5).map(|_| Uuid::new_v4()).collect();

        manager.bulk_revoke(&ids, RevocationMode::Terminate);

        for id in &ids {
            assert!(manager.is_revoked(*id));
        }
    }

    #[test]
    fn test_unrevoke() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        manager.revoke(task_id, RevocationMode::Terminate);
        assert!(manager.is_revoked(task_id));

        manager.unrevoke(task_id);
        assert!(!manager.is_revoked(task_id));
    }

    #[test]
    fn test_cleanup_expired() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        // Add an expired revocation
        let request = RevocationRequest::new(task_id, RevocationMode::Terminate)
            .with_expiration(Duration::from_secs(0));
        std::thread::sleep(Duration::from_millis(10));
        manager.revoke_with_request(request);

        // Add a non-expired revocation
        let other_id = Uuid::new_v4();
        manager.revoke(other_id, RevocationMode::Terminate);

        manager.cleanup_expired();

        assert!(!manager.is_revoked(task_id)); // Expired
        assert!(manager.is_revoked(other_id)); // Not expired
    }

    #[test]
    fn test_export_import_state() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        manager.revoke(task_id, RevocationMode::Terminate);
        manager.revoke_by_pattern("email.*", RevocationMode::Ignore);

        let state = manager.export_state();

        let mut new_manager = RevocationManager::new();
        new_manager.import_state(state);

        assert!(new_manager.is_revoked(task_id));
        let result = new_manager.check_revocation(Uuid::new_v4(), "email.send");
        assert!(result.revoked);
    }

    #[test]
    fn test_worker_revocation_manager() {
        let manager = WorkerRevocationManager::new();
        let task_id = Uuid::new_v4();

        manager.revoke(task_id, RevocationMode::Terminate);
        assert!(manager.is_revoked(task_id));

        manager.mark_terminated(task_id);
        assert!(manager.is_terminated(task_id));
    }

    #[test]
    fn test_revocation_state_serialization() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        manager.revoke(task_id, RevocationMode::Terminate);
        manager.revoke_by_pattern("tasks.*", RevocationMode::Ignore);

        let state = manager.export_state();
        let json = serde_json::to_string(&state).unwrap();
        let parsed: RevocationState = serde_json::from_str(&json).unwrap();

        assert!(!parsed.revoked_tasks.is_empty());
        assert!(!parsed.pattern_revocations.is_empty());
    }

    #[test]
    fn test_revocation_with_reason() {
        let mut manager = RevocationManager::new();
        let task_id = Uuid::new_v4();

        let request = RevocationRequest::new(task_id, RevocationMode::Terminate)
            .with_reason("Manual cancellation by user");
        manager.revoke_with_request(request);

        let result = manager.check_revocation(task_id, "any.task");
        assert!(result.revoked);
        assert_eq!(
            result.reason,
            Some("Manual cancellation by user".to_string())
        );
    }

    #[test]
    fn test_revoked_count() {
        let mut manager = RevocationManager::new();

        for _ in 0..5 {
            manager.revoke(Uuid::new_v4(), RevocationMode::Terminate);
        }

        assert_eq!(manager.revoked_count(), 5);
        assert_eq!(manager.revoked_ids().len(), 5);
    }

    #[test]
    fn test_clear() {
        let mut manager = RevocationManager::new();

        manager.revoke(Uuid::new_v4(), RevocationMode::Terminate);
        manager.revoke_by_pattern("*", RevocationMode::Ignore);
        manager.mark_terminated(Uuid::new_v4());

        manager.clear();

        assert_eq!(manager.revoked_count(), 0);
    }
}