Skip to content

Commit

Permalink
adding unit tests... debugged the code
Browse files Browse the repository at this point in the history
  • Loading branch information
petersilva committed Jun 14, 2024
1 parent 029df42 commit 8ffd482
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
31 changes: 25 additions & 6 deletions sarracenia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def __repr__(self) -> str:

count_options = [
'batch', 'count', 'exchangeSplit', 'instances', 'logRotateCount', 'no',
'post_exchangeSplit', 'prefetch', 'messageCountMax', 'messageRateMax',
'messageRateMin', 'runStateThreshold_cpuSlow', 'runStateThreshold_reject', 'runStateThreshold_retry', 'runStateThreshold_slow',
'post_exchangeSplit', 'prefetch', 'messageCountMax', 'runStateThreshold_cpuSlow',
'runStateThreshold_reject', 'runStateThreshold_retry', 'runStateThreshold_slow',
]


Expand Down Expand Up @@ -338,6 +338,10 @@ def isTrue(S):
return S.lower() in ['true', 'yes', 'on', '1']

def parse_count(cstr):
"""
number argument accepts k,m,g suffix with i and b to use base 2 ) and +-
return value is integer.
"""
if cstr[0] == '-':
offset=1
else:
Expand All @@ -351,21 +355,36 @@ def parse_count(cstr):
return 0

def parse_float(cstr):
"""
like parse_count, numeric argument accepts k,m,g suffix and +-.
below 1000, return a decimal number with 3 digits max.
"""
if type(cstr) is not str:
return cstr

try:
fa = parse_count(cstr)
if abs(fa) < 1000:
if cstr[-1] in [ 'k', 'b', 'i' ]:
if cstr[-1] in [ 'b', 'i' ]:
if cstr[-1] in [ 'b', 'i' ]:
if cstr[-2] in [ 'k' ]:
fa=float(cstr[0:-2])*1024
else:
if cstr[-2] != 'k':
logger.error( f"malformed float: {cstr}, overriding to kilo" )
fa=float(cstr[0:-1])
elif cstr[-1] in [ 'k' ]:
fa=float(cstr[0:-1])*1000
else:
fa=float(cstr)

# apply 3 sig figs.
if abs(fa) > 1000:
fa=int(fa)
elif abs(fa) > 100:
fa=round(fa,1)
elif abs(fa) > 10:
fa=round(fa,2)
else:
fa=round(fa,3)

return fa
except Exception as Ex:
logger.error( f"failed to parse: {cstr} as a float value" )
Expand Down
16 changes: 16 additions & 0 deletions tests/sarracenia/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ def test_read_line_counts():
options.parse_line( "subscribe", "ex1", "subscribe/ex1", 1, "batch 1.9" )
assert( options.batch == 1 )

def test_read_line_floats():

options = sarracenia.config.default_config()

options.parse_line( "subscribe", "ex1", "subscribe/ex1", 1, "messageRateMax 1.5mb" )
assert( options.messageRateMax == 1572864 )

options.parse_line( "subscribe", "ex1", "subscribe/ex1", 1, "messageRateMax 0.5k" )
assert( options.messageRateMax == 500 )

options.parse_line( "subscribe", "ex1", "subscribe/ex1", 1, "messageRateMax 0.5kb" )
assert( options.messageRateMax == 512 )

options.parse_line( "subscribe", "ex1", "subscribe/ex1", 1, "messageRateMax 0.5b" )
assert( options.messageRateMax == 0.5 )


def test_read_line_sets():

Expand Down

0 comments on commit 8ffd482

Please sign in to comment.