-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
175 lines (141 loc) · 5.58 KB
/
example.py
File metadata and controls
175 lines (141 loc) · 5.58 KB
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
"""
Example usage of the chaotic-exceptions library.
"""
from chaotic_exceptions import (
ChaoticExceptionGenerator,
chaos_monkey,
random_exception,
NetworkChaosException,
DatabaseChaosException
)
import time
def example_basic_usage():
"""Basic usage examples."""
print("=== Basic Usage Examples ===")
# Simple random exception with 100% probability
print("1. Force a random exception:")
try:
random_exception(probability=1.0)
except Exception as e:
print(f" Caught: {type(e).__name__}: {e}")
# Random exception with 50% probability
print("\n2. Maybe raise an exception (50% chance):")
for i in range(5):
try:
random_exception(probability=0.5)
print(f" Attempt {i+1}: No exception raised")
except Exception as e:
print(f" Attempt {i+1}: Caught {type(e).__name__}: {e}")
def example_decorator():
"""Example using the chaos_monkey decorator."""
print("\n=== Decorator Examples ===")
@chaos_monkey(probability=0.3)
def unreliable_function():
return "Function executed successfully!"
@chaos_monkey(probability=0.5, exception_types=[NetworkChaosException])
def network_operation():
return "Network operation completed!"
print("1. Testing unreliable function (30% failure rate):")
for i in range(5):
try:
result = unreliable_function()
print(f" Attempt {i+1}: {result}")
except Exception as e:
print(f" Attempt {i+1}: Failed with {type(e).__name__}: {e}")
print("\n2. Testing network operation (50% failure rate, network errors only):")
for i in range(5):
try:
result = network_operation()
print(f" Attempt {i+1}: {result}")
except NetworkChaosException as e:
print(f" Attempt {i+1}: Network failed: {e}")
def example_generator():
"""Example using ChaoticExceptionGenerator."""
print("\n=== Generator Examples ===")
# Create a generator with specific exception types
chaos = ChaoticExceptionGenerator(
exception_types=[NetworkChaosException, DatabaseChaosException],
probability=0.4,
seed=42 # For reproducible results
)
print("1. Using generator with specific exception types:")
for i in range(5):
try:
chaos.maybe_raise()
print(f" Operation {i+1}: Success")
except Exception as e:
print(f" Operation {i+1}: Failed with {type(e).__name__}: {e}")
def example_context_manager():
"""Example using context manager."""
print("\n=== Context Manager Examples ===")
chaos = ChaoticExceptionGenerator(probability=0.3)
for i in range(3):
try:
with chaos.chaos_context():
print(f" Inside context {i+1}: Doing some work...")
time.sleep(0.1) # Simulate work
print(f" Context {i+1}: Work completed")
except Exception as e:
print(f" Context {i+1}: Failed with {type(e).__name__}: {e}")
def example_custom_messages():
"""Example with custom error messages."""
print("\n=== Custom Messages Examples ===")
custom_messages = {
NetworkChaosException: [
"The internet is broken again!",
"Network hamsters stopped running",
"WiFi is having an existential crisis"
],
DatabaseChaosException: [
"Database went on vacation",
"SQL queries are feeling antisocial",
"The database is having trust issues"
]
}
chaos = ChaoticExceptionGenerator(
exception_types=[NetworkChaosException, DatabaseChaosException],
custom_messages=custom_messages,
probability=1.0 # Always fail for demonstration
)
print("1. Custom error messages:")
for i in range(3):
try:
chaos.force_raise()
except Exception as e:
print(f" Error {i+1}: {type(e).__name__}: {e}")
def example_testing_resilience():
"""Example of testing system resilience."""
print("\n=== Testing System Resilience ===")
@chaos_monkey(probability=0.2, exception_types=[NetworkChaosException])
def api_call(data):
"""Simulate an API call that might fail."""
return f"API response for: {data}"
def resilient_api_call(data, max_retries=3):
"""A resilient version with retry logic."""
for attempt in range(max_retries):
try:
return api_call(data)
except NetworkChaosException as e:
print(f" Attempt {attempt + 1} failed: {e}")
if attempt == max_retries - 1:
raise
time.sleep(0.1 * (attempt + 1)) # Exponential backoff
print("1. Testing resilient API calls:")
test_data = ["user1", "user2", "user3", "user4", "user5"]
for data in test_data:
try:
result = resilient_api_call(data)
print(f" Success: {result}")
except Exception as e:
print(f" Final failure for {data}: {type(e).__name__}: {e}")
if __name__ == "__main__":
print("Chaotic Exceptions Library - Example Usage\n" + "="*50)
example_basic_usage()
example_decorator()
example_generator()
example_context_manager()
example_custom_messages()
example_testing_resilience()
print("\n" + "="*50)
print("Examples completed! Check out the different exception types and messages.")
print("Remember: In production, use much lower probabilities (0.01-0.05)!")