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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Chaos Engineering Framework for QuantLib Pro

Fault injection and resilience testing to validate system robustness.

Features:
- Network failures (latency, timeouts, errors)
- Resource exhaustion (memory, CPU)
- Data corruption
- Service failures
- Cascading failures

Example
-------
>>> from quantlib_pro.testing.chaos import ChaosEngineer, Fault
>>>
>>> chaos = ChaosEngineer()
>>> with chaos.inject(Fault.NETWORK_LATENCY, intensity=0.5):
>>>     # Your system under test
>>>     result = portfolio_optimizer.optimize()
>>>
>>> # System should handle gracefully
>>> assert result is not None
"""

from __future__ import annotations

import contextlib
import logging
import random
import time
from dataclasses import dataclass
from enum import Enum
from typing import Any, Callable, Dict, List, Optional

import numpy as np

log = logging.getLogger(__name__)


class FaultType(Enum):
    """Types of chaos faults."""
    NETWORK_LATENCY = 'network_latency'  # Add random delays
    NETWORK_TIMEOUT = 'network_timeout'  # Simulate timeouts
    NETWORK_ERROR = 'network_error'  # Simulate network errors
    MEMORY_PRESSURE = 'memory_pressure'  # Consume memory
    CPU_SPIKE = 'cpu_spike'  # CPU intensive operations
    DATA_CORRUPTION = 'data_corruption'  # Corrupt data randomly
    SERVICE_FAILURE = 'service_failure'  # Complete service failure
    INTERMITTENT_FAILURE = 'intermittent_failure'  # Random failures
    SLOW_RESPONSE = 'slow_response'  # Degraded performance
    CASCADING_FAILURE = 'cascading_failure'  # Chain of failures


@dataclass
class FaultInjection:
    """Fault injection configuration."""
    fault_type: FaultType
    intensity: float  # 0.0 to 1.0
    duration: Optional[float] = None  # Seconds, None = indefinite
    probability: float = 1.0  # Probability of fault occurring (0-1)
    metadata: Dict = None
    
    def __post_init__(self):
        if self.metadata is None:
            self.metadata = {}


@dataclass
class ChaosExperiment:
    """Chaos experiment definition."""
    name: str
    faults: List[FaultInjection]
    steady_state_validator: Callable[[], bool]
    duration: float = 60.0  # Experiment duration in seconds
    
    def validate_steady_state(self) -> bool:
        """Check if system is in steady state."""
        try:
            return self.steady_state_validator()
        except Exception as e:
            log.error(f"Steady state validation failed: {e}")
            return False


class ChaosEngineer:
    """
    Chaos engineering framework.
    
    Injects faults to test system resilience and recovery.
    
    Example
    -------
    >>> chaos = ChaosEngineer()
    >>>
    >>> # Inject network latency
    >>> with chaos.inject_fault(FaultType.NETWORK_LATENCY, intensity=0.5):
    >>>     data = fetch_market_data()  # Should handle delays gracefully
    >>>
    >>> # Inject data corruption
    >>> with chaos.inject_fault(FaultType.DATA_CORRUPTION, intensity=0.1):
    >>>     result = calculate_var(corrupted_data)  # Should detect corruption
    """
    
    def __init__(self):
        self.active_faults: List[FaultInjection] = []
        self.experiment_results: List[Dict] = []
        
        log.info("Initialized ChaosEngineer")
    
    @contextlib.contextmanager
    def inject_fault(
        self,
        fault_type: FaultType,
        intensity: float = 0.5,
        probability: float = 1.0,
    ):
        """
        Context manager for fault injection.
        
        Parameters
        ----------
        fault_type : FaultType
            Type of fault to inject
        intensity : float
            Fault intensity (0.0 to 1.0)
        probability : float
            Probability of fault (0.0 to 1.0)
        
        Example
        -------
        >>> with chaos.inject_fault(FaultType.NETWORK_LATENCY, intensity=0.8):
        >>>     result = api_call()  # Will experience delays
        """
        fault = FaultInjection(
            fault_type=fault_type,
            intensity=intensity,
            probability=probability,
        )
        
        self.active_faults.append(fault)
        log.info(f"Injecting fault: {fault_type.value} (intensity={intensity})")
        
        try:
            yield fault
        finally:
            self.active_faults.remove(fault)
            log.info(f"Removed fault: {fault_type.value}")
    
    def maybe_inject_network_latency(self, base_latency: float = 0.0) -> float:
        """Add network latency based on active faults."""
        added_latency = base_latency
        
        for fault in self.active_faults:
            if fault.fault_type == FaultType.NETWORK_LATENCY:
                if random.random() < fault.probability:
                    # Add 0-5 seconds based on intensity
                    delay = fault.intensity * 5.0
                    added_latency += delay
        
        if added_latency > base_latency:
            time.sleep(added_latency - base_latency)
        
        return added_latency
    
    def maybe_inject_network_error(self) -> bool:
        """Check if network error should be injected."""
        for fault in self.active_faults:
            if fault.fault_type == FaultType.NETWORK_ERROR:
                if random.random() < (fault.intensity * fault.probability):
                    return True
        
        return False
    
    def maybe_inject_timeout(self, timeout: float) -> float:
        """Modify timeout based on active faults."""
        for fault in self.active_faults:
            if fault.fault_type == FaultType.NETWORK_TIMEOUT:
                if random.random() < fault.probability:
                    # Reduce timeout by intensity percentage
                    timeout = timeout * (1 - fault.intensity)
        
        return timeout
    
    def maybe_corrupt_data(self, data: Any) -> Any:
        """Corrupt data based on active faults."""
        for fault in self.active_faults:
            if fault.fault_type == FaultType.DATA_CORRUPTION:
                if random.random() < fault.probability:
                    # Corrupt data based on type
                    if isinstance(data, (list, np.ndarray)):
                        return self._corrupt_array(data, fault.intensity)
                    elif isinstance(data, dict):
                        return self._corrupt_dict(data, fault.intensity)
                    elif isinstance(data, (int, float)):
                        return self._corrupt_number(data, fault.intensity)
        
        return data
    
    def _corrupt_array(self, arr: Any, intensity: float) -> Any:
        """Corrupt array data."""
        if isinstance(arr, np.ndarray):
            arr = arr.copy()
        else:
            arr = list(arr)
        
        # Corrupt random elements
        n_corrupt = max(1, int(len(arr) * intensity))
        indices = random.sample(range(len(arr)), min(n_corrupt, len(arr)))
        
        for idx in indices:
            if isinstance(arr[idx], (int, float)):
                arr[idx] = np.nan  # Replace with NaN
        
        return arr
    
    def _corrupt_dict(self, d: Dict, intensity: float) -> Dict:
        """Corrupt dictionary data."""
        d = d.copy()
        
        # Remove random keys
        if random.random() < intensity:
            keys = list(d.keys())
            if keys:
                key_to_remove = random.choice(keys)
                del d[key_to_remove]
        
        return d
    
    def _corrupt_number(self, value: float, intensity: float) -> float:
        """Corrupt numeric value."""
        if random.random() < intensity:
            # Scale by large factor
            return value * (1 + random.uniform(-10, 10))
        
        return value
    
    def maybe_inject_service_failure(self) -> bool:
        """Check if service should fail completely."""
        for fault in self.active_faults:
            if fault.fault_type == FaultType.SERVICE_FAILURE:
                if random.random() < fault.probability:
                    return True
        
        return False
    
    def maybe_inject_intermittent_failure(self) -> bool:
        """Check if intermittent failure should occur."""
        for fault in self.active_faults:
            if fault.fault_type == FaultType.INTERMITTENT_FAILURE:
                # Failures occur based on intensity
                if random.random() < (fault.intensity * fault.probability):
                    return True
        
        return False
    
    def run_experiment(self, experiment: ChaosExperiment) -> Dict[str, Any]:
        """
        Run chaos experiment.
        
        Parameters
        ----------
        experiment : ChaosExperiment
            Experiment configuration
        
        Returns
        -------
        dict
            Experiment results
        """
        log.info(f"Running chaos experiment: {experiment.name}")
        
        # 1. Verify steady state
        initial_steady = experiment.validate_steady_state()
        
        if not initial_steady:
            log.warning("System not in steady state before experiment")
        
        # 2. Inject faults
        start_time = time.time()
        
        for fault in experiment.faults:
            self.active_faults.append(fault)
        
        # 3. Run for duration
        try:
            time.sleep(experiment.duration)
        finally:
            # 4. Remove faults
            for fault in experiment.faults:
                if fault in self.active_faults:
                    self.active_faults.remove(fault)
        
        # 5. Verify steady state recovered
        final_steady = experiment.validate_steady_state()
        
        duration = time.time() - start_time
        
        result = {
            'experiment_name': experiment.name,
            'duration': duration,
            'initial_steady_state': initial_steady,
            'final_steady_state': final_steady,
            'recovered': final_steady,
            'faults_injected': len(experiment.faults),
        }
        
        self.experiment_results.append(result)
        
        log.info(f"Experiment complete: {experiment.name} - Recovered: {final_steady}")
        
        return result


class ResilienceValidator:
    """
    Validates system resilience patterns.
    
    Tests common resilience patterns:
    - Circuit breaker
    - Retry with backoff
    - Fallback
    - Timeout
    - Bulkhead isolation
    """
    
    def __init__(self, chaos: ChaosEngineer):
        self.chaos = chaos
        self.test_results: List[Dict] = []
    
    def test_circuit_breaker(
        self,
        protected_function: Callable,
        failure_threshold: int = 5,
    ) -> Dict[str, Any]:
        """
        Test circuit breaker pattern.
        
        Parameters
        ----------
        protected_function : callable
            Function protected by circuit breaker
        failure_threshold : int
            Number of failures before circuit opens
        
        Returns
        -------
        dict
            Test results
        """
        log.info("Testing circuit breaker pattern")
        
        # Inject service failures
        failures = 0
        successes = 0
        
        with self.chaos.inject_fault(FaultType.SERVICE_FAILURE, intensity=1.0):
            # Should fail initially
            for i in range(failure_threshold + 2):
                try:
                    protected_function()
                    successes += 1
                except Exception:
                    failures += 1
        
        # Circuit should open after threshold
        circuit_opened = failures >= failure_threshold
        
        result = {
            'pattern': 'circuit_breaker',
            'failures': failures,
            'successes': successes,
            'circuit_opened': circuit_opened,
            'passed': circuit_opened,
        }
        
        self.test_results.append(result)
        
        return result
    
    def test_retry_with_backoff(
        self,
        flaky_function: Callable,
        max_retries: int = 3,
    ) -> Dict[str, Any]:
        """
        Test retry with exponential backoff.
        
        Parameters
        ----------
        flaky_function : callable
            Function that may fail intermittently
        max_retries : int
            Maximum retry attempts
        
        Returns
        -------
        dict
            Test results
        """
        log.info("Testing retry with backoff pattern")
        
        attempts = 0
        success = False
        
        # Inject intermittent failures
        with self.chaos.inject_fault(FaultType.INTERMITTENT_FAILURE, intensity=0.5):
            for attempt in range(max_retries + 1):
                attempts += 1
                
                try:
                    flaky_function()
                    success = True
                    break
                except Exception:
                    if attempt < max_retries:
                        # Exponential backoff
                        backoff = 2 ** attempt * 0.1
                        time.sleep(backoff)
        
        result = {
            'pattern': 'retry_with_backoff',
            'attempts': attempts,
            'success': success,
            'passed': success or attempts > max_retries,
        }
        
        self.test_results.append(result)
        
        return result
    
    def test_fallback(
        self,
        primary_function: Callable,
        fallback_function: Callable,
    ) -> Dict[str, Any]:
        """
        Test fallback pattern.
        
        Parameters
        ----------
        primary_function : callable
            Primary data source
        fallback_function : callable
            Fallback data source
        
        Returns
        -------
        dict
            Test results
        """
        log.info("Testing fallback pattern")
        
        result_source = None
        
        # Inject service failure on primary
        with self.chaos.inject_fault(FaultType.SERVICE_FAILURE, intensity=1.0):
            try:
                primary_function()
                result_source = 'primary'
            except Exception:
                # Should fall back
                try:
                    fallback_function()
                    result_source = 'fallback'
                except Exception:
                    result_source = 'failed'
        
        result = {
            'pattern': 'fallback',
            'result_source': result_source,
            'passed': result_source == 'fallback',
        }
        
        self.test_results.append(result)
        
        return result
    
    def test_timeout(
        self,
        slow_function: Callable,
        timeout: float = 1.0,
    ) -> Dict[str, Any]:
        """
        Test timeout pattern.
        
        Parameters
        ----------
        slow_function : callable
            Function that may be slow
        timeout : float
            Timeout in seconds
        
        Returns
        -------
        dict
            Test results
        """
        log.info("Testing timeout pattern")
        
        timed_out = False
        
        # Inject latency
        with self.chaos.inject_fault(FaultType.NETWORK_LATENCY, intensity=1.0):
            start = time.time()
            
            try:
                # This should timeout
                slow_function()
            except TimeoutError:
                timed_out = True
            except Exception:
                pass
            
            elapsed = time.time() - start
        
        # Should timeout within reasonable margin
        within_timeout = elapsed <= (timeout * 1.5)
        
        result = {
            'pattern': 'timeout',
            'timed_out': timed_out,
            'elapsed': elapsed,
            'within_timeout': within_timeout,
            'passed': timed_out or within_timeout,
        }
        
        self.test_results.append(result)
        
        return result
    
    def generate_report(self) -> str:
        """Generate resilience test report."""
        lines = [
            "Resilience Pattern Test Report",
            "=" * 50,
            "",
        ]
        
        total_tests = len(self.test_results)
        passed_tests = sum(1 for r in self.test_results if r.get('passed', False))
        
        for result in self.test_results:
            pattern = result.get('pattern', 'unknown')
            passed = result.get('passed', False)
            status = "✅ PASS" if passed else "❌ FAIL"
            
            lines.append(f"{pattern}: {status}")
        
        lines.append("")
        lines.append(f"Total: {passed_tests}/{total_tests} passed")
        
        return '\n'.join(lines)