Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. from time import sleep
  2. from cb import CircuitBreaker
  3.  
  4. MY_EXCEPTION = 'Threw Dependency Exception'
  5.  
  6. @CircuitBreaker(max_failure_to_open=3, reset_timeout=3)
  7. def dependency_call(call_num):
  8. if call_num in (3, 4, 5, 6, 7, 17, 19):
  9. raise Exception(MY_EXCEPTION)
  10. else:
  11. return 'SUCCESS'
  12.  
  13. def run():
  14. num_success = 0
  15. num_failure = 0
  16. num_circuitbreaker_passthrough_failure = 0
  17.  
  18. for i in range(1, 21):
  19. try:
  20. print "Call-%d:" % i
  21. print "Result=%s" % dependency_call(i)
  22. num_success += 1
  23. except Exception as ex:
  24. num_failure += 1
  25. if ex.message == MY_EXCEPTION:
  26. num_circuitbreaker_passthrough_failure += 1
  27. print ex.message
  28.  
  29. sleep(0.5)
  30.  
  31. return num_success, num_failure, num_circuitbreaker_passthrough_failure
  32.  
  33.  
  34. if __name__ == "__main__":
  35. success, failure, pass_through_failure = run()
  36. print "num_success=%d, num_failure=%d, num_circuitbreaker_passthrough_failure=%d" % (
  37. success, failure, pass_through_failure)
  38. assert (success == 10), 'Expected num of success is 10'
  39. assert (failure == 10), 'Expected num of failure is 10'
  40. assert (pass_through_failure == 5), 'Expected num of CircuitBreaker passthrough failure is 5'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement