raise exception if heartbeat status_info is missing required fields

This commit is contained in:
Noah Levitt 2017-04-27 14:34:07 -07:00
parent c5da679b1d
commit a1c5a08790
3 changed files with 32 additions and 1 deletions

View File

@ -59,6 +59,13 @@ class ServiceRegistry(object):
Returns updated status info on success, un-updated status info on
failure.
'''
for field in 'role', 'heartbeat_interval', 'load':
if not field in status_info:
raise Exception(
'status_info is missing required field %s', field)
val = status_info['heartbeat_interval']
if not (isinstance(val, float) or isinstance(val, int)) or val <= 0:
raise Exception('heartbeat_interval must be a number > 0')
updated_status_info = dict(status_info)
updated_status_info['last_heartbeat'] = r.now()
if not 'first_heartbeat' in updated_status_info:

View File

@ -3,7 +3,7 @@ import codecs
setuptools.setup(
name='doublethink',
version='0.2.0.dev73',
version='0.2.0.dev74',
packages=['doublethink'],
classifiers=[
'Programming Language :: Python :: 2.7',

View File

@ -80,6 +80,30 @@ def test_leader_election(rr):
def test_service_registry(rr):
svcreg = doublethink.ServiceRegistry(rr)
# missing required fields
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"role":"foo","heartbeat_interval":1.0})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":1.0,"load":1})
# invalid heartbeat interval (we accept anything for load and role)
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":-1,"role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":"strang","role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":[],"role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":[1],"role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":{},"role":"foo","load":1})
with pytest.raises(Exception) as excinfo:
svcreg.heartbeat({"heartbeat_interval":{1:2},"role":"foo","load":1})
assert svcreg.available_service("yes-such-role") == None
assert svcreg.available_services("yes-such-role") == []
assert svcreg.available_services() == []