confers 0.4.1

Production-ready Rust configuration library with zero boilerplate
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
//! Integration tests for progressive reload support.
//!
//! These tests verify the ProgressiveReloader implementation with various strategies.
//! Uses real configuration types instead of mocks for more realistic testing.

#![cfg(feature = "progressive-reload")]

mod common;

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use confers::error::ConfigError;
use confers::interface::ConfigProvider;
use confers::watcher::{
    HealthStatus, ProgressiveReloader, ReloadHealthCheck, ReloadOutcome, ReloadStrategy,
};

// Real health check that validates configuration values
#[derive(Debug)]
struct ConfigHealthCheck {
    max_timeout_ms: u32,
    min_connections: usize,
}

impl ConfigHealthCheck {
    fn new(max_timeout_ms: u32, min_connections: usize) -> Self {
        Self {
            max_timeout_ms,
            min_connections,
        }
    }
}

#[async_trait]
impl ReloadHealthCheck for ConfigHealthCheck {
    async fn check(&self, provider: Arc<dyn ConfigProvider>) -> HealthStatus {
        let timeout = provider
            .get_raw("timeout_ms")
            .and_then(|v| v.inner.as_u64())
            .unwrap_or(0) as u32;

        let connections = provider
            .get_raw("max_connections")
            .and_then(|v| v.inner.as_u64())
            .unwrap_or(0) as usize;

        if timeout > self.max_timeout_ms {
            return HealthStatus::Critical {
                reason: format!(
                    "timeout {} exceeds maximum allowed {}",
                    timeout, self.max_timeout_ms
                ),
            };
        }

        if connections < self.min_connections {
            return HealthStatus::Degraded {
                reason: format!(
                    "connections {} below minimum {}",
                    connections, self.min_connections
                ),
            };
        }

        HealthStatus::Healthy
    }
}

// Health check that always returns healthy (for testing successful scenarios)
#[derive(Debug)]
struct AlwaysHealthyCheck;

#[async_trait]
impl ReloadHealthCheck for AlwaysHealthyCheck {
    async fn check(&self, _provider: Arc<dyn ConfigProvider>) -> HealthStatus {
        HealthStatus::Healthy
    }
}

// Health check that always returns critical (for testing rollback scenarios)
#[derive(Debug)]
struct AlwaysCriticalCheck {
    reason: String,
}

impl AlwaysCriticalCheck {
    fn new(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
        }
    }
}

#[async_trait]
impl ReloadHealthCheck for AlwaysCriticalCheck {
    async fn check(&self, _provider: Arc<dyn ConfigProvider>) -> HealthStatus {
        HealthStatus::Critical {
            reason: self.reason.clone(),
        }
    }
}

// Health check that always returns degraded (for testing degraded scenarios)
#[derive(Debug)]
struct AlwaysDegradedCheck {
    reason: String,
}

impl AlwaysDegradedCheck {
    fn new(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
        }
    }
}

#[async_trait]
impl ReloadHealthCheck for AlwaysDegradedCheck {
    async fn check(&self, _provider: Arc<dyn ConfigProvider>) -> HealthStatus {
        HealthStatus::Degraded {
            reason: self.reason.clone(),
        }
    }
}

// Test: Immediate reload commits successfully
#[tokio::test]
async fn test_immediate_reload_commits() {
    let initial_config = Arc::new(common::TestConfig::new(100, 10));
    let reloader = ProgressiveReloader::new(initial_config, ReloadStrategy::Immediate);

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
    assert_eq!(reloader.current().max_connections, 20);
}

// Test: Immediate reload replaces current config atomically
#[tokio::test]
async fn test_immediate_reload_atomic() {
    let initial_config = Arc::new(common::TestConfig::new(100, 10));
    let reloader = ProgressiveReloader::new(initial_config, ReloadStrategy::Immediate);

    let new_config = Arc::new(common::TestConfig::new(300, 30));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config, provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 300);
    assert_eq!(reloader.current().max_connections, 30);
}

// Test: Canary strategy commits after healthy trial period
#[tokio::test]
async fn test_canary_reload_commits_when_healthy() {
    let health_check = Arc::new(AlwaysHealthyCheck);
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_millis(50),
            poll_interval: Duration::from_millis(10),
        })
        .health_check(health_check)
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
}

// Test: Canary strategy rolls back on critical health status
#[tokio::test]
async fn test_canary_reload_rollback_on_critical() {
    let health_check = Arc::new(AlwaysCriticalCheck::new("config validation failed"));
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_secs(5),
            poll_interval: Duration::from_millis(10),
        })
        .health_check(health_check)
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Err(ConfigError::ReloadRolledBack { .. })));
    assert_eq!(reloader.current().timeout_ms, 100);
}

// Test: Canary strategy warns but continues on degraded health
#[tokio::test]
async fn test_canary_reload_continues_on_degraded() {
    let health_check = Arc::new(AlwaysDegradedCheck::new("high latency"));
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_millis(50),
            poll_interval: Duration::from_millis(10),
        })
        .health_check(health_check)
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
}

// Test: Canary without health check works (pass-through)
#[tokio::test]
async fn test_canary_reload_without_health_check() {
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_millis(50),
            poll_interval: Duration::from_millis(10),
        })
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
}

// Test: Real health check validates configuration values
#[tokio::test]
async fn test_real_health_check_validates_config() {
    let health_check = Arc::new(ConfigHealthCheck::new(500, 5));
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_millis(50),
            poll_interval: Duration::from_millis(10),
        })
        .health_check(health_check)
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 15));
    let provider = Arc::new(common::TestConfig::new(200, 15));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
}

// Test: Real health check triggers critical on invalid config
#[tokio::test]
async fn test_real_health_check_critical_on_invalid() {
    let health_check = Arc::new(ConfigHealthCheck::new(500, 5));
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Canary {
            trial_duration: Duration::from_secs(5),
            poll_interval: Duration::from_millis(10),
        })
        .health_check(health_check)
        .build();

    let new_config = Arc::new(common::TestConfig::new(1000, 15));
    let provider = Arc::new(common::TestConfig::new(1000, 15));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Err(ConfigError::ReloadRolledBack { .. })));
    assert_eq!(reloader.current().timeout_ms, 100);
}

// Test: ProgressiveReloader can be cloned
#[test]
fn test_progressive_reloader_is_clone() {
    let reloader = ProgressiveReloader::new(
        Arc::new(common::TestConfig::new(100, 10)),
        ReloadStrategy::Immediate,
    );

    let _cloned = reloader.clone();
}

// Test: ProgressiveReloader current() returns Arc<T>
#[test]
fn test_current_returns_arc() {
    let reloader = ProgressiveReloader::new(
        Arc::new(common::TestConfig::new(42, 100)),
        ReloadStrategy::Immediate,
    );

    let current = reloader.current();
    assert_eq!(current.timeout_ms, 42);
    assert_eq!(current.max_connections, 100);
}

// Test: ReloadOutcome enum variants
#[test]
fn test_reload_outcome_variants() {
    let committed = ReloadOutcome::Committed;
    let rolled_back = ReloadOutcome::RolledBack {
        reason: "test".to_string(),
    };

    assert!(matches!(committed, ReloadOutcome::Committed));
    assert!(matches!(
        rolled_back,
        ReloadOutcome::RolledBack { reason } if reason == "test"
    ));
}

// Test: HealthStatus enum variants
#[test]
fn test_health_status_variants() {
    let healthy = HealthStatus::Healthy;
    let degraded = HealthStatus::Degraded {
        reason: "high latency".to_string(),
    };
    let critical = HealthStatus::Critical {
        reason: "service down".to_string(),
    };

    assert!(matches!(healthy, HealthStatus::Healthy));
    assert!(matches!(
        degraded,
        HealthStatus::Degraded { reason } if reason == "high latency"
    ));
    assert!(matches!(
        critical,
        HealthStatus::Critical { reason } if reason == "service down"
    ));
}

// Test: Default builder creates working reloader
#[test]
fn test_builder_default() {
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .build();

    assert_eq!(reloader.current().timeout_ms, 100);
}

// Test: with_dependencies constructor
#[test]
fn test_with_dependencies() {
    let health_check = Arc::new(AlwaysHealthyCheck);
    let reloader = ProgressiveReloader::with_dependencies(
        Arc::new(common::TestConfig::new(100, 10)),
        ReloadStrategy::Immediate,
        Some(health_check),
    );

    assert_eq!(reloader.current().timeout_ms, 100);
}

// Test: Linear strategy commits after all steps
#[tokio::test]
async fn test_linear_reload_commits_after_steps() {
    let reloader = ProgressiveReloader::builder()
        .initial(Arc::new(common::TestConfig::new(100, 10)))
        .strategy(ReloadStrategy::Linear {
            steps: 3,
            interval: Duration::from_millis(20),
        })
        .build();

    let new_config = Arc::new(common::TestConfig::new(200, 20));
    let provider = Arc::new(common::TestConfig::new(100, 10));

    let result = reloader.begin_reload(new_config.clone(), provider).await;

    assert!(matches!(result, Ok(ReloadOutcome::Committed)));
    assert_eq!(reloader.current().timeout_ms, 200);
}

// Test: ConfigProvider implementation works correctly
#[test]
fn test_config_provider_implementation() {
    let config = common::TestConfig::new(500, 100);

    assert!(config.get_raw("timeout_ms").is_some());
    assert!(config.get_raw("max_connections").is_some());
    assert!(config.get_raw("nonexistent").is_none());

    let keys = config.keys();
    assert_eq!(keys.len(), 4);
}

// Test: ConfigHealthCheck returns degraded for low connections
#[tokio::test]
async fn test_config_health_check_degraded() {
    let health_check = ConfigHealthCheck::new(1000, 10);

    let config = Arc::new(common::TestConfig::new(500, 5));
    let status = health_check.check(config).await;

    assert!(matches!(status, HealthStatus::Degraded { .. }));
}