rust-task-queue 0.1.5

Production-ready Redis task queue with intelligent auto-scaling, Actix Web integration, and enterprise-grade observability for high-performance async Rust applications.
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
//! Configuration management for the task queue framework
//!
//! This module provides a comprehensive configuration system that supports:
//! - Environment variables
//! - TOML and YAML configuration files
//! - Global configuration initialization
//! - Integration with Actix Web

use crate::autoscaler::AutoScalerConfig;
use crate::TaskQueueError;
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;

/// Global configuration instance
static GLOBAL_CONFIG: OnceLock<TaskQueueConfig> = OnceLock::new();

/// Main configuration structure for the task queue system
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TaskQueueConfig {
    /// Redis connection configuration
    pub redis: RedisConfig,

    /// Worker configuration
    pub workers: WorkerConfig,

    /// Auto-scaling configuration
    pub autoscaler: AutoScalerConfig,

    /// Auto-registration settings
    pub auto_register: AutoRegisterConfig,

    /// Scheduler configuration
    pub scheduler: SchedulerConfig,

    /// Actix Web integration settings
    #[cfg(feature = "actix-integration")]
    pub actix: ActixConfig,

    /// Axum integration settings
    #[cfg(feature = "axum-integration")]
    pub axum: AxumConfig,
}

/// Redis connection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisConfig {
    /// Redis connection URL
    pub url: String,

    /// Connection pool size
    pub pool_size: Option<u32>,

    /// Connection timeout in seconds
    pub connection_timeout: Option<u64>,

    /// Command timeout in seconds
    pub command_timeout: Option<u64>,
}

/// Worker configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerConfig {
    /// Initial number of workers to start
    pub initial_count: usize,

    /// Maximum number of concurrent tasks per worker
    pub max_concurrent_tasks: Option<usize>,

    /// Worker heartbeat interval in seconds
    pub heartbeat_interval: Option<u64>,

    /// Grace period for shutdown in seconds
    pub shutdown_grace_period: Option<u64>,
}

/// Auto-registration configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoRegisterConfig {
    /// Enable automatic task registration
    pub enabled: bool,
}

/// Scheduler configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchedulerConfig {
    /// Enable the scheduler
    pub enabled: bool,

    /// Scheduler tick interval in seconds
    pub tick_interval: Option<u64>,

    /// Maximum number of scheduled tasks to process per tick
    pub max_tasks_per_tick: Option<usize>,
}

/// Actix Web integration configuration
#[cfg(feature = "actix-integration")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActixConfig {
    /// Enable automatic route configuration
    pub auto_configure_routes: bool,

    /// Base path for task queue routes
    pub route_prefix: String,

    /// Enable metrics endpoint
    pub enable_metrics: bool,

    /// Enable health check endpoint
    pub enable_health_check: bool,
}

/// Axum integration configuration
#[cfg(feature = "axum-integration")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AxumConfig {
    /// Enable automatic route configuration
    pub auto_configure_routes: bool,

    /// Base path for task queue routes
    pub route_prefix: String,

    /// Enable metrics endpoint
    pub enable_metrics: bool,

    /// Enable health check endpoint
    pub enable_health_check: bool
}

impl Default for RedisConfig {
    fn default() -> Self {
        Self {
            url: std::env::var("REDIS_URL")
                .unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string()),
            pool_size: None,
            connection_timeout: None,
            command_timeout: None,
        }
    }
}

impl Default for WorkerConfig {
    fn default() -> Self {
        Self {
            initial_count: std::env::var("INITIAL_WORKER_COUNT")
                .ok()
                .and_then(|s| s.parse().ok())
                .unwrap_or(2),
            max_concurrent_tasks: None,
            heartbeat_interval: None,
            shutdown_grace_period: None,
        }
    }
}

