To simulate a service failure, you can create a mock service that randomly fails based on certain conditions. Here's a simple example in Python:
import random
import time
def simulate_service():
# Simulate a service that randomly fails
if random.random() < 0.3: # 30% chance of failure
raise Exception("Service failure occurred!")
return "Service is running smoothly."
def main():
for _ in range(5):
try:
result = simulate_service()
print(result)
except Exception as e:
print(e)
time.sleep(1) # Wait for a second before the next call
if __name__ == "__main__":
main()
In this code:
- The
simulate_servicefunction randomly raises an exception to simulate a service failure. - The
mainfunction calls the service multiple times, handling any exceptions that occur.
You can adjust the failure probability by changing the value in the if statement.
