fix unit tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-04-29 12:31:33 +02:00
8 changed files with 73 additions and 42 deletions

View File

@@ -6,6 +6,7 @@ Communicator module
import logging
import requests
import requests.exceptions
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
@@ -15,6 +16,7 @@ __version__text__ = "1"
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
class Communicator:
"""
Class handling low level communication with consumers.
@@ -38,8 +40,11 @@ class Communicator:
"""
currentconsumer=self.currenctconsumer
LOGGER.info(f"Sending message to {currentconsumer}")
postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message})
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
try:
postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}, timeout=5)
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
LOGGER.exception(e) # Fun fact: ez azt jelenti, hogy elveszett az üzenet... ide valami retry kellene inkább más consumerek felé...
def discoveravailableconsumers(self) -> list:
"""Get the list of available consumer from the current primary consumer. Logs the received list.
@@ -48,12 +53,13 @@ class Communicator:
"""
try:
currentconsumer = self.currenctconsumer
response = requests.get(f'http://{currentconsumer}/consumers')
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
json = response.json()
LOGGER.info(f"List of currently available consumers: {json}")
return json
except Exception as e:
LOGGER.exception(e)
LOGGER.error("Could not query available consumer list!")
#LOGGER.exception(e)
return []
def isconsumeravailable(self) -> bool:
@@ -63,10 +69,10 @@ class Communicator:
"""
currentconsumer = self.currenctconsumer
try:
response = requests.get(f'http://{currentconsumer}/consumers')
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
isavailable = response.status_code == 200
except Exception as e:
LOGGER.exception(e)
#LOGGER.exception(e)
isavailable = False
LOGGER.info(f"Current consumer availability: {isavailable}")
return isavailable
@@ -78,10 +84,10 @@ class Communicator:
:return: True if available, False otherwise
"""
try:
response = requests.get(f'http://{consumer}/consumers')
response = requests.get(f'http://{consumer}/consumers', timeout=5)
isavailable = response.status_code == 200
except Exception as e:
LOGGER.exception(e)
#LOGGER.exception(e)
isavailable = False
LOGGER.info(f"Consumer {consumer} availability: {isavailable}")
return isavailable