impl Default for AutoRegisterConfig {
    fn default() -> Self {
        Self {
            enabled: std::env::var("AUTO_REGISTER_TASKS")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(false),
        }
    }
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            enabled: std::env::var("ENABLE_SCHEDULER")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(false),
            tick_interval: None,
            max_tasks_per_tick: None,
        }
    }
}

#[cfg(feature = "actix-integration")]
impl Default for ActixConfig {
    fn default() -> Self {
        Self {
            auto_configure_routes: std::env::var("AUTO_CONFIGURE_ROUTES")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
            route_prefix: std::env::var("ROUTE_PREFIX")
                .unwrap_or_else(|_| "/api/v1/tasks".to_string()),
            enable_metrics: std::env::var("ENABLE_METRICS")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
            enable_health_check: std::env::var("ENABLE_HEALTH_CHECK")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
        }
    }
}

#[cfg(feature = "axum-integration")]
impl Default for AxumConfig {
    fn default() -> Self {
        Self {
            auto_configure_routes: std::env::var("AXUM_AUTO_CONFIGURE_ROUTES")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
            route_prefix: std::env::var("AXUM_ROUTE_PREFIX")
                .unwrap_or_else(|_| "/api/v1/tasks".to_string()),
            enable_metrics: std::env::var("AXUM_ENABLE_METRICS")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
            enable_health_check: std::env::var("AXUM_ENABLE_HEALTH_CHECK")
                .map(|s| s.to_lowercase() == "true")
                .unwrap_or(true),
        }
    }
}

