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
//! Architectural compliance tests for the domain layer.
//!
//! These tests ensure that the domain layer maintains Clean Architecture
//! principles:
//! - No dependencies on infrastructure layer types
//! - Pure business logic without external concerns
//! - Clear separation of concerns
#[cfg(test)]
mod architectural_compliance {
use std::any::TypeId;
use super::super::*;
/// Test that domain resilience contracts don't depend on infrastructure
/// types
#[test]
fn domain_resilience_contracts_no_infrastructure_dependencies() {
// This test ensures that domain layer resilience types don't accidentally
// import infrastructure types. We do this by checking that the types
// don't contain references to known infrastructure modules.
let _resilience_policy_type = TypeId::of::<ResiliencePolicy>();
let _backoff_strategy_type = TypeId::of::<BackoffStrategy>();
let _domain_error_type = TypeId::of::<ResilienceDomainError>();
// These types should be pure domain concepts
// If this test fails, it means infrastructure types have leaked into domain
// layer
// The types should exist and be constructible without infrastructure
let policy = ResiliencePolicy::None;
let backoff = BackoffStrategy::default();
let error = ResilienceDomainError::Timeout {
duration: std::time::Duration::from_secs(1),
};
// Ensure they're the expected types
assert!(matches!(policy, ResiliencePolicy::None));
assert!(matches!(backoff, BackoffStrategy::Exponential { .. }));
assert!(matches!(error, ResilienceDomainError::Timeout { .. }));
}
/// Test that domain layer can be used without any infrastructure imports
#[test]
fn domain_layer_standalone_usage() {
// This test verifies that domain layer code can be written and used
// without importing any infrastructure types
// Define a domain service that declares resilience requirements
struct PaymentService;
impl PaymentService {
fn process_payment(&self, amount: u32) -> Result<String, ResilienceDomainError> {
if amount > 1000 {
return Err(ResilienceDomainError::RateLimited {
retry_after: Some(std::time::Duration::from_secs(60)),
});
}
Ok(format!("Processed payment of ${}", amount))
}
}
// Domain layer can declare policies without knowing how they're implemented
let policy = ResiliencePolicy::Retry {
max_attempts: 3,
backoff: BackoffStrategy::Exponential {
initial_delay: std::time::Duration::from_millis(100),
multiplier: 2.0,
max_delay: Some(std::time::Duration::from_secs(10)),
jitter: true,
},
};
// Verify the policy is well-formed
match policy {
ResiliencePolicy::Retry {
max_attempts,
backoff: BackoffStrategy::Exponential { .. },
} => {
assert_eq!(max_attempts, 3);
}
_ => panic!("Expected retry policy"),
}
// Domain logic works independently
let service = PaymentService;
let result = service.process_payment(500);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Processed payment of $500");
}
/// Test that domain error types are properly isolated
#[test]
fn domain_error_isolation() {
// Domain errors should not depend on infrastructure error types
let timeout_error = ResilienceDomainError::Timeout {
duration: std::time::Duration::from_secs(5),
};
let retry_error = ResilienceDomainError::RetryExhausted {
attempts: 3,
last_error: "Connection failed".to_string(),
};
let circuit_error = ResilienceDomainError::CircuitOpen;
// These should be pure domain concepts
assert!(timeout_error.is_retryable());
assert!(!retry_error.is_retryable()); // Already exhausted retries
assert!(!circuit_error.is_retryable()); // Circuit breaker protects from further calls
assert!(!timeout_error.is_service_unavailable());
assert!(circuit_error.is_service_unavailable());
}
/// Test that ResilientOperation trait maintains domain purity
#[test]
fn resilient_operation_domain_purity() {
// Domain operations should be able to declare resilience without infrastructure
// knowledge
struct DomainOperation {
operation_id: String,
critical: bool,
}
#[async_trait::async_trait]
impl ResilientOperation<String, ResilienceDomainError> for DomainOperation {
fn resilience_policy(&self) -> ResiliencePolicy {
ResiliencePolicy::CircuitBreaker {
failure_threshold: 5,
recovery_timeout: std::time::Duration::from_secs(30),
success_threshold: 3,
}
}
async fn execute(&self) -> Result<String, ResilienceDomainError> {
// Pure domain logic - no infrastructure dependencies
if self.operation_id == "fail" {
Err(ResilienceDomainError::Infrastructure {
message: "Domain logic failure".to_string(),
})
} else {
Ok(format!("Executed {}", self.operation_id))
}
}
fn operation_id(&self) -> &str {
&self.operation_id
}
fn is_critical(&self) -> bool {
self.critical
}
}
// Test domain operation construction and policy declaration
let op = DomainOperation {
operation_id: "test_op".to_string(),
critical: true,
};
assert_eq!(op.operation_id(), "test_op");
assert!(op.is_critical());
// Policy should be declarable without infrastructure knowledge
let policy = op.resilience_policy();
match policy {
ResiliencePolicy::CircuitBreaker {
failure_threshold, ..
} => {
assert_eq!(failure_threshold, 5);
}
_ => panic!("Expected circuit breaker policy"),
}
}
/// Test policy helper functions maintain domain purity
#[test]
fn policy_helpers_domain_purity() {
use crate::domain::resilience::policies;
// Policy helpers should create pure domain policies
let retry_policy = policies::retry(5);
let circuit_policy = policies::circuit_breaker(10, 60);
let rate_limit_policy = policies::rate_limit(100);
let timeout_policy = policies::timeout(30);
let combined_policy = policies::combine(vec![retry_policy.clone(), timeout_policy.clone()]);
// Verify policies are constructed correctly
match retry_policy {
ResiliencePolicy::Retry { max_attempts, .. } => assert_eq!(max_attempts, 5),
_ => panic!("Expected retry policy"),
}
match circuit_policy {
ResiliencePolicy::CircuitBreaker {
failure_threshold,
recovery_timeout,
..
} => {
assert_eq!(failure_threshold, 10);
assert_eq!(recovery_timeout, std::time::Duration::from_secs(60));
}
_ => panic!("Expected circuit breaker policy"),
}
match rate_limit_policy {
ResiliencePolicy::RateLimit {
requests_per_second,
..
} => {
assert_eq!(requests_per_second, 100);
}
_ => panic!("Expected rate limit policy"),
}
match timeout_policy {
ResiliencePolicy::Timeout { duration } => {
assert_eq!(duration, std::time::Duration::from_secs(30));
}
_ => panic!("Expected timeout policy"),
}
match combined_policy {
ResiliencePolicy::Combined { policies } => {
assert_eq!(policies.len(), 2);
}
_ => panic!("Expected combined policy"),
}
}
}
/// Integration tests to ensure domain and application layers work together
/// cleanly
#[cfg(all(test, feature = "resilience"))]
mod domain_application_integration {
use super::super::*;
use crate::application::resilience::{DefaultResilienceOrchestrator, ResilienceOrchestrator};
#[tokio::test]
async fn domain_application_layer_integration() {
// Domain layer defines operation
struct TestOperation;
#[async_trait::async_trait]
impl ResilientOperation<i32, ResilienceDomainError> for TestOperation {
fn resilience_policy(&self) -> ResiliencePolicy {
ResiliencePolicy::Retry {
max_attempts: 2,
backoff: BackoffStrategy::Fixed {
delay: std::time::Duration::from_millis(10),
},
}
}
async fn execute(&self) -> Result<i32, ResilienceDomainError> {
static CALL_COUNT: std::sync::atomic::AtomicU32 =
std::sync::atomic::AtomicU32::new(0);
let count = CALL_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
if count < 1 {
// Fail first attempt
Err(ResilienceDomainError::Infrastructure {
message: "Temporary failure".to_string(),
})
} else {
// Succeed on retry
Ok(42)
}
}
}
// Application layer orchestrates with infrastructure
let orchestrator = DefaultResilienceOrchestrator::new();
let operation = TestOperation;
// Execute through application layer - domain stays pure
let result = orchestrator.execute_operation(operation).await;
// Should succeed after retry
assert_eq!(result, Ok(42));
// Verify metrics were recorded
let metrics = orchestrator.metrics();
assert_eq!(metrics.total_operations, 1);
assert_eq!(metrics.successful_operations, 1);
assert_eq!(metrics.retry_attempts, 1); // One retry attempt
}
#[tokio::test]
async fn infrastructure_failures_properly_handled() {
// Test that infrastructure failures are properly translated to domain errors
let orchestrator = DefaultResilienceOrchestrator::new();
let result = orchestrator
.execute_with_policy(
ResiliencePolicy::Retry {
max_attempts: 1, // Only one attempt
backoff: BackoffStrategy::Fixed {
delay: std::time::Duration::from_millis(1),
},
},
|| async {
Result::<i32, ResilienceDomainError>::Err(
ResilienceDomainError::Infrastructure {
message: "Database connection failed".to_string(),
},
)
},
)
.await;
// Should fail with retry exhausted
match result {
Err(crate::application::resilience::ResilienceOrchestrationError::Domain(
ResilienceDomainError::RetryExhausted { attempts, .. },
)) => {
assert_eq!(attempts, 1);
}
other => panic!("Expected retry exhausted error, got {:?}", other),
}
}
}