Skip to content

Commit

Permalink
Fix the behavior of the decorator when notEnabled + some speliing mis…
Browse files Browse the repository at this point in the history
…takes.
  • Loading branch information
adhorn committed Jul 10, 2019
1 parent 5d2c665 commit 5a790d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
24 changes: 13 additions & 11 deletions lambda_function.py
@@ -1,8 +1,10 @@
from chaos_lib import corrupt_delay
from chaos_lib import corrupt_expection
from chaos_lib import corrupt_exception
from chaos_lib import corrupt_statuscode
from chaos_lib import SessionWithDelay

# from chaos_lib import *


# def dummy1():
# session = SessionWithDelay(delay=300)
Expand All @@ -15,25 +17,25 @@
# pass


# @corrupt_expection
# def lambda_handler(event, context):
# return {
# 'statusCode': 200,
# 'body': 'Hello from Lambda!'
# }


# @corrupt_delay
# @corrupt_exception
# def lambda_handler(event, context):
# return {
# 'statusCode': 200,
# 'body': 'Hello from Lambda!'
# }


@corrupt_statuscode
@corrupt_delay
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello from Lambda!'
}


# @corrupt_statuscode
# def lambda_handler(event, context):
# return {
# 'statusCode': 200,
# 'body': 'Hello from Lambda!'
# }
13 changes: 10 additions & 3 deletions python/chaos_lib.py
Expand Up @@ -28,7 +28,7 @@ def get_config(config_key):
try:
value = json.loads(param.value)
if not value["isEnabled"]:
return 0
return 0, 1
return value[config_key], value.get('rate', 1)
except InvalidParameterError as e:
# key does not exist in SSM
Expand All @@ -41,10 +41,13 @@ def get_config(config_key):
def corrupt_delay(func):
def latency(*args, **kw):
delay, rate = get_config('delay')
if not delay:
return func(*args, **kw)
print("delay: {0}, rate: {1}".format(delay, rate))
start = time.time()
# if delay and rate exist, delaying with that value at that rate
start = time.time()
if delay > 0 and rate >= 0:

# add latency approx rate% of the time
if random.random() <= rate:
time.sleep(delay / 1000.0)
Expand All @@ -60,10 +63,12 @@ def latency(*args, **kw):
return latency


def corrupt_expection(func):
def corrupt_exception(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
exception_msg, rate = get_config('exception_msg')
if not exception_msg:
return result
print("exception_msg from config {0} with a rate of {1}".format(exception_msg, rate))
# add injection approx rate% of the time
if random.random() <= rate:
Expand All @@ -78,6 +83,8 @@ def corrupt_statuscode(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
error_code, rate = get_config('error_code')
if not error_code:
return result
print("Error from config {0} at a rate of {1}".format(error_code, rate))
# add injection approx rate% of the time
if random.random() <= rate:
Expand Down

0 comments on commit 5a790d0

Please sign in to comment.