impl TaskQueueConfig {
    /// Validate the entire configuration
    pub fn validate(&self) -> Result<(), TaskQueueError> {
        // Validate Redis URL
        if self.redis.url.is_empty() {
            return Err(TaskQueueError::Configuration(
                "Redis URL cannot be empty".to_string(),
            ));
        }

        // Basic URL format validation
        if !self.redis.url.starts_with("redis://") && !self.redis.url.starts_with("rediss://") {
            return Err(TaskQueueError::Configuration(
                "Redis URL must start with redis:// or rediss://".to_string(),
            ));
        }

        // Validate worker configuration
        if self.workers.initial_count == 0 {
            return Err(TaskQueueError::Configuration(
                "Initial worker count must be greater than 0".to_string(),
            ));
        }

        if self.workers.initial_count > 1000 {
            return Err(TaskQueueError::Configuration(
                "Initial worker count cannot exceed 1000".to_string(),
            ));
        }

        if let Some(max_concurrent) = self.workers.max_concurrent_tasks {
            if max_concurrent == 0 || max_concurrent > 1000 {
                return Err(TaskQueueError::Configuration(
                    "Max concurrent tasks per worker must be between 1 and 1000".to_string(),
                ));
            }
        }

        if let Some(heartbeat) = self.workers.heartbeat_interval {
            if heartbeat == 0 || heartbeat > 3600 {
                return Err(TaskQueueError::Configuration(
                    "Heartbeat interval must be between 1 and 3600 seconds".to_string(),
                ));
            }
        }

        if let Some(grace_period) = self.workers.shutdown_grace_period {
            if grace_period > 300 {
                return Err(TaskQueueError::Configuration(
                    "Shutdown grace period cannot exceed 300 seconds".to_string(),
                ));
            }
        }

        // Validate pool size
        if let Some(pool_size) = self.redis.pool_size {
            if pool_size == 0 || pool_size > 1000 {
                return Err(TaskQueueError::Configuration(
                    "Redis pool size must be between 1 and 1000".to_string(),
                ));
            }
        }

        // Validate timeouts
        if let Some(timeout) = self.redis.connection_timeout {
            if timeout == 0 || timeout > 300 {
                return Err(TaskQueueError::Configuration(
                    "Connection timeout must be between 1 and 300 seconds".to_string(),
                ));
            }
        }

        if let Some(timeout) = self.redis.command_timeout {
            if timeout == 0 || timeout > 300 {
                return Err(TaskQueueError::Configuration(
                    "Command timeout must be between 1 and 300 seconds".to_string(),
                ));
            }
        }

        // Validate scheduler configuration
        if let Some(tick_interval) = self.scheduler.tick_interval {
            if tick_interval == 0 || tick_interval > 3600 {
                return Err(TaskQueueError::Configuration(
                    "Scheduler tick interval must be between 1 and 3600 seconds".to_string(),
                ));
            }
        }

        if let Some(max_tasks) = self.scheduler.max_tasks_per_tick {
            if max_tasks == 0 || max_tasks > 10000 {
                return Err(TaskQueueError::Configuration(
                    "Max tasks per tick must be between 1 and 10000".to_string(),
                ));
            }
        }

        // Validate autoscaler configuration
        self.autoscaler.validate()?;

        // Validate Actix configuration
        #[cfg(feature = "actix-integration")]
        {
            if self.actix.route_prefix.is_empty() {
                return Err(TaskQueueError::Configuration(
                    "Actix route prefix cannot be empty".to_string(),
                ));
            }

            if !self.actix.route_prefix.starts_with('/') {
                return Err(TaskQueueError::Configuration(
                    "Actix route prefix must start with '/'".to_string(),
                ));
            }
        }

        // Validate Axum configuration
        #[cfg(feature = "axum-integration")]
        {
            if self.axum.route_prefix.is_empty() {
                return Err(TaskQueueError::Configuration(
                    "Axum route prefix cannot be empty".to_string(),
                ));
            }

            if !self.axum.route_prefix.starts_with('/') {
                return Err(TaskQueueError::Configuration(
                    "Axum route prefix must start with '/'".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Load configuration from environment variables only
    pub fn from_env() -> Result<Self, TaskQueueError> {
        let mut config = Self::default();

        // Load additional environment variables that aren't in defaults
        if let Ok(pool_size) = std::env::var("REDIS_POOL_SIZE") {
            config.redis.pool_size = Some(pool_size.parse().map_err(|_| {
                TaskQueueError::Configuration("Invalid REDIS_POOL_SIZE".to_string())
            })?);
        }

        if let Ok(timeout) = std::env::var("REDIS_CONNECTION_TIMEOUT") {
            config.redis.connection_timeout = Some(timeout.parse().map_err(|_| {
                TaskQueueError::Configuration("Invalid REDIS_CONNECTION_TIMEOUT".to_string())
            })?);
        }

        // Validate the configuration
        config.validate()?;

        Ok(config)
    }

    /// Load configuration from a file (TOML or YAML based on extension)
    #[cfg(feature = "config")]
    pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self, TaskQueueError> {
        let path = path.as_ref();
        let contents = std::fs::read_to_string(path).map_err(|e| {
            TaskQueueError::Configuration(format!("Failed to read config file: {}", e))
        })?;

        let config: TaskQueueConfig = if path.extension().and_then(|s| s.to_str()) == Some("toml") {
            toml::from_str(&contents).map_err(|e| {
                TaskQueueError::Configuration(format!("Failed to parse TOML config: {}", e))
            })?
        } else {
            serde_yaml::from_str(&contents).map_err(|e| {
                TaskQueueError::Configuration(format!("Failed to parse YAML config: {}", e))
            })?
        };

        // Validate the configuration
        config.validate()?;

        Ok(config)
    }

    /// Load configuration with automatic source detection and validation
    #[cfg(feature = "config")]
    pub fn load() -> Result<Self, TaskQueueError> {
        use config::{Config, Environment, File};

        let mut builder = Config::builder()
            // Start with defaults
            .add_source(Config::try_from(&Self::default()).map_err(|e| {
                TaskQueueError::Configuration(format!("Failed to create default config: {}", e))
            })?);

        // Check for config file in common locations
        for config_path in &[
            "task-queue.toml",
            "task-queue.yaml",
            "task-queue.yml",
            "config/task-queue.toml",
            "config/task-queue.yaml",
            "config/task-queue.yml",
        ] {
            if std::path::Path::new(config_path).exists() {
                builder = builder.add_source(File::with_name(config_path));
                break;
            }
        }

        // Override with environment variables (prefixed with TASK_QUEUE_)
        builder = builder.add_source(
            Environment::with_prefix("TASK_QUEUE")
                .separator("_")
                .try_parsing(true),
        );

        // Also support unprefixed common variables like REDIS_URL
        if let Ok(redis_url) = std::env::var("REDIS_URL") {
            builder = builder.set_override("redis.url", redis_url).map_err(|e| {
                TaskQueueError::Configuration(format!("Failed to set REDIS_URL override: {}", e))
            })?;
        }

        let config = builder
            .build()
            .map_err(|e| TaskQueueError::Configuration(format!("Failed to build config: {}", e)))?;

        let config: TaskQueueConfig = config.try_deserialize().map_err(|e| {
            TaskQueueError::Configuration(format!("Failed to deserialize config: {}", e))
        })?;

        // Validate the configuration
        config.validate()?;

        Ok(config)
    }

    /// Load configuration without the config crate (fallback)
    #[cfg(not(feature = "config"))]
    pub fn load() -> Result<Self, TaskQueueError> {
        Self::from_env()
    }

    /// Initialize global configuration if not already done
    pub fn init_global() -> Result<&'static Self, TaskQueueError> {
        match GLOBAL_CONFIG.get() {
            Some(config) => Ok(config),
            None => match GLOBAL_CONFIG.set(Self::load()?) {
                Ok(()) => Ok(GLOBAL_CONFIG.get().unwrap()),
                Err(_) => Ok(GLOBAL_CONFIG.get().unwrap()),
            },
        }
    }

    /// Get global configuration (if initialized)
    pub fn global() -> Option<&'static Self> {
        GLOBAL_CONFIG.get()
    }

    /// Get global configuration or initialize it
    pub fn get_or_init() -> Result<&'static Self, TaskQueueError> {
        match GLOBAL_CONFIG.get() {
            Some(config) => Ok(config),
            None => Self::init_global(),
        }
    }
}

/// Configuration builder for fluent API
#[derive(Default)]
pub struct ConfigBuilder {
    config: TaskQueueConfig,
}

impl ConfigBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn redis_url(mut self, url: impl Into<String>) -> Self {
        self.config.redis.url = url.into();
        self
    }

    pub fn workers(mut self, count: usize) -> Self {
        self.config.workers.initial_count = count;
        self
    }

    pub fn enable_auto_register(mut self, enabled: bool) -> Self {
        self.config.auto_register.enabled = enabled;
        self
    }

    pub fn enable_scheduler(mut self, enabled: bool) -> Self {
        self.config.scheduler.enabled = enabled;
        self
    }

    pub fn autoscaler_config(mut self, config: AutoScalerConfig) -> Self {
        self.config.autoscaler = config;
        self
    }

    pub fn build(self) -> TaskQueueConfig {
        self.config
    }
}

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

    #[test]
    fn test_redis_config_default() {
        let config = RedisConfig::default();

        // Should use environment variable or default
        assert!(!config.url.is_empty());
        assert!(config.url.starts_with("redis://"));
        assert!(config.pool_size.is_none());
        assert!(config.connection_timeout.is_none());
        assert!(config.command_timeout.is_none());
    }

    #[test]
    fn test_worker_config_default() {
        let config = WorkerConfig::default();

        assert!(config.initial_count > 0);
        assert!(config.max_concurrent_tasks.is_none());
        assert!(config.heartbeat_interval.is_none());
        assert!(config.shutdown_grace_period.is_none());
    }

    #[test]
    fn test_auto_register_config_default() {
        let config = AutoRegisterConfig::default();
        // Default should be false unless environment variable is set
        assert!(!config.enabled || env::var("AUTO_REGISTER_TASKS").is_ok());
    }

    #[test]
    fn test_scheduler_config_default() {
        let config = SchedulerConfig::default();
        // Default should be false unless environment variable is set
        assert!(!config.enabled || env::var("ENABLE_SCHEDULER").is_ok());
        assert!(config.tick_interval.is_none());
        assert!(config.max_tasks_per_tick.is_none());
    }

    #[cfg(feature = "actix-integration")]
    #[test]
    fn test_actix_config_default() {
        let config = ActixConfig::default();

        assert!(!config.route_prefix.is_empty());
        assert!(config.route_prefix.starts_with('/'));
        // Other defaults may vary based on environment variables
    }

    #[test]
    fn test_task_queue_config_default() {
        let config = TaskQueueConfig::default();

        assert!(!config.redis.url.is_empty());
        assert!(config.workers.initial_count > 0);
        // Other fields should have sensible defaults
    }

    #[test]
    fn test_config_validation_valid() {
        let config = TaskQueueConfig {
            redis: RedisConfig {
                url: "redis://localhost:6379".to_string(),
                pool_size: Some(10),
                connection_timeout: Some(30),
                command_timeout: Some(60),
            },
            workers: WorkerConfig {
                initial_count: 4,
                max_concurrent_tasks: Some(10),
                heartbeat_interval: Some(30),
                shutdown_grace_period: Some(60),
            },
            autoscaler: AutoScalerConfig::default(),
            auto_register: AutoRegisterConfig { enabled: true },
            scheduler: SchedulerConfig {
                enabled: true,
                tick_interval: Some(60),
                max_tasks_per_tick: Some(100),
            },
            #[cfg(feature = "actix-integration")]
            actix: ActixConfig {
                auto_configure_routes: true,
                route_prefix: "/api/tasks".to_string(),
                enable_metrics: true,
                enable_health_check: true,
            },
            #[cfg(feature = "axum-integration")]
            axum: AxumConfig {
                auto_configure_routes: true,
                route_prefix: "/api/tasks".to_string(),
                enable_metrics: true,
                enable_health_check: true
            },
        };

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validation_empty_redis_url() {
        let mut config = TaskQueueConfig::default();
        config.redis.url = "".to_string();

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Redis URL cannot be empty"));
    }

    #[test]
    fn test_config_validation_invalid_redis_url() {
        let mut config = TaskQueueConfig::default();
        config.redis.url = "http://localhost:6379".to_string();

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Redis URL must start with redis://"));
    }

    #[test]
    fn test_config_validation_zero_workers() {
        let mut config = TaskQueueConfig::default();
        config.workers.initial_count = 0;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Initial worker count must be greater than 0"));
    }

    #[test]
    fn test_config_validation_too_many_workers() {
        let mut config = TaskQueueConfig::default();
        config.workers.initial_count = 1001;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Initial worker count cannot exceed 1000"));
    }

    #[test]
    fn test_config_validation_invalid_pool_size() {
        let mut config = TaskQueueConfig::default();
        config.redis.pool_size = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Redis pool size must be between 1 and 1000"));
    }

    #[test]
    fn test_config_validation_invalid_timeouts() {
        let mut config = TaskQueueConfig::default();
        config.redis.connection_timeout = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Connection timeout must be between 1 and 300"));

        config.redis.connection_timeout = Some(30);
        config.redis.command_timeout = Some(301);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Command timeout must be between 1 and 300"));
    }

    #[test]
    fn test_config_validation_invalid_worker_settings() {
        let mut config = TaskQueueConfig::default();
        config.workers.max_concurrent_tasks = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Max concurrent tasks per worker must be between 1 and 1000"));

        config.workers.max_concurrent_tasks = Some(10);
        config.workers.heartbeat_interval = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Heartbeat interval must be between 1 and 3600"));

        config.workers.heartbeat_interval = Some(30);
        config.workers.shutdown_grace_period = Some(301);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Shutdown grace period cannot exceed 300"));
    }

    #[test]
    fn test_config_validation_invalid_scheduler_settings() {
        let mut config = TaskQueueConfig::default();
        config.scheduler.tick_interval = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Scheduler tick interval must be between 1 and 3600"));

        config.scheduler.tick_interval = Some(60);
        config.scheduler.max_tasks_per_tick = Some(0);

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Max tasks per tick must be between 1 and 10000"));
    }

    #[cfg(feature = "actix-integration")]
    #[test]
    fn test_config_validation_invalid_actix_settings() {
        let mut config = TaskQueueConfig::default();
        config.actix.route_prefix = "".to_string();

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Actix route prefix cannot be empty"));

        config.actix.route_prefix = "api/tasks".to_string(); // Missing leading slash

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Actix route prefix must start with '/'"));
    }

    #[cfg(feature = "axum-integration")]
    #[test]
    fn test_config_validation_invalid_axum_settings() {
        let mut config = TaskQueueConfig::default();
        config.axum.route_prefix = "".to_string();

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Axum route prefix cannot be empty"));

        config.axum.route_prefix = "api/tasks".to_string(); // Missing leading slash

        let result = config.validate();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Axum route prefix must start with '/'"));
    }

    #[cfg(feature = "axum-integration")]
    #[test]
    fn test_axum_config_default() {
        let config = AxumConfig::default();

        assert!(!config.route_prefix.is_empty());
        assert!(config.route_prefix.starts_with('/'));
        // Other defaults may vary based on environment variables
    }

    #[test]
    fn test_config_builder() {
        let config = ConfigBuilder::new()
            .redis_url("redis://test:6379")
            .workers(8)
            .enable_auto_register(true)
            .enable_scheduler(true)
            .autoscaler_config(AutoScalerConfig {
                min_workers: 2,
                max_workers: 16,
                scale_up_count: 4,
                scale_down_count: 2,
                scaling_triggers: crate::autoscaler::ScalingTriggers {
                    queue_pressure_threshold: 1.0,
                    worker_utilization_threshold: 0.85,
                    task_complexity_threshold: 1.5,
                    error_rate_threshold: 0.05,
                    memory_pressure_threshold: 512.0,
                },
                enable_adaptive_thresholds: true,
                learning_rate: 0.1,
                adaptation_window_minutes: 30,
                scale_up_cooldown_seconds: 60,
                scale_down_cooldown_seconds: 300,
                consecutive_signals_required: 2,
                target_sla: crate::autoscaler::SLATargets {
                    max_p95_latency_ms: 5000.0,
                    min_success_rate: 0.95,
                    max_queue_wait_time_ms: 10000.0,
                    target_worker_utilization: 0.70,
                },
            })
            .build();

        assert_eq!(config.redis.url, "redis://test:6379");
        assert_eq!(config.workers.initial_count, 8);
        assert!(config.auto_register.enabled);
        assert!(config.scheduler.enabled);
        assert_eq!(config.autoscaler.min_workers, 2);
        assert_eq!(config.autoscaler.max_workers, 16);
    }

    #[test]
    fn test_config_serialization() {
        let config = TaskQueueConfig::default();

        // Test JSON serialization
        let json = serde_json::to_string(&config).expect("Failed to serialize to JSON");
        let deserialized: TaskQueueConfig =
            serde_json::from_str(&json).expect("Failed to deserialize from JSON");

        assert_eq!(config.redis.url, deserialized.redis.url);
        assert_eq!(
            config.workers.initial_count,
            deserialized.workers.initial_count
        );
        assert_eq!(
            config.auto_register.enabled,
            deserialized.auto_register.enabled
        );
        assert_eq!(config.scheduler.enabled, deserialized.scheduler.enabled);
    }

    #[test]
    fn test_config_from_env() {
        // Set environment variables
        env::set_var("REDIS_POOL_SIZE", "15");
        env::set_var("REDIS_CONNECTION_TIMEOUT", "45");

        let config = TaskQueueConfig::from_env().expect("Failed to load config from env");

        assert_eq!(config.redis.pool_size, Some(15));
        assert_eq!(config.redis.connection_timeout, Some(45));

        // Clean up
        env::remove_var("REDIS_POOL_SIZE");
        env::remove_var("REDIS_CONNECTION_TIMEOUT");
    }

    #[test]
    fn test_config_from_env_invalid_values() {
        // Set invalid environment variable
        env::set_var("REDIS_POOL_SIZE", "invalid");

        let result = TaskQueueConfig::from_env();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Invalid REDIS_POOL_SIZE"));

        // Clean up
        env::remove_var("REDIS_POOL_SIZE");
    }

    #[test]
    fn test_config_clone() {
        let original = TaskQueueConfig::default();
        let cloned = original.clone();

        assert_eq!(original.redis.url, cloned.redis.url);
        assert_eq!(original.workers.initial_count, cloned.workers.initial_count);
        assert_eq!(original.auto_register.enabled, cloned.auto_register.enabled);
        assert_eq!(original.scheduler.enabled, cloned.scheduler.enabled);
    }

    #[test]
    fn test_config_debug() {
        let config = TaskQueueConfig::default();
        let debug_str = format!("{:?}", config);

        assert!(debug_str.contains("TaskQueueConfig"));
        assert!(debug_str.contains("redis"));
        assert!(debug_str.contains("workers"));
        assert!(debug_str.contains("autoscaler"));
    }

    #[test]
    fn test_individual_config_structs_clone() {
        let redis_config = RedisConfig::default();
        let cloned_redis = redis_config.clone();
        assert_eq!(redis_config.url, cloned_redis.url);

        let worker_config = WorkerConfig::default();
        let cloned_worker = worker_config.clone();
        assert_eq!(worker_config.initial_count, cloned_worker.initial_count);

        let auto_register_config = AutoRegisterConfig::default();
        let cloned_auto_register = auto_register_config.clone();
        assert_eq!(auto_register_config.enabled, cloned_auto_register.enabled);

        let scheduler_config = SchedulerConfig::default();
        let cloned_scheduler = scheduler_config.clone();
        assert_eq!(scheduler_config.enabled, cloned_scheduler.enabled);
    }

    #[test]
    fn test_config_builder_default() {
        let builder = ConfigBuilder::new();
        let config = builder.build();

        // Should have the same values as TaskQueueConfig::default()
        let default_config = TaskQueueConfig::default();
        assert_eq!(config.redis.url, default_config.redis.url);
        assert_eq!(
            config.workers.initial_count,
            default_config.workers.initial_count
        );
    }

    #[test]
    fn test_config_builder_method_chaining() {
        let config = ConfigBuilder::default()
            .redis_url("redis://chained:6379")
            .workers(5)
            .enable_auto_register(false)
            .enable_scheduler(false)
            .build();

        assert_eq!(config.redis.url, "redis://chained:6379");
        assert_eq!(config.workers.initial_count, 5);
        assert!(!config.auto_register.enabled);
        assert!(!config.scheduler.enabled);
    }

    #[test]
    fn test_redis_url_validation() {
        let test_cases = vec![
            ("redis://localhost:6379", true),
            ("rediss://localhost:6379", true),
            ("redis://user:pass@localhost:6379/0", true),
            ("redis://localhost", true),
            ("http://localhost:6379", false),
            ("localhost:6379", false),
            ("", false),
        ];

        for (url, should_be_valid) in test_cases {
            let mut config = TaskQueueConfig::default();
            config.redis.url = url.to_string();

            let result = config.validate();
            if should_be_valid {
                assert!(result.is_ok(), "URL '{}' should be valid", url);
            } else {
                assert!(result.is_err(), "URL '{}' should be invalid", url);
            }
        }
    }
}